Why does “np.inf // 2” result in NaN and not infinity?

Floor division is defined in relation to modulo, both forming one part of the divmod operation.

Binary arithmetic operations

The floor division and modulo operators are connected by the following identity: x == (x//y)*y + (x%y). Floor division and modulo are also connected with the built-in function divmod(): divmod(x, y) == (x//y, x%y).

This equivalence cannot hold for x = inf — the remainder inf % y is undefined — making inf // y ambiguous. This means nan is at least as good a result as inf. For simplicity, CPython actually only implements divmod and derives both // and % by dropping a part of the result — this means // inherits nan from divmod.


I'm going to be the person who just points at the C level implementation without any attempt to explain intent or justification:

*mod = fmod(vx, wx);
div = (vx - *mod) / wx;

It looks like in order to calculate divmod for floats (which is called when you just do floor division) it first calculates the modulus and float('inf') %2 only makes sense to be NaN, so when it calculates vx - mod it ends up with NaN so everything propagates nan the rest of the way.

So in short, since the implementation of floor division uses modulus in the calculation and that is NaN, the result for floor division also ends up NaN


Infinity is not a number. For example, you can't even say that infinity - infinity is zero. So you're going to run into limitations like this because NumPy is a numerical math package. I suggest using a symbolic math package like SymPy which can handle many different expressions using infinity:

import sympy as sp

sp.floor(sp.oo/2)
sp.oo - 1
sp.oo + sp.oo