Unexpected behaviour of math.floor, round and int functions (rounding)

Chris Angelico rosuav at gmail.com
Fri Nov 19 17:15:38 EST 2021


On Sat, Nov 20, 2021 at 9:07 AM Ben Bacarisse <ben.usenet at bsb.me.uk> wrote:
>
> Chris Angelico <rosuav at gmail.com> writes:
>
> > On Sat, Nov 20, 2021 at 5:08 AM ast <ast at invalid> wrote:
>
> >>  >>> 0.3 + 0.3 + 0.3 == 0.9
> >> False
> >
> > That's because 0.3 is not 3/10. It's not because floats are
> > "unreliable" or "inaccurate". It's because the ones you're entering
> > are not what you think they are.
> >
> > When will people understand this?
> >
> > (Probably never. Sigh.)
>
> Most people understand what's going on when it's explained to them.  And
> I think that being initially baffled is not unreasonable.  After all,
> almost everyone comes to computers after learning that 3/10 can be
> written as 0.3.  And Python "plays along" with the fiction to some
> extent.  0.3 prints as 0.3, 3/10 prints as 0.3 and 0.3 == 3/10 is True.

In grade school, we learn that not everything can be written that way,
and 1/3 isn't actually equal to 0.3333333333. Yet somehow people
understand that computers speak binary ("have you learned to count
yet, or are you still on zeroes and ones?" -- insult to a machine
empire, in Stellaris), but don't seem to appreciate that floats are
absolutely accurate and reliable, just in binary.

But lack of knowledge is never a problem. (Or rather, it's a solvable
problem, and I'm always happy to explain things to people.) The
problem is when, following that lack of understanding, people assume
that floats are "unreliable" or "inaccurate", and that you should
never ever compare two floats for equality, because they're never
"really equal". That's what leads to horrible coding practices and
badly-defined "approximately equal" checks that cause far more harm
than a simple misunderstanding ever could on its own.

ChrisA


More information about the Python-list mailing list