May someone please explain me why np.array([1e5])**2 is not the equivalent of np.array([100000])**2? Coming from Matlab, I found it confusing!

>>> np.array([1e5])**2
array([  1.00000000e+10])   # correct

>>> np.array([100000])**2
array([1410065408])         # Why??

I found that this behaviour starts from 1e5, as the below code is giving the right result:

>>> np.array([1e4])**2
array([  1.00000000e+08])   # correct

>>> np.array([10000])**2
array([100000000])          # and still correct
有帮助吗?

解决方案

1e5 is a floating point number, but 10000 is an integer:

In [1]: import numpy as np

In [2]: np.array([1e5]).dtype
Out[2]: dtype('float64')

In [3]: np.array([10000]).dtype
Out[3]: dtype('int64')

But in numpy, integers have a fixed width (as opposed to python itself in which they are arbitrary length numbers), so they "roll over" when they get larger than the maximum allowed value.

(Note that in your case you are using a 32-bit build, so in fact the latter would give you dtype('int32'), which has a maximum value 2**32-1=2,147,483,647, roughly 2e9, which is less than 1e10.)

其他提示

You're system is defaulting to np.int32, which can't handle 100000**2. If you use 64-bit precision, you'll be fine:

In [6]: np.array([100000], dtype=np.int32)**2
Out[6]: array([1410065408], dtype=int32)

In [7]: np.array([100000], dtype=np.int64)**2
Out[7]: array([10000000000])

What the default is (32 vs 64) depends on your numpy build.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top