Dear Nmupy developers and users: I am a new user of Numpy ,I have encountered a question about Numpy recently, which need your help. The question is below: I don know why there are negative numbers can somebody explain to me why these happens Thanks in advance!
You are seeing negative numbers because of an overflow beyond 32 bits. Change the type of your np array to int64 using a = (np.arange(2000) ** 3).astype(np.int64) and you won’t see negative numbers. I assume you are running this code on a 32 bit machine which is why Numpy is defaulting to int32. On a 64 bit machine it defaults to int64. Hope this helps.
On Jan 28, 2019, at 11:22 PM, Yuping Wang <wyupinghhu@163.com> wrote:
Dear Nmupy developers and users: I am a new user of Numpy ,I have encountered a question about Numpy recently, which need your help. The question is below:
<捕获.JPG>
I don know why there are negative numbers can somebody explain to me why these happens Thanks in advance! _______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@python.org https://mail.python.org/mailman/listinfo/numpydiscussion
On 29/1/19 9:22 am, Yuping Wang wrote:
Dear Nmupy developers and users: I am a new user of Numpy ,I have encountered a question about Numpy recently, which need your help. The question is below:
I don know why there are negative numbers can somebody explain to me why these happens Thanks in advance!
NumPy ndarrays are typed. Since you do not specify a type in arange, numpy assumes you want `a` to be the type of 2000. I assume you are on a system where numpy converts 2000 to an `int32`, so if you ask what is the type of a: (`a.dtype`) it will show `int32`, meaning the values in a are interpreted as if they are 32 bit integers. 2000*2000*2000 overflows a 32 bit signed integer, so it wraps around to a negative value. The way around this is to specify what type you wish to use: `a=np.arange(2000, dtype=float)**3` or `a=np.arange(2000, dtype='int64`)**3 will not overflow. For floats, most CPUs/compilers provide an indication when float values overflow, so we can raise a warning appropriately. Unfortunately, detecting overflows with ints has no hardware support, so adding a warning would mean checking each value before a calculation, causing an unacceptable slow down. Where did you start learning about NumPy? Perhaps you could suggest they add this explanation (or if it is in our documentation point out where you think would be appropriate) since it seems many people have problems with dtypes and overflow. Matti
On Tue, 29 Jan 2019 10:15:00 +0200, Matti Picus wrote:
2000*2000*2000 overflows a 32 bit signed integer, so it wraps around to a negative value.
I was curious about what happens, bitwise, in this case. We are dealing with a signed integer, so I presume of the 33 bits in 2000**3, only 32 are kept: In [60]: len("{0:b}".format(1999**3)) Out [60]: 33 In [61]: "{0:b}".format(1999**3) Out [61]: '111011100000111110100110001101111' In [62]: "{0:b}".format(1999**3)[32:] Out [62]: '11011100000111110100110001101111' The first of the 32 bits indicates sign, so converting back to integer would give: 1 * np.iinfo(np.int32).min + int('1011100000111110100110001101111', 2) == 601928593 Best regards, Stéfan
On Jan 29, 2019, at 12:15 AM, Matti Picus <matti.picus@gmail.com> wrote:
Perhaps you could suggest they add this explanation (or if it is in our documentation point out where you think would be appropriate) since it seems many people have problems with dtypes and overflow.
arange() is particularly problematic, as it defaults(in the common case) to integers, which are less familiar to new users than float64, which numpy uses as default dtype in most places. And many (most) use of arange() would be better served by lib space anyway. CHB
Matti
_______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@python.org https://mail.python.org/mailman/listinfo/numpydiscussion
participants (5)

Chris Barker  NOAA Federal

Kirit Thadaka

Matti Picus

Stefan van der Walt

Yuping Wang