Not a compiler bug.

mw mingwu at gmail.com
Thu Aug 13 07:56:31 UTC 2020


On Thursday, 13 August 2020 at 07:51:06 UTC, FeepingCreature 
wrote:
> "Not a bug." Or rather, same semantics as in C. The division 
> reinterprets a as unsigned, and the result is within the 
> positive range of a long, so it stays positive post-division.


https://issues.dlang.org/show_bug.cgi?id=21151

Let me add all my post together:

It can NOT silently do this, at least a warning.

BTW, on Windows, dmd correctly output -2500.

Just because C/C++ did it doesn't means it's correct.

And D supposed to be an improvement of C++.


OK, let me write this in this way to show it's impact:
==================================
import std.algorithm;
import std.stdio;

void main() {
   long[] a = [-5000, 0];
   long   c = sum(a) / a.length;
   writeln(c);
}
==================================


$ ./divbug
9223372036854773308


More information about the Digitalmars-d mailing list