MAINT: np.spacing: avoid warnings with NaN input - #32602
Conversation
ikrommyd
left a comment
There was a problem hiding this comment.
This looks good to me. I only have a minor styling comment.
In addition, my AI agent says these nits. I don't know if we wanna do anything about them now
- NaN sign is dropped for float16 only. The half path returns the canonical NaN, while the float, double, and longdouble paths return the input, so a negative NaN keeps its sign there. npy_half_nextafter also canonicalizes, so it is consistent within half, but returning h would make all dtypes match.
- spacing(inf) raises invalid for float16 but not for float32 or float64, so the dtypes disagree on infinity the same way they used to on NaN.
| if (h_exp == 0x7c00u && h_sig != 0) { | ||
| ret = NPY_HALF_NAN; | ||
| } else if (h_exp == 0x7c00u) { | ||
| #if NPY_HALF_GENERATE_INVALID | ||
| npy_set_floatstatus_invalid(); | ||
| #endif |
There was a problem hiding this comment.
I think this reads a little better? (if I formatted it correctly here)
| if (h_exp == 0x7c00u && h_sig != 0) { | |
| ret = NPY_HALF_NAN; | |
| } else if (h_exp == 0x7c00u) { | |
| #if NPY_HALF_GENERATE_INVALID | |
| npy_set_floatstatus_invalid(); | |
| #endif | |
| if (h_exp == 0x7c00u) { | |
| /* NaN passes through quietly; only inf is an invalid operand. */ | |
| if (h_sig == 0) { | |
| #if NPY_HALF_GENERATE_INVALID | |
| npy_set_floatstatus_invalid(); | |
| #endif | |
| } |
|
I think this is an improvement but it needs a release note describing what it does You're changing behavior that was intentionally chosen back in 2010 in 89d8512 for float16. We reverted the |
|
Thanks @ngoldbaum, added the note. Looks like some other |
|
Thanks @mdhaber! |
|
@ikrommyd @ngoldbaum Would you consider a similar PR that avoids the warning for DetailsMy first concern is performance: I'd rather not incur the overhead of I could silence them throughout, but that risks ignoring warnings that should be emitted. I know this has been discussed to death. I'm just trying to guage whether the conclusion these days is that suppression is OK, and it's just a matter of doing the work. |
PR summary
On some platforms, including my Windows machine:
emits
RuntimeWarning: invalid value encountered in spacing. Based on the behavior of similar functions and the comments in the code, I don't think this was intentional. This PR eliminates the inadvertent warning by special-casing NaN input.Additional information
I encountered this while developing SciPy. I thought it would be better to fix the source rather than leaving the
np.errstatein the code indefinitely.There are comments in the tests about similar issues in
ceilandarccos. I set Codex on those, too, but the diffs are a bit larger, so I thought I'd start with this simple one. If you'd be be interested in a PR to fix those, I can update this PR to include those commits, available here:https://github.com/mdhaber/numpy/pull/new/fix_spacing_ceil_arccos_nan_warning
I'd also love it if we could eliminate the warnings I get with
np.log(np.nan + 0j).np.logaddexp(x, y)where eitherxoryis NaN, and similar issues withnp.expI've only seen in SciPy CI.AI Disclosure
OpenAI Codex was used to inspect the relevant implementation and tests, draft
the code changes and regression tests, and prepare this AI disclosure.