This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

strange double compare problem


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;
	}
}




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]