www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Casting from int to long

reply Yui Hosaka <hos hos.ac> writes:
On DMD32 D Compiler v2.089.0-dirty, Windows, the code

----
import std.stdio;

int f(int x) {
   return 1;
}

void main() {
   enum N = 100;
   auto a = new int[N];
   a[] = 1;
   long sum;
   foreach_reverse (i; 0 .. N) {
     const r = i % 5;
     sum += cast(long)(a[i]) * f(r);
   }
   writeln(sum);
}
----

gives the output 950, while it should be 100. This happens only 
with -O and without -m64 option.

I have encountered some kind of "32-bit / 64-bit issues" several 
times (https://issues.dlang.org/show_bug.cgi?id=20162, 
https://issues.dlang.org/show_bug.cgi?id=20128).
Nov 18 2019
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 11/18/19 7:16 AM, Yui Hosaka wrote:

 gives the output 950, while it should be 100.
Looks like a bug. Confirmed with dmd 2.088.0 compiling with -O -m32 on Ubuntu. Ali
Nov 18 2019
prev sibling parent Yui Hosaka <hos hos.ac> writes:
On Monday, 18 November 2019 at 15:16:11 UTC, Yui Hosaka wrote:
 I have encountered some kind of "32-bit / 64-bit issues" 
 several times (https://issues.dlang.org/show_bug.cgi?id=20162, 
 https://issues.dlang.org/show_bug.cgi?id=20128).
Not sure it is related, but DMD32 with -O has the following seemingly bug... ---- import std.stdio; void main() { foreach (a; 0L .. 4L) foreach (b; 0L .. 4L) { pragma(msg, typeof(a)); pragma(msg, typeof(b)); writeln(a, " ", b); } } ---- Compiler Output: ---- long long ---- Output: ---- 0 0 0 4294967297 0 2 0 4294967299 1 0 1 4294967297 1 2 1 4294967299 2 0 2 4294967297 2 2 2 4294967299 3 0 3 4294967297 3 2 3 4294967299 ----
Nov 25 2019