Reputation: 157
When I execute the below on Ipython:
test = np.array([1,2,3,4])
test**50
it returns:
array([ 1, -2147483648, -2147483648, -2147483648])
which has both the wrong value and sign. Any clues why I might be getting this?
Upvotes: 2
Views: 1290
Reputation: 23637
As mentioned in the comments, this happens because the integer data type overflows. Numpy initializes the array with a low level int
data type because that fits the data you provided.
test = np.array([1,2,3,4])
test.dtype
# dtype('int32')
test[0] = 2**31 - 1 # works
test[0] = 2**31 # OverflowError: Python int too large to convert to C long
A 32 bit signed integer is used (on my system), which can hold values between -2147483648 and 2147483647.
You can force the array to have a different data type, e.g. floating point:
test = np.array([1, 2, 3, 4], dtype=float)
# test = np.array([1.0, 2.0, 3.0, 4.0]) # this is the same
test**50
# array([ 1.00000000e+00, 1.12589991e+15, 7.17897988e+23, 1.26765060e+30])
Here is a list of data types that can be passed as strings to the dtype
argument.
If you want Python's large integers instead of floating point precision, this works too (performance warning):
test = np.array([1,2,3,4], dtype=object)
test**50
# array([1, 1125899906842624, 717897987691852588770249,
# 1267650600228229401496703205376], dtype=object)
Upvotes: 2