Math errors in python

Paul Rubin http
Sun Sep 19 12:14:22 CEST 2004


Gary Herron <gherron at islandtraining.com> writes:
> Any representation of the infinity of numbers on a finite computer
> *must* necessarily be unable to represent some (actually infinity
> many) of those numbers.  The inaccuracies stem from that fact.

Well, finite computers can't even represent all the integers, but 
we can reasonably think of Python as capable of doing exact integer
arithmetic. 

The issue here is that Python's behavior confuses the hell out of some
new users.  There is a separate area of confusion, that

   a = 2 / 3

sets a to 0, and to clear that up, the // operator was introduced and
Python 3.0 will supposedly treat / as floating-point division even
when both operands are integers.  That doesn't solve the also very
common confusion that (1.0/3.0)*3.0 = 0.99999999.  Rational arithmetic
can solve that.

Yes, with rational arithmetic, it will still be true that
sqrt(5.)**2.0 doesn't quite equal 5, but hardly anyone ever complains
about that.

And yes, there are languages that can do exact arithmetic on arbitrary
algebraic numbers, but they're normally not used for general-purpose
programming.



More information about the Python-list mailing list