Issue 1:
Describe the issue:

We have the following code:

I_wb = I_wb.astype(np.uint16)

I_wb was float32 before the above conversion.

We noticed that when overflow happens, astype could behave differently on different platforms.

as shown in the above image, when running the same code on mac, the value 68469.13 will be truncated to 65535, whereas on linux, the value is applied with a modulo operation . 68469.13 % 65535

on both sides, the code uses numpy 1.21.2

also tried 1.22.0, it's the same.

note: The shape of the data matters,

for example:

I2 = np.zeros((2448, 2048, 3), dtype="float32")
I2[1700,970] = [68469.13, 65472., 65535.]

The above can reproduce, the value 68469.13 gets truncated on certain platforms, but applied with modulo on other platforms.

Whereas the following won't reproduce:

I = np.array([68469.13, 65472., 65535.], dtype="float32")

on both platforms I tried, it behaves the same ( modulo)

could this be due to the underlining cpp compiler and optimization level (clang vs. gcc) ?

Reproduce the code example:

import numpy as np
import sys

print(np.__version__ , sys.version)

I2 = np.zeros((2448, 2048, 3), dtype="float32")
I2[1700,970] = [68469.13, 65472., 65535.]
print("pixel color: ", I2[1700,970])
I2_wb = I2.astype(np.uint16)
print(I2_wb[1700,970])

Error message:

compare the reproduce code on different platforms, notice output difference even with matched numpy version.

NumPy/Python version information:

1.22.0

1.21.2

sample output on my mac (value truncated):

1.21.2 3.9.1 (default, Oct 15 2021, 13:26:04)
[Clang 13.0.0 (clang-1300.0.29.3)]
pixel color: [68469.13 65472. 65535. ]
[65535 65472 65535]

sample output on my linux (modulo)

1.21.2 3.9.7 (default, Sep 10 2021, 14:59:43)
[GCC 11.2.0]
pixel color: [68469.13 65472. 65535. ]
[ 2933 65472 65535]

Issue 2:
Describe the issue:

We have the following code:

I_wb = I_wb.astype(np.uint16)

I_wb was float32 before the above conversion.

We noticed that when overflow happens, astype could behave differently on different platforms.

as shown in the above image, when running the same code on mac, the value 68469.13 will be truncated to 65535, whereas on linux, the value is applied with a modulo operation . 68469.13 % 65535

on both sides, the code uses numpy 1.21.2

also tried 1.22.0, it's the same.

note: The shape of the data matters,

for example:

I2 = np.zeros((2448, 2048, 3), dtype="float32")
I2[1700,970] = [68469.13, 65472., 65535.]

The above can reproduce, the value 68469.13 gets truncated on certain platforms, but applied with modulo on other platforms.

Whereas the following won't reproduce:

I = np.array([68469.13, 65472., 65535.], dtype="float32")

on both platforms I tried, it behaves the same ( modulo)

could this be due to the underlining cpp compiler and optimization level (clang vs. gcc) ?

Reproduce the code example:

import numpy as np
import sys

print(np.__version__ , sys.version)

I2 = np.zeros((2448, 2048, 3), dtype="float32")
I2[1700,970] = [68469.13, 65472., 65535.]
print("pixel color: ", I2[1700,970])
I2_wb = I2.astype(np.uint16)
print(I2_wb[1700,970])

Error message:

compare the reproduce code on different platforms, notice output difference even with matched numpy version.

NumPy/Python version information:

1.22.0

1.21.2

sample output on my mac (value truncated):

1.21.2 3.9.1 (default, Oct 15 2021, 13:26:04)
[Clang 13.0.0 (clang-1300.0.29.3)]
pixel color: [68469.13 65472. 65535. ]
[65535 65472 65535]

sample output on my linux (modulo)

1.21.2 3.9.7 (default, Sep 10 2021, 14:59:43)
[GCC 11.2.0]
pixel color: [68469.13 65472. 65535. ]
[ 2933 65472 65535]