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

Failing np.clip() on float32 #339

Open
crusaderky opened this issue Jan 30, 2025 · 2 comments · Fixed by #355
Open

Failing np.clip() on float32 #339

crusaderky opened this issue Jan 30, 2025 · 2 comments · Fixed by #355

Comments

@crusaderky
Copy link
Contributor

e.g. https://github.com/data-apis/array-api-compat/actions/runs/13060007276/job/36440413878?pr=247

FAILED array_api_tests/test_operators_and_elementwise_functions.py::test_clip - AssertionError: out[()]=-16777216.0 but should be -16777217.0 [clip()]
  x[()]=0.0, min[()]=None, max[()]=-16777217.0
assert -16777216.0 == np.float64(-16777217.0)
Falsifying example: test_clip(
    x=array(0., dtype=float32),
    data=data(...),
)
Draw 1 (min.shape, max.shape): ((), ())
Draw 2 (min): None
Draw 3 (max): np.float64(-16777217.0)
Draw 4 (kwargs): {'max': np.float64(-16777217.0)}

The problem is somehow caused by the fact that np.float32(-16777217.0) == -16777216.0.
However I can't reproduce the error on numpy 2.0.2:

>>> np.clip(np.asarray(0, dtype=np.float32), None, -16777217.0)
np.float32(-16777216.0)  # Correctly follows rule about binops mixing pure python and arrays

>>> np.clip(np.asarray(0, dtype=np.float32), None, np.float32(-16777217.0))
np.float32(-16777216.0)

>>> np.clip(np.asarray(0, dtype=np.float32), None, np.float64(-16777217.0))
np.float64(-16777217.0)
@ev-br ev-br marked this as a duplicate of #352 Mar 20, 2025
ev-br added a commit to ev-br/array-api-tests that referenced this issue 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.

closes data-apisgh-339
@ev-br
Copy link
Member

ev-br commented Mar 23, 2025

managed to catch it on numpy 2.2. A fix in gh-355

@ev-br ev-br closed this as completed in cff3431 Mar 24, 2025
@ev-br ev-br reopened this Mar 24, 2025
@ev-br
Copy link
Member

ev-br commented Mar 24, 2025

Reopening: as discussed in gh-355, that fix may fix a different issue. So let's keep this open until we can repro and fix this specific issue.

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 a pull request may close this issue.

2 participants