strange double compare problem

John Love-Jensen eljay@adobe.com
Thu Feb 20 13:03:00 GMT 2003


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



More information about the Gcc-help mailing list