Math errors in python

Radioactive Man rm at rm.rm
Sat Sep 18 18:50:16 CEST 2004


In python 2.3 (IDLE 1.0.3) running under windows 95, I get the
following types of errors whenever I do simple arithmetic:
      
1st example:
>>> 12.10 + 8.30
20.399999999999999
>>> 1.1 - 0.2
0.90000000000000013


2nd example(no errors here):
>>> bool(130.0 - 129.0 == 1.0)
True


3rd example:
>>> a = 0.013
>>> b = 0.0129
>>> c = 0.0001
>>> [a, b, c]
[0.012999999999999999, 0.0129, 0.0001]
>>> bool((a - b) == c)
False


This sort of error is no big deal in most cases, but I'm sure it could
become a problem under certain conditions, particularly the 3rd
example, where I'm using truth testing.  The same results occur in all
cases whether I define variables a, b, and c, or enter the values
directly into the bool statement.  Also, it doesn't make a difference
whether "a = 0.013" or "a = 0.0130".

I haven't checked this under windows 2000 or XP, but I expect the same
thing would happen.  Any suggestions for a way to fix this sort of
error?



More information about the Python-list mailing list