This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
strange double compare problem
- From: Robin Ericsson <robin dot ericsson at noname4us dot com>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 19 Feb 2003 11:47:34 +0100
- Subject: strange double compare problem
- Organization: Noname4Us AB
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;
}
}