Math errors in python

Peter Otten __peter__ at web.de
Sun Sep 19 08:41:53 EDT 2004


Paul Rubin wrote:

> Peter Otten <__peter__ at web.de> writes:
>> 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")
> 
> I haven't tried 2.4 yet.  After

The auther is currently working on an installer, but just dropping it into
2.3's site-packages should work, too.

>     a = Decimal("1") / Decimal("3")
>     b = a * Decimal("3")
>     print b
> 
> What happens?  Is that arithmetic as the way I know it?

Decimal as opposed to rational:

>>> from decimal import *
>>> Decimal(1)/Decimal(3)
Decimal("0.3333333333333333333333333333")
>>> 3*_
Decimal("0.9999999999999999999999999999")

Many people can cope with the inaccuracy induced by base 10 representations
and are taken by surprise by base 2 errors.
But you are right I left too much room for interpretation.

Peter




More information about the Python-list mailing list