Skip to content

MAINT: silence invalid warning for np.log(complex(np.nan, np.nan)) and such - #32706

Merged
ngoldbaum merged 5 commits into
numpy:mainfrom
mdhaber:fix-clog-nan-warnings
Sep 21, 2026
Merged

ngoldbaum merged 5 commits into
numpy:mainfrom
mdhaber:fix-clog-nan-warnings

Conversation

@mdhaber

@mdhaber mdhaber commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

PR summary

On (at least?) some platforms, including my Windows machine, complex logarithms with NaN input and NaN output produce invalid warnings (RuntimeWarning: invalid value encountered in log)

import numpy as np
np.log(np.nan)  # no warning
np.log(np.nan + 0j)  # warning

Based on the behavior of other elementary functions (e.g. np.sin) and other precedent (e.g. gh-12236, gh-15988, gh-32602), I don't think this is desired anymore. This PR eliminates the inadvertent warning by using floating point exception-quiet comparison implementations from the C standard library.

Additional information

I encountered this while developing SciPy. I thought it would be better to fix the source rather than leaving the np.errstate in the code indefinitely.

This does not adjust the cases in which there is an infinite non-NaN component. Those produce np.complex128(inf+nanj). In most cases, this is prescribed by the array API, but it seems a little less clear-cut whether the warning is desirable.

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.

Comment thread numpy/_core/tests/test_umath_complex.py Outdated
Comment thread doc/release/upcoming_changes/32706.improvement.rst
Comment thread numpy/_core/src/npymath/npy_math_complex.c.src Outdated
@ikrommyd ikrommyd changed the title Silence invalid warning for np.log(complex(np.nan, np.nan)) and such MAINT: silence invalid warning for np.log(complex(np.nan, np.nan)) and such Sep 20, 2026
@mdhaber

mdhaber commented Sep 21, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @seberg, looks like that did the trick.

@seberg

seberg commented Sep 21, 2026

Copy link
Copy Markdown
Member

Yap. In the past that didn't quite always do the trick. I think because SSE3.something didn't have quiet comparisons or so. I think that is now below our default baseline and times passed, so happy to give it a shot and see...

@ngoldbaum ngoldbaum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Matt. Feel free to send in followups for other spots like this you're aware of where we're over-zealous in emitting warnings.

@ngoldbaum
ngoldbaum merged commit a5028e8 into numpy:main Sep 21, 2026
91 checks passed
Riaz1729 pushed a commit to Riaz1729/numpy that referenced this pull request Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants