Not a compiler bug.

John Colvin john.loughran.colvin at gmail.com
Thu Aug 13 08:07:56 UTC 2020


On Thursday, 13 August 2020 at 07:56:31 UTC, mw wrote:
> 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

The windows version is probably different because you are 
building a 32 bit exe and therefore size_t is uint, which will be 
promoted to long in the division.

If you do a bit of a search on the forums you'll find this issue 
having come up many times before and you might find those 
discussions informative.


More information about the Digitalmars-d mailing list