16

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:

  • Is this expected behaviour?
  • Why is it different for the two platforms for this one case?
2
  • That sounds like a bug. It should be np.int8 on Windows. Commented Dec 6, 2017 at 19:13
  • Somehow, numpy.can_cast(127, numpy.int8, casting='safe') returns False on Windows. Commented Dec 6, 2017 at 19:15

1 Answer 1

18

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.