Time to move std.experimental.checkedint to std.checkedint ?

tsbockman thomas.bockman at gmail.com
Mon Mar 29 22:47:49 UTC 2021


On Monday, 29 March 2021 at 20:00:03 UTC, Walter Bright wrote:
> It isn't even clear what the behavior on overflows should be. 
> Error? Wraparound? Saturation?

It only seems unclear because you have accepted the idea that 
computer code "integer" operations may differ from mathematical 
integer operations in arbitrary ways. Otherwise, the algorithm is 
simple:

     if(floor(mathResult) <= codeResult && codeResult <= 
ceil(mathResult))
         return codeResult;
     else
         signalErrorSomehow();

Standard mathematical integer addition does not wrap around or 
saturate. When someone really wants an operation that wraps 
around or saturates (not just for speed's sake), then that is a 
different operation and should use a different name and/or 
type(s), to avoid sowing confusion and ambiguity throughout the 
codebase for readers and compilers.

All of the integer behavior that people complain about violates 
this in some way: wrapping overflow, incorrect signed-unsigned 
comparisons, confusing/inconsistent implicit conversion rules, 
undefined behavior of various more obscure operations for certain 
inputs, etc.

Mathematical integers are a more familiar, simpler, easier to 
reason about abstraction. When we use this abstraction, we can 
draw upon our understanding and intuition from our school days, 
use common mathematical laws and formulas with confidence, etc. 
Of course the behavior of the computer cannot fully match this 
infinite abstraction, but it could at least tell us when it is 
unable to do what was asked of it, instead of just silently doing 
something else.


More information about the Digitalmars-d mailing list