Unexpected results in multiplying array by scalar

From the "Just When I Thought I Was Gettin' The Hang Of Numpy" file...

``````>>> import numpy as np
>>> y = np.array((1650, 2300, 2560, 3710)) * 1000000
>>> y
array([ 1650000000, -1994967296, -1734967296,  -584967296])
``````

My old math instructor would agree with the first result, but the others???

FWIW, running Python 3.6.3 on 64-bit Win 10, and also get (as expected)

``````>>> 2300 * 1000000
2300000000
``````
-
I'm not sure why you are getting this result, all my outputs agree with your old math instructor.. Can you provide your run environment? – Yilun Zhang
It's working fine for me. Getting `array([1650000000, 2300000000, 2560000000, 3710000000])` – Jutorres

This is due to the fact that integers have a maximum value of 2^31 - 1 = 2147483647.

Your first value is smaller than that, but the other 3 are larger. Hence you get "looparound". Note that:

``````-1994967296 = -2147483648 + (2300000000 - 2147483647 - 1)
``````

So basically you've gone to the maximum value (2147483647) added 1 to get to the the lowest (-2147483648) and then continued from there.

You can get around this by forcing 64-bit precision

``````>>> import numpy as np
>>> y = np.array((1650, 2300, 2560, 3710), dtype='int64') * 1000000
>>> y
array([1650000000, 2300000000, 2560000000, 3710000000], dtype=int64)
``````
-
It's not happening for me, is this due to python or numpy version issue? – Yilun Zhang
On windows with python 2.7, I get `np.array((1650, 2300, 2560, 3710)).dtype == 'int32'`, i guess you get `'int64'` on your machine? – Jonas Adler
It's possible, getting `int64` on Linux. – Jutorres
@YilunZhang numpy uses C language `long` as default int type. I think OP's machine has `long` only 32-bit. – liliscent
Is the C language "long" changeable from 32-bit to 64-bit? – D Collins
@liliscent that makes sense to me. Thanks for the explanation. – Yilun Zhang

Your problem is caused by 32-bit integer overflow. If you want to handle all big integers, use `dtype=np.object`. Of course there will be some performance penalty:

``````y = np.array((1650, 2300, 2560, 3710),dtype=np.object) * 10000000000000
``````
-
That approach did indeed produce the intended "magnified" array, but unfortunately, as an 'object' dtype (I suppose) I wasn't able to take the log of the array elements. But taking the logs in vectorized fashion (np.log(y)) worked fine when setting up y as dtype=int64. – D Collins
It always depends on your need. `int64` will also overflow, but `object` will never. Try: `np.array((1650, 2300, 2560, 3710),dtype=np.int64) * 100000000000000000`, similar situations to your original question occur because of `int64` overflow. – liliscent

Your values are looping around because they are bigger than the maximum int.

You can use int64 to get more range (will also use more memory):

``````y = np.array((1650, 2300, 2560, 3710), dtype=np.int64)
``````

See all the available types here.

-