I'm surprised that
>>> import math
>>> 1**math.nan
1.0
And while we are at it, also that
>>> 0j**math.nan
0j
I didn't find any other examples. Is there a reason or some logic I've missed that makes this the right choice? Or is this a slip?
I was expecting nan
. As for every other number except 1
or 0j
.
Edit 1: Thanks to jedwards's comment below I have a reference. But I still don't understand why. Why was this decided as the standard? Also, couldn't find reference to 0j**mat.nan
...
Edit 2: So following the answers below and some other stuff, the logic may be this one: Any calculation involving nan
should return nan
unless the calculation is always returning the same answer regardless of arguments. In such cases, the fact that we have nan
as an argument should not affect the result and we should still get the fixed answer.
This certainly explains 1**math.nan
and math.nan**0
. This also explains why 0**math.nan
gives nan
and not 0
(since 0**n
is 0
for all but when n=0
where it results with 1
), and might be stretched to cover why math.nan*0
is nan
if we agree that the argument need not be finite.
But if this is the logic behind the scene, then 0j**math.nan
should have been nan
, since 0j**n
is 0
for all n
's except n=0
where 0j**0
is 1
. So... does 0j**math.nan
have different reasoning? or is it a problem in implementation?
Quoting this question which in turns quotes IEEE 754 (see Wikipedia),
The 2008 version of the IEEE 754 standard says that pow(1,qNaN) and pow(qNaN,0) should both return 1 since they return 1 whatever else is used instead of quiet NaN.
For details see page 56 of IEEE 754 2008:
pow(x, ±0) is 1 for any x (even a zero, quiet NaN, or infinity)
pow(±0, y) is ±∞ and signals the divideByZero exception for y an odd
Thus, the reasoning seems to be that no matter what number k is in the exponent, 1^k = 1
, 1^Nan
should also be 1. Why that reasoning is reasonable (I'm sure it is) I'll need to dig further.
Personally, I think this makes sense - Nan
doesn't really exist in math, it's just that our floating point representation can't handle it (or, Nan is "the computation is too much, this is some number but not sure which"). Thus, 1^Nan
could be 1 to an arbitrary power (not 1 to something that is not a number), but since the answer will always be 1, it can only help if we define 1^Nan
to be 1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With