A strange div bug on Linux x86_64, (both dmd & ldc2): long -5000 / size_t 2 = 9223372036854773308

jmh530 john.michael.hall at gmail.com
Thu Aug 13 14:10:54 UTC 2020

On Thursday, 13 August 2020 at 13:33:19 UTC, bachmeier wrote:
>> Also, note that the true mean of vec above is -22.75, which 
>> wouldn't even be the result of your function if length 
>> returned a signed variable, because you would be doing integer 
>> division. A person who comes to D without ever having 
>> programmed before would get tripped up by that too.
> That's why that behavior needs to be changed as well. It's 
> horrible to implicitly cast from int to double when doing so 
> results in obviously wrong behavior.

I'm a little confused by this. My point was that the way D works 
now is that there is no error in the below code.
assert(-91 / 4 == -22);
even though the result is not the mathematically correct one 
(-22.75). There is no implicit cast to double in this case. 
Modifying it to
assert(-91.0 / 4 == -22.75);
gives the right result, but if you want to be more explicit and 
start from a signed variable and an unsigned variable, you would 
assert(cast(double) -91 / cast(double) 4u == -22.75);

More information about the Digitalmars-d mailing list