# simple float numbers problem

Christopher A. Craig list-python at ccraig.org
Fri Nov 7 18:34:44 CET 2003

```Vio <vmilitaru at sympatico.ca> writes:

> Perhaps because Python translates "25.99" to "25.98999999999999998"
> and not "25.99", which may be the reason for this error (me
> guessing...). If that's the case, how do I force Python to only use
> 2 decimal points, and not "make up" superfluous decimals? Or if
> that's not the cause for the problem, how do I make Python see my
> math expression as TRUE (as it "should" be)?

Python does not translate 25.99 as 25.98999999999999998.  IEEE
(assuming your chip has the same fp as mine, which may not be valid)
floating point stores 25.99 sort of as the binary number
7315534644709949*(2**-48), which is the closest it can come to 25.99.

Of course, that doesn't tell you how to compare them.  You have two
choices there: The normal way to do floating point compares is to say
if abs(a-b) < 1e-10 then that's good enough.  If you really, really
need equality, then you need something other than a float.  You could
try rationals[0] or fixed points[1] but they both have other
problems...

see
http://www.python.org/doc/faq/general.html#why-are-floating-point-calculations-so-inaccurate

[0] http://sourceforge.net/project/showfiles.php?group_id=27765&release_id=90046

[1] http://fixedpoint.sourceforge.net/

--
Christopher A. Craig <list-python at ccraig.org>
"No one has ever called us untrustworthy" -- Steve Balmer

```