0

I read that comparing two floats using == in python can lead to errors. But what about using that operator in integers ? Thanks

Because I never found anything against the use of the operator I believe that is okay to use.

LeoC
  • 21
  • 2
  • 1
    If you want to check if two ints are exactly equal, `==` is the correct operator. – khelwood Feb 13 '23 at 14:11
  • 1
    It is. Floating-point arithmetic may produce approximate results, which is the root of the problem. Integer arithmetic is exact. – chepner Feb 13 '23 at 14:11
  • https://docs.python.org/2/library/operator.html – Bhargav - Retarded Skills Feb 13 '23 at 14:14
  • 3
    @LeoC "comparing two floats using == in python can lead to errors." --> be wary of such axioms. Although the idea is often a good, exceptions exist. It is understanding when to use and not to use such ideas is part of the learning experience of programming. [A Ship in Harbor Is Safe, But that Is Not What Ships Are Built For](https://quoteinvestigator.com/tag/john-a-shedd/). – chux - Reinstate Monica Feb 13 '23 at 15:12
  • 2
    @chepner Both integer arithmetic and floating point arithmetic are exact, as long as the result of a computation is representable in the target type. If it is not, then both integer arithmetic and floating point arithmetic produce an approximate result. – Sneftel Feb 13 '23 at 15:12
  • Python mandates an arbitrary-precision integer type, so integer arithmetic is required to be exact (though you may not have enough memory to store the result). I should have been clearer regarding floating-point arithmetic; the floating-point type is an approximation of the real numbers, so floating-point arithmetic is an approximation of real arithmetic. – chepner Feb 13 '23 at 15:21
  • 1
    @chepner Sure, my point was that the problem isn't the arithmetic itself, but the set of representable values. Too many programmers think that `2.0 + 3.0` isn't guaranteed to produce exactly 5. And it leads to mushy thinking about epsilons. – Sneftel Feb 13 '23 at 16:23
  • 1
    @chepner: Re “integer arithmetic is required to be exact”: What does `7//3` produce? – Eric Postpischil Feb 13 '23 at 17:41
  • All finite-precision arithmetic has limits, even Python’s so-called arbitrary precision. `7 // 3 * 3` will not equal `7 * 3 // 3`, so you cannot expect that if you “compare integers using `==`”, you will get the same results as you would get with real-number arithmetic if you have done any operations that produce inexact results. This is true for both integer arithmetic and floating-point arithmetic. – Eric Postpischil Feb 13 '23 at 17:43
  • @E There's no reason to expect `n // d * d == n`, because that's not the *definition* of the `//` operator. You *can* expect `n // d * d == n - n % d` to be true for all integers `n` and `d`. This has nothing to do with *precision*. – chepner Feb 13 '23 at 17:55

2 Answers2

0

Its safe to use == operator for integers.

Vishnu Balaji
  • 175
  • 1
  • 11
0

Yes, it's fine. The int type in Python doesn't have the representational problems that float has.

But an interesting bit of trivia is that float types with an integer value are safe to compare too, as long as they're less than 2**53 (9007199254740992).

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • But that little qualifier "as long as they're less than 2**53" (or 2^24 for single precision) is crucial! The second big surprise for beginners to floating point (after the fact that 0.1 is not exactly representable) is that 9007199254740993 is not exactly representable, either, meaning that comparisons for exact equality up in that range can behave unexpectedly, too. – Steve Summit Feb 13 '23 at 14:48
  • ...as discussed in [Why does 9007199254740993 != 9007199254740993.0?](https://stackoverflow.com/questions/31437463) – Steve Summit Feb 13 '23 at 14:56
  • @SteveSummit which is why I was careful to include the qualifier. I could have gone further with it, since all the *even* numbers between `2**53` and `2**54` are also exactly representable. – Mark Ransom Feb 13 '23 at 17:50
  • Whether exactly equal results will be obtained (if and only if real-number arithmetic would produce exactly equal results) depends whether the ideal results of all intermediate and final operations were representable, not on the range of the final values being compared. This is true for integer arithmetic and floating-point. – Eric Postpischil Feb 13 '23 at 17:57