2

numpy's floor division seems counterintuitive when dealing with np.inf. I feel like the answer should be same as true division in these examples

>>> -7 // np.inf
-1.0

>>> -7 / np.inf
-0.0

>>> np.inf // 7
nan

>>> np.inf / 7
inf

In my head, an integer divided by infinity should approach 0 and infinity divided by any integer should remain +/- infinity. Can anyone provide some insight here? Thanks!

I am running version 1.22.2 in case that's relevant

  • Not just NumPy, I get the same results with `math.inf`. – Kelly Bundy Jul 26 '22 at 22:06
  • From the [floor division link](https://numpy.org/doc/stable/reference/generated/numpy.true_divide.html) you provided: "Return the largest integer smaller or equal to the division of the inputs." This is very clear, -1 < -7 ÷ ∞ < 0, therefore the largest integer which is less than or equal to the result is -1. For `np.inf // 7`: the result isn't an integer so floor division is undefined and it looks like they decided to convey this to the user by evaluating it as `nan`. – Michael Ruth Jul 26 '22 at 22:12
  • 1
    @MichaelRuth What makes you say -7 ÷ ∞ is less than 0? [WolframAlpha](https://www.wolframalpha.com/input?i=-7+%C3%B7+%E2%88%9E) for example says is **is** 0. – Kelly Bundy Jul 26 '22 at 22:31
  • @KellyBundy, I probably should have stated it as `-x ÷ y approaches 0 from the negative side as y approaches infinity`. I bet they implemented floor division this way in order to avoid some sort of local continuity issue. – Michael Ruth Jul 26 '22 at 22:59
  • This makes a lot of sense! Thanks @MichaelRuth – pierce314159 Jul 27 '22 at 16:43

0 Answers0