-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
Inconsistency: NaN ** 0.0 = 1.0, but NaN * 0.0 = NaN #133274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Can someone reformat the post to use codeblocks please. cc @skirpichev edit: It's not a bug, I read the messy post wrong, this should be closed (It is well documented :-)
|
This won't be changed - it's required behavior by various standards. And it's not alone: >>> math.hypot(math.inf, math.nan)
inf
>>> math.hypot(math.nan, math.inf)
inf
>>> pow(math.nan, 0.)
1.0
>>> pow(1., math.nan)
1.0
That's from a public post by an IEEE committee member here. You're not required to like it, but, as that post says,
One side eventually "won", and the "always propagate NaNs" side lost. They already debated it beyond death, and won't revisit it. In brief, I also happen to agree with the post's close:
|
It's is documented, indeed. But, maybe, not well. Here is my hubly attempt to read it. Section 6.2 says:
We also have rules for conversion:
So, we reasonable could conclude, that In turn, the math.pow() docs indeed says something relevant:
Also, difference wrt builtin pow() is noted:
Edit: and pow() and math.pow() aren't actually equal wrt exception handling or complex results: >>> import math
>>> pow(0.0, -1.0)
Traceback (most recent call last):
File "<python-input-1>", line 1, in <module>
pow(0.0, -1.0)
~~~^^^^^^^^^^^
ZeroDivisionError: 0.0 cannot be raised to a negative power
>>> math.pow(0.0, -1.0)
Traceback (most recent call last):
File "<python-input-2>", line 1, in <module>
math.pow(0.0, -1.0)
~~~~~~~~^^^^^^^^^^^
ValueError: math domain error
>>> pow(-1.0, 0.5)
(6.123233995736766e-17+1j)
>>> math.pow(-1.0, 0.5)
Traceback (most recent call last):
File "<python-input-2>", line 1, in <module>
math.pow(-1.0, 0.5)
~~~~~~~~^^^^^^^^^^^
ValueError: math domain error IMO, it might be not something obvious for newbie to search details of Looking more in code, I found that |
Bug report
Bug description:
As you can see, the behavior of ** is inconsistent with that of * and +. ** treats NaN like a normal number,
so NaN ** 0.0 = 1.0. But other operations like * and + treat NaN like a special value, since it is not a number.
Therefore, the result of operations like NaN * 0.0 are NaN.
"""
CPython versions tested on:
3.13, 3.11
Operating systems tested on:
Windows
The text was updated successfully, but these errors were encountered: