3.2*2 is 9.6 ... or maybe it isn't?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Jun 26 12:01:52 CEST 2009


On Thu, 25 Jun 2009 12:41:13 -0600, Michael Torrie wrote:

> If you want accurate math, check out other types like what is in the
> decimal module:
> 
>>>> import decimal
>>>> a=decimal.Decimal('3.2')
>>>> print a * 3
> 9.6

Not so. Decimal suffers from the exact same problem, just with different 
numbers:

>>> import decimal
>>> x = decimal.Decimal('1')/decimal.Decimal('3')
>>> 3*x == 1
False

Some numbers can't be represented exactly in base 2, and some numbers 
can't be represented exactly in base 10.


-- 
Steven



More information about the Python-list mailing list