Not a compiler bug.
FeepingCreature
feepingcreature at gmail.com
Thu Aug 13 08:10:16 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
Yes, hence: not a compiler bug, but a spec bug. Implicit
conversions should never throw away data, and silently converting
from signed to unsigned, especially for a sign-sensitive
operation like division, is insane. I'm not saying it's not an
issue, I'm saying it should be raised against the spec, not the
compiler, which faithfully implements it.
More information about the Digitalmars-d
mailing list