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

Chris Angelico rosuav at gmail.com
Sat Nov 20 17:27:24 EST 2021


On Sun, Nov 21, 2021 at 9:22 AM Grant Edwards <grant.b.edwards at gmail.com> wrote:
>
> On 2021-11-20, Chris Angelico <rosuav at gmail.com> wrote:
>
> > But you learn that it isn't the same as 1/3. That's my point. You
> > already understand that it is *impossible* to write out 1/3 in
> > decimal. Is it such a stretch to discover that you cannot write 3/10
> > in binary?
>
> For many people, it seems to be.
>
> There are plenty of people trying to write code who don't even under
> the concept of different bases.
>
> I remember trying to explain the concept of CPU registers, stacks,
> interrupts, and binary representations to VAX/VMS FORTRAN programmers
> and getting absolutely nowhere.
>
> Years later, I went through the same exercise with a bunch of Windows
> C++ programmers, and they seemed similarly baffled.
>
> Perhaps I was just a bad teacher.
>

And to some extent, that's not really surprising; not everyone can
think the way other people do, and not everyone can think the way
computers do. But it seems that, in this one specific case, there's a
massive tendency to (a) misunderstand, and then (b) belligerently
assume that the computer acts the way they want it to act. And then
sometimes (c) get really annoyed at the computer for not being a
person, and start the cargo cult practice of "always use a
nearly-equal function instead of testing for equality", which we've
seen in this exact thread.

That's what I take issue with: the smug "0.1 + 0.2 != 0.3, therefore
computers are wrong" people, and the extremely unhelpful "never use ==
with floats" people.

ChrisA


More information about the Python-list mailing list