Skip to content

Failing np.clip() on float32 #339

Closed
@crusaderky

Description

@crusaderky

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions