Do you like bounded integrals?

tsbockman via Digitalmars-d digitalmars-d at puremagic.com
Tue Aug 30 13:38:49 PDT 2016


On Tuesday, 30 August 2016 at 14:58:16 UTC, Chris Wright wrote:
> We can only track types, not values, and that strongly hampers 
> our ability to reduce ranges.

Playing with some examples, I think the real issue is that 
precise bounds tracking requires analyzing the formula as a 
whole, rather than just each operation individually:

auto blend(
   Bound!(int, 0, 100) x,
   Bound!(int, 0, 100) y,
   Bound!(int, 0, 100) z)
{
   enum Bound!(int, 100, 100) h = 100;

   return ((x * z) + (y * (h - z))) / h;
}

With propagation computed at the individual operation level, the 
above returns a `Bound!(int, 0, 200)`. But, using the expression 
as a whole it can easily be proven that the true maximum is `100`.

(With just-plain-wrong "union of the input ranges" propagation, 
the above will generate many spurious exceptions.)

Unfortunately, proving the true bounds statically in the general 
case would require something like a compile-time computer algebra 
system connected combined with AST macros. That's obviously 
overkill for what we're doing here.



More information about the Digitalmars-d mailing list