[Python-ideas] Python Numbers as Human Concept Decimal System

David Mertz mertz at gnosis.cx
Sat Mar 8 00:12:00 CET 2014


On Fri, Mar 7, 2014 at 2:33 PM, Chris Kaynor <ckaynor at zindagigames.com>wrote:

> Presuming enough digits of precision, every binary float could be
> represented precisely in decimal. Decimal is base 10, which factors to 2
> and 5, meaning all powers of 1/2 and 1/5 can be precisely represented.
> Binary floats can only be powers of 1/2.
>

This is half true.  Yes, the factors of 10 are 2 and 5.  So every number
that can be exactly represented in base-2 or in base-5 can also be
represented exactly in base-10 (given enough digits.

The problem is that the converse is not the case.  So in the case Mark
presents several times:

>>> from decimal import Decimal
>>> a = 0.1   # store a binary float
>>> Decimal(a)
Decimal('0.1000000000000000055511151231257827021181583404541015625')

That decimal number *is* an exact representation of the value referenced by
'a'.  So if that's all you want, no problem exists.

But the problem that does exist is that there is *no* binary floating point
representation that is exactly the same numeric value as Decimal('0.1').
 This would be true, not only for float32 or float64 values that Python has
(depending on the machine architecture and compilation switches), it would
also be true if we used float128, or float256, or float8192 numbers.

And again, while that one example might lead you to think that decimal
floating point is simply better, we don't need to go far to find a value
like Fraction('1/3') that simply cannot have an exact representation in
decimal either, no matter how many digits of precision we decide to use.

-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140307/aefcef70/attachment.html>


More information about the Python-ideas mailing list