float comparison gives wrong result in loop?

Bradley Smith digitalmars-com at baysmith.com
Thu Sep 21 16:42:15 PDT 2006


> import std.stdio;
> 
> void main() {
>     const float STEP_SIZE = 0.2f;
> 
>     float j = 0.0f;
>     real q;
>     while (j <=  ( 1.0f / STEP_SIZE)) {
>         j += 1.0f;
>         q = j;
>         writefln("%a %a %a", q, j, ( 1.0f / STEP_SIZE));
>         writefln(j <= ( 1.0f / STEP_SIZE));
>     }
>     float step2 = STEP_SIZE;
> 
>     assert(!(j <= ( 1.0f / step2))); // passes
>     assert(!(j <= ( 1.0f / STEP_SIZE))); // fails!
> }

After some research, I can now explain this last part. Because of 
compile-type evaluation of const values, the second statement is effectively
   cast(real) j <= cast(real)(1.0f / STEP_SIZE)

However the first is effectively
   cast(real) j <= (cast(real) 1.0f / cast(real) step2)

which when printed with
   writefln("%a <= %a", cast(real)j, cast(real)1.0f / cast(real)step2);
gives
   0x1.4p+2 <= 0x1.3fffffb0000014p+2

Unfortunately, this still does not explain why the loop doesn't print 
"false" before exiting.



More information about the Digitalmars-d-learn mailing list