Fun with floating point

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Feb 7 15:26:05 PST 2015


On 02/07/2015 01:33 PM, 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;
> }
>
>

OK, ignore some of my earlier response. :)

The code above works with dmd git head 64-bit compilation and prints the 
following:

eps = 1, max_f = 1.67772e+07

You can use the %a format specifier when debugging this issue. It allows 
you see the bits of the floating point value:

       writefln("eps: %a", 0.1);

double 0.1 on my system:

eps: 0x1.999999999999ap-4

Ali



More information about the Digitalmars-d-learn mailing list