Floating point bug?

Jeff Schwab jeff at schwabcenter.com
Fri Feb 15 03:19:42 CET 2008


Zentrader wrote:
>> 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.

Diez was not claiming that the decimal module did anything less than 
what it promised.  He just pointed out that the module does not support 
infinitely precise floating-point arithmetic, any more than tradition 
base-2 representations do.  Please review the thread (the parts you 
snipped) for clarification.



More information about the Python-list mailing list