NOhs
NOhs

Reputation: 2830

Multiplying a np.int8 array with 127 yields different numpy array types depending on platform

The following code:

>>> import numpy as np
>>> np.arange(2).astype(np.int8) * 127

produces for numpy 1.13.3

# On Windows
array([0, 127], dtype=int16)
# On Linux
array([0, 127], dtype=int8)

However, if I change the 127 to a 126, both return a np.int8 array. And if I change the 127 to a 128 both return a np.int16 array.

Questions:

Upvotes: 16

Views: 713

Answers (1)

user2357112
user2357112

Reputation: 281683

This is due to NumPy issue 5917. A < instead of <= caused np.can_cast(127, np.int8) to be False, so NumPy used a too-large dtype for 127. The OS-dependence is because C longs have a different size on Linux and Windows, and some NumPy code paths depend on the size of a C long.

A fix has been released in NumPy 1.14.0. Once you update to at least NumPy 1.14.0, you should see a dtype of int8 on all platforms.

Upvotes: 18

Related Questions