Reading the documentation

Paul Moore p.f.moore at gmail.com
Fri Aug 25 09:41:48 EDT 2017


On Friday, 25 August 2017 14:16:05 UTC+1, Chris Angelico  wrote:
> Yep. We're in agreement on that. My only point about the confusion was
> the way in which you could get "wrong answers" in binary but "right
> answers" in decimal, leading to the "0.1 + 0.2 != 0.3" problem. That's
> a sum that works flawlessly in decimal but not in binary.

And in practice this *is* the example that causes the most confusion, in terms of people thinking what Python does is "wrong". The usual "explanation" is something along the lines of "floats aren't exact" or "rounding", neither of which is the real explanation (and "floats aren't exact" isn't even true!)

The real explanation is that "0.1" is a textual representation in your source, that gets converted into a value in your code that doesn't actually equate to what you *meant* by 0.1 (i.e. 1/10). Explaining the difference between the form of a literal, and the resulting actual value used in your program, is surprisingly hard (I say this after having recently spent rather longer than I thought I'd need to explaining the difference between a "date literal" and a "date object" to a colleague who's far from being a "dumb newbie").

Paul



More information about the Python-list mailing list