Fun with floating point

Peter Alexander via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Feb 7 15:13:54 PST 2015


On Saturday, 7 February 2015 at 21:33:51 UTC, Kenny wrote:
> The above code snippet works correctly when I use LDC compiler 
> (it finds expected 'f' value and prints it to console). I'm 
> wondering is it a bug in DMD?
>
> p.s. the final code used by both compilers:
>
> import std.stdio;
> import std.conv;
>
> int main(string[] argv)
> {
>     const float eps = 1.0f;
>     float f = 0.0f;
>     while (f + eps != f)
>         f += 1.0f;
>
>     writeln("eps = ", eps, ", max_f = ", f);
>     return 0;
> }

Intermediate calculations may be performed at higher precision 
than the precision of the values themselves. In particular, the f 
+ eps may be performed with 80 bits of precision, even though 
both values are 32-bit. The comparison will then fail.

The reason for the difference between DMD and LDC is that DMD 
tends to use the FPU more with 80 bits of precision, whereas LDC 
and GDC will use the SSE2 instructions, which only support 32-bit 
and 64-bit precision.


More information about the Digitalmars-d-learn mailing list