[Python-Dev] Expert floats
tim.one at comcast.net
Tue Apr 6 11:00:27 EDT 2004
>> >>> 3.3
>> confused and frightened them, and continues to confuse and frighten
>> almost everyone i teach.
> It bothered me when I first saw it (not in Python).
> It was still better than trying to figure out why my code was doing
> the wrong thing, when my math and logic were both clearly correct.
> To steal Simon Percivall's example, I assumed 2.20 - 1.20 == 1.00.
> It didn't even occur to me to check something as obviously true as
> that -- until I had learned and remembered that floats were not
I believe Ping would have
>>> 2.20 - 1.20
because "1.0000000000000002" is the shortest string that evals to the true
machine result. OTOH, he'd have
>> 2.20 - 1.10
The true machine result isn't decimal 1.1, but "1.1" is again the shortest
string that evals to the true machine result. Presumably newbies aren't
confused or frightened by 1.0000000000000002, because their intuition about
shortest-possible reproducing decimal representations of binary floats is
finely honed <wink>.
Most people should use Decimal:
>>> print Decimal("2.20") - Decimal("1.20")
>>> print Decimal("2.20") - Decimal("1.1")
The output is "what they expect" then, 100% truthful (the internal results
are exactly 1. and decimal 1.1, respectively), and even preserves a useful
notion of significant trailing zeroes.
More information about the Python-Dev