BUG: numpy int64/float64 comparisons are inexact
Describe the issue:
Python guarantees that a == b implies hash(a) == hash(b). Around 2**53 this breaks for numpy scalars: == promotes the int64 to float64 (losing precision) so the values compare equal, but each hashes as its own exact value, so the hashes differ.
In the bigger picture, it is also not clear whether np.int64(9007199254740993) == np.float64(9007199254740992.0) should be True in the first place, but I would consider this a separate issue.
Reproduce the code example:
# /// script
# requires-python = ">=3.10"
# dependencies = ["numpy"]
# ///
import numpy as np
a = np.int64(2**53 + 1) # 9007199254740993 (exact as int64)
b = np.float64(2**53 + 1) # 9007199254740992.0 (2**53 + 1 is not representable in float64)
print(f"numpy {np.__version__}")
print(f"a = {a!r}")
print(f"b = {b!r}")
print(f"a == b -> {bool(a == b)}")
print(f"hash(a) == hash(b) -> {hash(a) == hash(b)}")Error message:
a = np.int64(9007199254740993)
b = np.float64(9007199254740992.0)
a == b -> True
hash(a) == hash(b) -> FalsePython and NumPy Versions:
numpy: 2.5.1 python: 3.12.11
Runtime Environment:
No response
How does this issue affect you or how did you find it:
Found during fixing a hashing related bug in zarr-python (https://github.com/zarr-developers/zarr-python). While fixing, I explored how numpy handles equality tests and hashing of scalar values. np.generic values are part of a bigger structure we need to hash.
Source: numpy/numpy