[Tutor] the binary math "wall"

spir ☣ denis.spir at gmail.com
Tue Apr 20 19:41:35 CEST 2010

On Tue, 20 Apr 2010 09:58:06 -0700 (PDT)
Lowell Tackett <lowelltackett at yahoo.com> wrote:

> I'm running headlong into the dilemma of binary math representation, with game-ending consequences, e.g.:
> >>> 0.15
> 0.14999999999999999
> [...]
> The last line should be zero, and needs to be for me to continue this algorithm.
> Any of Python's help-aids that I apply to sort things out, such as formatting (%), or modules like "decimal" do nothing more than "powder up" the display for visual consumption (turning it into a string).  The underlying float value remains "corrupted",

You are wrong:

>>> from decimal import Decimal
>>> s  = "0.15"
>>> Decimal(s) == float(s)

This shows, maybe weirdly, that the decimal version != 0.14999999999999999 --since it is actually equal to 0.15. Decimals are *not* internally represented as binary values, but instead with groups of bits each coding a *decimal* digit; this is precisely the point. Else why do you think developpers would cope with such a problem?


vit esse estrany ☣


More information about the Tutor mailing list