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

mw mingwu at gmail.com
Fri Aug 14 02:10:20 UTC 2020


On Thursday, 13 August 2020 at 23:47:30 UTC, Walter Bright wrote:

> checkedint is really a nice package. I encourage you to check 
> it out.

Yes, it works: 
https://github.com/mingwugmail/dlang_tour/blob/master/divbug.d

But it need a more *automatic* way to use this package to be 
useful, i.e. minimal change on existing code base (all over the 
places), from:

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

to:

----------------------------------------------------------
alias Long = Checked!long;

void checked_div() {
   Long[] a = [checked(-5000L), checked(0L)];
   Long   c = sum(a) / a.length;  // compile error
   writeln(c);  // Checked!(long, Abort)(-2500)
}
----------------------------------------------------------


currently, there is a compile error
Error: template std.experimental.checkedint.Checked!(long, 
Abort).Checked.__ctor cannot deduce function from argument types 
!()(Checked!(ulong, Abort))

have to re-write it as:
```
   Long c = sum(a);
   c /= a.length;
```



More information about the Digitalmars-d mailing list