BUG: Fix np.abs(nan) returning negative NaN on wasm32/Pyodide - #31433
Conversation
|
I want to know why adding 0.0 corrupted the nan sign, any ideas? Was it possible that the nan was already negative? |
|
HI @charris , The bug occurs because
So |
I noticed the problem on WASM with |
|
|
|
Thank you for the feedback! I have amended the PR to fix float32 and float64 as well.they had the same |
|
HI @seberg , |
| assert_(np.signbit(np.nan) == 0) | ||
| def test_abs_nan_signbit(): | ||
| """#31421 abs(nan) preserves positive sign bit correctly.""" | ||
| for dtype in [np.float16, np.float32, np.float64, np.longdouble]: |
There was a problem hiding this comment.
could parametrize for better feedback, although array test below would then make sense to split off
There was a problem hiding this comment.
Thanks for the feedback ..Test case has been updated accordingly ,I have parametrized the test for better feedback, split the array test into a separate function.
seberg
left a comment
There was a problem hiding this comment.
Thanks, LGTM, let's put it in, since C99 guarantees abs being defined.
I am not sure that C99 actually guarantees the sign of NaN (or that we should), but in practice I don't see anything plausibly diverging.
Fix: #31421
PR summary
This PR fixes a bug where np.signbit(np.abs(np.nan)) incorrectly returns True instead of False.
problem
The
LONGDOUBLE_absolutefunction innumpy/_core/src/umath/loops.c.srcusedtmp + 0 to clear -0.0,but this operation corrupted the sign bit ofNaNvalues, causing np.signbit(np.abs(np.nan)) to return True.Testing
Manually run the new test case and run all the
numpy/_core/tests/test_umath.pyfile test cases.