Fun with floating point

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Feb 8 01:33:22 PST 2015


On Sun, 08 Feb 2015 09:19:06 +0000, Kenny wrote:

> I asked more about this case:
> float f = 16777216.0f;
> if (f == f + 1.0f)
>   writeln("Works");
> 
> Although all operands are 32bit FP the result is not guaranteed to be
> equal to the result for FP32 computations as specified by IEEE-754.

i think you are mixing two things here. IEEE doesn't specify which 
internal representation compilers should use, it only specifies the 
results for chosen representation. so if D specs states that `float` 
calculations are always performing with `float` precision (and specs 
aren't), your sample should work.

but the specs says that compiler is free to promote such expression to 
any type it wants, it just should not loose precision. so the actual type 
of `f` in `f == f + 1.0f` can be freely promoted to `double`, `real` or 
even to some GMP representation.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150208/55d128b3/attachment.sig>


More information about the Digitalmars-d-learn mailing list