public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* strange double compare problem
@ 2003-02-20  9:58 Robin Ericsson
  2003-02-20 13:03 ` John Love-Jensen
  0 siblings, 1 reply; 2+ messages in thread
From: Robin Ericsson @ 2003-02-20  9:58 UTC (permalink / raw)
  To: gcc-help

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2003-02-20 13:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-20  9:58 strange double compare problem Robin Ericsson
2003-02-20 13:03 ` John Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).