Math errors in python

Peter Otten __peter__ at web.de
Sat Sep 18 13:29:47 EDT 2004


Radioactive Man wrote:

> thing would happen.  Any suggestions for a way to fix this sort of
> error?

Starting with Python 2.4 there will be the 'decimal' module supporting
"arithmetic the way you know it":

>>> from decimal import *
>>> Decimal("12.10") + Decimal("8.30")
Decimal("20.40")
>>> Decimal("1.1") - Decimal("0.2")
Decimal("0.9")
>>> Decimal("130.0")-Decimal("129.0") == Decimal("1.0")
True
>>> a, b, c = map(Decimal, "0.013 0.0129 0.0001".split())
>>> a, b, c
(Decimal("0.013"), Decimal("0.0129"), Decimal("0.0001"))
>>> (a-b) == c
True


Peter




More information about the Python-list mailing list