[Tutor] Python 2.5.4 - error in rounding

Wayne Werner waynejwerner at gmail.com
Sat May 22 16:19:07 CEST 2010


On Sat, May 22, 2010 at 7:32 AM, Steven D'Aprano <steve at pearwood.info>wrote:

> Why do people keep recommending Decimal? Decimals suffer from the exact
> same issues as floats,
>

This is exactly incorrect! The Decimal operator offers /exact/ decimal point
operations. They implement non-hardware operations to preserve exactness.
For more information on exactly what and how the decimal module does what it
does, see the following:

http://docs.python.org/library/decimal.html
<http://docs.python.org/library/decimal.html>http://speleotrove.com/decimal/
 <http://speleotrove.com/decimal/>http://754r.ucbtest.org/standards/854.pdf

 plus they are slower.
>

Because if memory serves correctly the Python implementation uses serial
arithmetic, rather than the hardware implementation of floating point
calculations.

Please stop propagating myths about the Decimal module.

For an example about the exactness of Decimal v Float:

>>> d = Decimal(1)/Decimal(3)
>>> d
Decimal('0.3333333333333333333333333333')
>>> d*Decimal(3)
Decimal('0.9999999999999999999999999999')
>>> d = 1/3.0
>>> d*3
1.0

3*.3333 != 1

Floating point rounds that, while Decimal does not.

-Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100522/d987fc0b/attachment.html>


More information about the Tutor mailing list