Math errors in python

DogWalker forestiero at qwest.net
Sat Sep 18 12:58:26 EDT 2004


Have a look at the FAQ (before the response to your message builds).

----- Original Message ----- 
From: Radioactive Man 
Newsgroups: comp.lang.python
To: python-list at python.org 
Sent: Saturday, September 18, 2004 9:50 AM
Subject: Math errors in python


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?
-- 
http://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list