Problem with floating point precision

Roy Smith roy at panix.com
Tue Aug 17 12:23:51 EDT 2004


j_mckitrick at bigfoot.com (j_mckitrick) wrote:

> I checked the docs, but found nothing about floating point issues.
> 
> I am working on an app that deals with currency values, and only does
> addition and subtraction (no division).  But I am starting to see
> values that should be the same failing a compare!  What is the
> workaround or correct way to compare 2 floats?

A full analysis of floating point math would fill a book.  The details 
depend greatly on the underlying FP hardware in your system, although 
these days it's pretty hard to find a machine which doesn't use IEEE 754 
standard math, so you might start by googling for "ieee 754".

Python 2.4 will have a new Decimal numeric type, which was invented to 
resolve many of the problems that happen when trying to use floats for 
currency calculations.  Take a look at 
http://www.python.org/peps/pep-0327.html for more information.

There is an alpha version of 2.4 that was released recently; you might 
want to download that and start playing around with the Decimal type to 
see if that solves your problem.  I'm sure other people here could give 
a better estimate of when there will be a production release of 2.4, but 
my guess is that it's a few months away.



More information about the Python-list mailing list