[Python-Dev] Expert floats

Tim Peters tim.one at comcast.net
Tue Apr 6 12:23:09 EDT 2004


[Tim]
>> I believe Ping would have
>>
>> >>> 2.20 - 1.20
>> 1.0000000000000002

[Andrew Koenig]
> As would I.

Only sometimes, right?  You earlier gave a list of 4 behaviors you sometimes
want, and that particular output isn't suitable for all of them.

>> OTOH, he'd have
>>
>> >>> 2.20 - 1.10
>> 1.1

> Believe it or not, this apparent anomaly doesn't bother me, but
>
> >>> 1.1
> 1.1000000000000001
>
> does.  The reason is that the first example does arithmetic, and it
> is easy to explain that floating-point arithmetic is not completely
> accurate.

Wouldn't it bother you to give a wrong explanation?  It's simply impossible
to explain why 2.2-1.2 is inaccurate without explaining that "2.2" and/or
"1.2" are already inaccurate on their own.  The *arithmetic* in both
examples is exactly correct (if you watched the hardware's inexact flag
across the subtractions, you'd see that it doesn't get set; x-y is always
exact in 754 binary floating point when x and y are within a factor of 2 of
each other).  The only cause of "funny results" in these examples is
representation error.

So trying to "explain" this by telling a newbie that arithmetic is
inaccurate may pacify them, but at the cost of increasing their confusion
(the explanation they're given then doesn't actually explain anything about
what they're seeing, and the true cause has been left untold).

OTOH,

>>> 2.2
2.2000000000000002
>>> 1.1
1.1000000000000001
>>>

points to the true cause, and the Tutorial Appendix on binary fp issues has
been successful in bringing newbies to a correct understanding of that.




More information about the Python-Dev mailing list