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

Chris Angelico rosuav at gmail.com
Sat Nov 20 21:01:21 EST 2021


On Sun, Nov 21, 2021 at 12:56 PM Avi Gross via Python-list
<python-list at python.org> wrote:
>
> Not at all, Robb. I am not intending to demean Mathematicians as one of my degrees is in that subject and I liked it. I mean that some things in mathematics are not as intuitive to people when they first encounter them, let alone those who never see them and then marvel at results and have expectations.
>
> The example I gave, is NOW, indeed on quite firm footing but for quite a while was not.
>
> What we have in this forum recently is people taking pot shots at aspects of Python where in a similar way, they know not what is actually happening and insist it be some other way. Some people also assume that an email message work any way they want and post things to a text-only group that other cannot see or become badly formatted or complain why a very large attachment makes a message be rejected. They also expect SPAM checkers to be perfect and never reject valid messages and so on.
>
> Things are what they are, not what we wish them to be. And many kinds of pure mathematics live in a Platonic world and must be used with care. Calculus is NOT on a firm footing when any of the ideas in it are violated. A quantum Mechanical universe at a deep level does not have continuity so continuous functions may not really exist and there can be no such thing as an infinitesimal smaller than any epsilon and so on. Much of what we see at that level includes things like a probabilistic view of an electron cloud forming the probability that an electron (which is not a mathematical point) is at any moment at a particular location around an atom. But some like the p-orbital have a sort of 3-D figure eight shape (sort of a pair of teardrops) where there is a plane between the two halves with a mathematically zero probability of the electron ever being there. Yet, quantum tunneling effects let it dross through that plane without actually ever being in the plane because various kinds of
>  quantum jumps in a very wiggly space-time fabric can and will happen in a way normal mathematics may not predict or allow.
>
> Which brings me back to the python analogy of algorithms implemented that gradually zoom in on an answer you might view as a local maximum or minimum. It may be that with infinite precision calculations, you might zoom in ever closer to the optimal answer where the tangent to such a curve has slope zero. Your program would never halt though if the condition was that it be exactly at that point to an infinite number of decimal places. This is a place I do not agree that the concept of being near the answer (or in this case being near zero) is not a good enough heuristic solution. There are many iterative problems (and recursive ones) where  a close-enough condition is adequate. Some libraries incorporated into languages like Python use an infinite series to calculate something like sin(x) and many other such things, including potentially e and pi and various roots. Many of them can safely stop after N significant digits are locked into place, and especially when all available signific
>  ant digits are locked. Running them further gains nothing much. So code like:
>
> (previous_estimate - current_estimate) == 0
>
> may be a bad idea compared to something like:
>
> abs(previous_estimate - current_estimate) < epsilon
>
> No disrespect to mathematics intended. My understanding is that mathematics can only be used validly if all underlying axioms are assumed to be true. When (as in the real world or computer programs) some axioms are violated, watch out. Matrix multiplication does not have a symmetry so A*B in general is not the same as B*A and even worse, may be a matrix of a different dimension. A 4x2 matrix and a 2x4 matrix can result in either a 2x2 or 4x4 for example. The violation of that rule may bother some people but is not really an issue as any mathematics that has an axiom for say an abelian group, simply is not expected to apply for a non-abelian case.
>

All of this is true, but utterly irrelevant to floating-point. If your
algorithm is inherently based on repeated estimates (Newton's method,
for instance), then you can iterate until you're "happy enough" with
the result. That's fine. But that is nothing whatsoever to do with the
complaint that 0.1+0.2!=0.3 or that you should "never use == with
floats" or any of those claims. It's as relevant as saying that my
ruler claims to be 30cm long but is actually nearly 310mm long, and
therefore the centimeter is an inherently unreliable unit and anything
measured in it should be treated as an estimate.

ChrisA


More information about the Python-list mailing list