Floating point bug?

Diez B. Roggisch deets at nospam.web.de
Fri Feb 15 15:30:13 EST 2008


Zentrader schrieb:
>> That's a misconception. The decimal-module has a different base (10
>> instead of 2), and higher precision. But that doesn't change the fact
>> that it will expose the same rounding-errors as floats do - just for
>> different numbers.
>>
>>  >>> import decimal as d
>>  >>> d = d.Decimal
>>  >>> d("1") / d("3") * d("3")
>> Decimal("0.9999999999999999999999999999")
> 
> Surely you jest.  Your example is exact to 28 digits.  Your attempted
> trick is to use a number that never ends (1/3=0.3333...).  It would
> only convert back to one if you have and infinite number of
> significant digits.  That has nothing to do with the Python decimal
> module (which does what it claims).  It is one of the idiosyncrasies
> of the base 10 number system.  Remember we are working with base 10
> decimals and not fractions.

The point is that all numbering systems with a base + precision will 
have (rational) values they can't exactly represent. Q\R is of course 
out of the question by definition....

And the poster I replied to said

"""
This is true.  Fortunately Python does provide a module which allows
you to work with exact floating point quantities.
"""

Which is not more true for decimal as it is for IEEE754 floating points. 
Just for other values.

Diez



More information about the Python-list mailing list