Really basic problem

Diez B. Roggisch deets at nospam.web.de
Mon Oct 8 18:22:04 CEST 2007


Zentrader wrote:

> You can use Python's decimal class if floating point arithmetic is not
> exact enough

This is a misleading statement. While it's true that decimal can be more
precise in the sense that smaller fractions are representable, the
underlying problem of certain values not being representable properly &
leading to rounding errors still exists:



>>> import decimal
>>> d = decimal.Decimal
>>> d("1") / d("3")
Decimal("0.3333333333333333333333333333")
>>> otrd = d("1") / d("3")
>>> otrd * 3
Decimal("0.9999999999999999999999999999")
>>> otrd * d("3")
Decimal("0.9999999999999999999999999999")
>>>


Diez



More information about the Python-list mailing list