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

* Re: strange double compare problem
  2003-02-20  9:58 strange double compare problem Robin Ericsson
@ 2003-02-20 13:03 ` John Love-Jensen
  0 siblings, 0 replies; 2+ messages in thread
From: John Love-Jensen @ 2003-02-20 13:03 UTC (permalink / raw)
  To: Robin Ericsson, gcc-help

Hi Robin,

You are mistaken.

The value 55.8000 is not equal to 52.8500 + 2.9500 using floating point
math.

The reason that it is not equal is because floating point representation is
binary, and the numbers you are using do not have an EXACT binary
representation.  So they are approximated.

The binary approximation of 52.8500 plus the binary approximation of 2.9500
does not equal the binary approximation of 55.8000.  Which is an unexpected
surprise for many people.

In you equality check, you need to do a bounds test.  The bounds test needs
to allow a fudge factor, and within that fudge factor you'd call it
(more-or-less) "equal".

Or you need to use integers with an implied fixed point (and you'll have to
compensate for that fixed point during multiplication or division).

Or you need to use BCD math, with fixed point.

Or you need to link against a robust math library, such as calc:
http://www.isthe.com/chongo/tech/comp/calc/

Sincerely,
--Eljay

^ 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).