float.max + 1.0 does not overflow

Dave Jones dave at jones.com
Thu Dec 28 10:43:57 UTC 2017


On Wednesday, 27 December 2017 at 14:14:42 UTC, Benjamin Thaut 
wrote:
> On Wednesday, 27 December 2017 at 13:40:28 UTC, rumbu wrote:
>> Is that normal?
> It computes the difference between float.max and the next 
> smaller reprensentable number in floating point. The difference 
> printed by the program is:
> 20282409603651670423947251286016.0
>
> As you might notice this is siginificantly bigger then 1.0. 
> Floating point operations work like this: They perform the 
> operation and then round to the nearest representable number in 
> floating point. So adding 1.0 to float.max and then rounding to 
> the nearest representable number will just give you back 
> float.max. If you however add float.max and float.max the next 
> nearest reprensentable number is float.inf.

The float with the lower exponent would have to be shifted to 
match the higher which means 1.0 would be shifted something like 
156 bits to the right before the addition can be done. If you 
shift right more bits than are in the mantissa then it get 
rounded to zero. Hence once the two values are lined up to do the 
actual op it becomes float.max + 0.0.

That said i suspect the OP was expecting the FPU unit to catch 
that in theory it should overflow. Not that the actual op would 
overflow but that the FPU would be checking the values on input. 
Maybe.






More information about the Digitalmars-d-learn mailing list