I’m slightly disappointed that np.inf // 2
evaluates to np.nan
and not to np.inf
, as is the case for normal division.
Is there a reason I’m missing why nan
is a better choice than inf
?
I’m slightly disappointed that np.inf // 2
evaluates to np.nan
and not to np.inf
, as is the case for normal division.
Is there a reason I’m missing why nan
is a better choice than inf
?
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
*mod
in div = (vx - *mod) / wx;
is replaced by the part after the equal mark above it ? –
Sipper 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.
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
© 2022 - 2024 — McMap. All rights reserved.
floor_divide
is more efficient than doing both operations separately. – Muffininf
would be an incorrect result of integer division becauseinf
is not an integer. Nownan
isn't an integer either, but at least it somehow expresses the fact that there is no correct answer to the question that was asked, i.e. there is no integerx
such thatx*2
equalsinf
. That's my take on it anyway. – Eyednan
was considered a better choice thaninf
? – Mellienp.floor(np.inf)
resulting innp.inf
a correct result? You could claim there is no correct integer answer to this question as well. – Melliefloat('inf')*2
returnsinf
. – Muffinfloat('inf')
isn't an integer – Naldafloor(x)
always differs fromx
by some finite value between 0 and 1, and the result of that subtraction would beInf
(according to IEEE semantics) no matter what value in that range was chosen. – Hospitalerfloor(inf) == inf
andfloor(-inf) == -inf
. And indeed POSIXfloor
does give that result. – Hospitaler