Skip to content

MAINT: silence invalid warning for np.logaddexp(2) with NaN input - #32733

Merged
ngoldbaum merged 2 commits into
numpy:mainfrom
mdhaber:fix-logaddexp-nan-warnings
Sep 22, 2026
Merged

ngoldbaum merged 2 commits into
numpy:mainfrom
mdhaber:fix-logaddexp-nan-warnings

Conversation

@mdhaber

@mdhaber mdhaber commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

PR summary

On (at least?) some platforms, including my Windows machine, np.logaddexp/np.logaddexp2 with NaN input produce invalid warnings.

import numpy as np
np.logaddexp(np.nan, np.nan)  # propagates NaN, but warns: 
# RuntimeWarning: invalid value encountered in logaddexp

Based on the behavior of other ufuncs and other precedent (e.g. gh-12236, gh-15988, gh-32602, and now gh-32706), 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.

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.

@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.

Just some nitpicks in the C code and about the release note. Otherwise this is consistent with your other recent work we've merged.

Comment thread doc/release/upcoming_changes/32733.improvement.rst
else {
const @type@ tmp = x - y;
if (tmp > 0) {
if (isgreater(tmp, 0)) {

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.

This should be e.g. (@type@)0 instead of just 0. Also the other constants below. You could define e.g. const @type@ ZERO = 0.0; to make this a little nicer.

These functions accept floating point arguments and it's technically a violation of the C11 spec to pass an integer constant here. clang and gcc accept them anyway but let's not intentionally add code that violates the standard just because clang and gcc are OK with it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I'll fix that.

@ngoldbaum

Copy link
Copy Markdown
Member

Thanks @mdhaber!

@ngoldbaum
ngoldbaum merged commit 698f04f into numpy:main Sep 22, 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.

2 participants