Skip to content
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

BUG: fix flaky clip with float32 #355

Merged
merged 1 commit into from
Mar 24, 2025
Merged

Conversation

ev-br
Copy link
Member

@ev-br ev-br commented Mar 23, 2025

Testing clip converts array elements to builtin types and uses exact equaity. Converting out from float32 to 64-bit builtin float incurs floating-point errors, and further exact equality testing fails. So use math.isclose instead.

EDIT: does not close gh-339

Testing `clip` converts array elements to builtin types and uses exact equaity.
Converting `out` from float32 to 64-bit builtin float incurs floating-point errors,
and further exact equality testing fails. So use `math.isclose` instead.

closes data-apisgh-339
@crusaderky
Copy link
Contributor

This doesn't look right.

To my understanding, the bug report implies that np.clip(x: float32, min: float64, max: float64) is downgrading min/max to float32, then doing the arithmetics in float32, and finally upgrading everything back to float64, which is definitely wrong.

This PR shouldn't do anything, because in the failure out.dtype=float64.

@ev-br
Copy link
Member Author

ev-br commented Mar 23, 2025

The failure I caught was exactly with out.dtype == float32

@crusaderky
Copy link
Contributor

crusaderky commented Mar 23, 2025

Then this won't close #339, as there the output must have had output dtype=float64:

Draw 4 (kwargs): {'max': np.float64(-16777217.0)}

@ev-br
Copy link
Member Author

ev-br commented Mar 23, 2025

OK, let's keep gh-339 open until somebody can repro

@ev-br ev-br merged commit 6acde75 into data-apis:master Mar 24, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Failing np.clip() on float32
2 participants