Do you like bounded integrals?

tsbockman via Digitalmars-d digitalmars-d at puremagic.com
Tue Aug 30 06:24:04 PDT 2016


On Tuesday, 30 August 2016 at 03:37:06 UTC, Chris Wright wrote:
> On Mon, 29 Aug 2016 18:20:05 +0000, tsbockman wrote:
>> They should. Generally speaking, if that doesn't produce 
>> reasonable bounds (leaving aside rounding errors) at the end 
>> of the computation, it means that the logic of the computation 
>> itself is wrong.
>
> The ranges expand very fast. Addition and subtraction double 
> the range, multiplication squares it, exponentiation is n^^n. 
> Larger bounds are usually not useful bounds.

Ranges don't always grow. Some operations will also cause them to 
shrink, if they're really being tracked correctly: bitwise AND, 
integer division, and modulus all tend to do so.

If the ranges are intrinsically significant (it's logically 
impossible for a creature to have -7 eyes) and the calculation is 
meaningful and correct, things should balance out at the end to 
produce a useful range for the final result.

On the other hand, arbitrary sanity check ranges (it's merely 
extremely unlikely for a creature to have 2147483647 eyes) 
shouldn't be passed on to intermediate values at all; they must 
be assigned manually based on human intuition, and this is only 
really worth doing for inputs and outputs. In such cases, the 
best return value for binary operations would be normal 
(conceptually) unbounded CheckedInt.

Give me mathematically correct bounds, or no bounds at all. 
Anything else will cause problems.


More information about the Digitalmars-d mailing list