strange double compare problem

Robin Ericsson robin.ericsson@noname4us.com
Wed Feb 19 10:46:00 GMT 2003


Can someone explain this problem.

When this is compiled with g++ (tested with 2.96, 3.1 and 3.3) 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;
	}
}





More information about the Gcc-bugs mailing list