How come the following test works fine on Windows, but fails on Linux:
import numpy as np
print(f"Numpy Version: {np.__version__}")
# version A
versionA = np.eye(4)
versionA[:3, 3] = 2**32 + 1
versionA = versionA.astype(np.uint32)
# version B
versionB = np.eye(4, dtype=np.uint32)
versionB[:3, 3] = np.asarray(2**32 + 1)
# # version C
# # (raises OverflowError)
# versionC = np.eye(4, dtype=np.uint32)
# versionC[:3, 3] = 2**32 + 1
np.testing.assert_array_equal(versionA, versionB)
I tested this on Windows and Linux with numpy versions:
1.23.4
, 1.21.5
, 1.24.0
. On Windows, the assignment overflows to 1
in both versions and the assertion compares as equal. On Linux, on the other hand, versionB
overflows to 1
, but versionA
results in assigning 0
. As a result, I get the following failure:
AssertionError:
Arrays are not equal
Mismatched elements: 3 / 16 (18.8%)
Max absolute difference: 4294967295
Max relative difference: 4.2949673e+09
x: array([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]], dtype=uint32)
y: array([[1, 0, 0, 1],
[0, 1, 0, 1],
[0, 0, 1, 1],
[0, 0, 0, 1]], dtype=uint32)
Can someone explain why numpy behaves this way?