Python3:RuntimeWarning with numpy.power

Python3:RuntimeWarning with numpy.power,python,python-3.x,numpy,integer,pow,Python,Python 3.x,Numpy,Integer,Pow,使用numpy.power(2,N)时,如果N是整数,我会遇到以下问题: In[1] np.power(2,63) Out[1] -9223372036854775808 RuntimeWarning: invalid value encountered in power 更奇怪的是 In[2] np.power(2,63)*2 Out[2] 0 所有大于或等于63的指数都会发生这种情况。我认为大整数在Python中不是问题-那么这里有什么问题?大整数在Python中不是问题,因为Pytho

使用
numpy.power(2,N)
时,如果
N
是整数,我会遇到以下问题:

In[1] np.power(2,63)
Out[1] -9223372036854775808
RuntimeWarning: invalid value encountered in power
更奇怪的是

In[2] np.power(2,63)*2
Out[2] 0

所有大于或等于63的指数都会发生这种情况。我认为大整数在Python中不是问题-那么这里有什么问题?

大整数在Python中不是问题,因为Python只有一种整数类型,并且具有任意精度。但这些数据的精确度有限:

>>> 2 ** 63              # Python
9223372036854775808

>>> np.int64(2) ** 63    # NumPy
-9223372036854775808

在大多数系统上,64位是纯NumPy可用的最高精度整数类型。因此,如果您处理的是较大的数字,您可以使用
float
d类型,或者简单地将Python整数与普通列表或NumPy对象数组一起使用(不推荐)。

这里不处理Python整数,而是处理固定大小的NumPy整数(本例中为64位),这是NumPy提供的快速矢量化数学运算的先决条件。如果您想使用Python ints,请编写
2**63
pow(2,63)