Why does this floating point comparison fail?

Jonathan M Davis jmdavisProg at gmx.com
Thu Dec 16 00:25:11 PST 2010


Maybe I'm just totally missing something, but this seems really wrong to me. 
This program:

import std.stdio;

void main()
{
    writeln(2.1);
    writeln(2.1 == 2.1);
    writeln(3 * .7);
    writeln(2.1 == 3 * .7);

    auto a = 2.1;
    auto b = 3 * .7;
    writeln(a);
    writeln(b);
    writeln(a == b);
}



prints out

2.1
true
2.1
false
2.1
2.1
true


How on earth is 2.1 not equal to 3 * .7? Adding extra parens doesn't help, so 
it's not an operator precedence issue or anything like that. For some reason, 
the result of 3 * .7 is not considered to be equal to the literal 2.1. What's 
the deal? As I understand it, floating point operations at compile time do not 
necessarily match those done at runtime, but 2.1 should be plenty exact for both 
compile time and runtime. It's not like there are a lot of digits in the number, 
and it prints out 2.1 whether you're dealing with a variable or a literal. 
What's the deal here? Is this a bug? Or am I just totally misunderstanding 
something?

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list