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

bachmeier no at spam.net
Thu Aug 13 10:40:33 UTC 2020


On Thursday, 13 August 2020 at 10:10:34 UTC, H. S. Teoh wrote:

> So, in the spirit of defensive programming, I recommend 
> avoiding mixing signed/unsigned values in this way.

This is not a practical solution. [1, 2, 3, 4].length returns 
ulong, which guarantees this type of mixing goes on without 
people even realizing it's in their code base.

Imagine a new user to the language wanting to compute the mean of 
an array of numbers:

import std;
void main()
{
     long sum = 0;
     long[] vec = [-112, 2, 23, -4];
     foreach(val; vec) {
         sum += val;
     }
     writeln(sum/vec.length);
}

This is inexcusable.


More information about the Digitalmars-d mailing list