Re: [Numpy-discussion] NumPy-Discussion Digest, Vol 148, Issue 27
I start to learn python from 《Numpy Beginner's Guide》second edition written by Ivan Idris now I know what the problem is ,the reason is that Numpy converts 2000 to an `int32`.When I use code `a=np.arange(2000, dtype='int64')**3 that you taught me, the calculation is correct thank you all ! Yuping Wang___ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Numpy-discussion
> On Jan 29, 2019, at 12:15 AM, Matti Picus 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 > > ___ > NumPy-Discussion mailing list > NumPy-Discussion@python.org > https://mail.python.org/mailman/listinfo/numpy-discussion ___ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] negative numbers
I'm not sure which numpy version you're using (I don't get that using numpy 1.14.3), but the issue is that for some reason the data type (dtype) of the array is being assumed to be a 4 byte integer or ' wrote: > From: Yuping Wang > To: numpy-discussion@python.org > Cc: > Bcc: > Date: Tue, 29 Jan 2019 15:22:00 +0800 (CST) > Subject: [Numpy-discussion] Numpy-discussion > 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-Discussion mailing list > NumPy-Discussion@python.org > https://mail.python.org/mailman/listinfo/numpy-discussion > -- Jonathan D. Slavin Astrophysicist - High Energy Astrophysics Division Center for Astrophysics | Harvard & Smithsonian Office: (617) 496-7981 | Cell: (781) 363-0035 60 Garden Street | MS 83 | Cambridge, MA 02138 ___ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Numpy-discussion
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, bit-wise, 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]: '111011101010011000110' In [62]: "{0:b}".format(1999**3)[-32:] Out [62]: '11011101010011000110' The first of the 32 bits indicates sign, so converting back to integer would give: 1 * np.iinfo(np.int32).min + int('1011101010011000110', 2) == -601928593 Best regards, Stéfan ___ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Numpy-discussion
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 ___ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Numpy-discussion
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 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! > ___ > NumPy-Discussion mailing list > NumPy-Discussion@python.org > https://mail.python.org/mailman/listinfo/numpy-discussion ___ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion