strange double compare problem

Robin Ericsson robin.ericsson@noname4us.com
Thu Feb 20 09:58:00 GMT 2003


Hi,


I sent this to gcc-bugs yesterday by misstake, I think gcc-help would be
a better list for this.

Can someone explain this problem.

When this is compiled with g++ (tested with 2.96, 3.1 and 3.3 on a few
different platforms thanks to sourceforge) it produces "not equal" even
though the values is the same.

Changing test2 and test3 to float resolves the problem, but why doesn't
it work with double and according to my man and headerfiles, atof should
return a double.



#include <cstdlib>
#include <string>
#include <iostream>

using namespace std;

int main()
{
        string test1 = "55.8000";
        double test2 = (52.85 + 2.95);
        double test3 = atof(test1.c_str());

        cout << "test1 is : " << test1 << endl;
        cout << "test2 is : " << test2 << endl;
        cout << "test3 is : " << test3 << endl;

        if (test2 == test3)
        {
                cout << "equal" << endl;
        }       
        else
        {
                cout << "not equal" << endl;
        }
}



best regards
Robin



More information about the Gcc-help mailing list