Weird behaviour of np.sqrt for very large integers

↘锁芯ラ 提交于 2019-12-08 20:29:23

问题


>>> np.__version__
'1.7.0'
>>> np.sqrt(10000000000000000000)
3162277660.1683793
>>> np.sqrt(100000000000000000000.)
10000000000.0
>>> np.sqrt(100000000000000000000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: sqrt

Huh... AttributeError: sqrt what's going on here then? math.sqrt doesn't seem to have the same problem.


回答1:


The final number is a long (Python's name for an arbitrary precision integer), which NumPy apparently can't deal with:

>>> type(100000000000000000000)
<type 'long'>
>>> type(np.int(100000000000000000000))
<type 'long'>
>>> np.int64(100000000000000000000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C long

The AttributeError occurs because NumPy, seeing a type that it doesn't know how to handle, defaults to calling the sqrt method on the object; but that doesn't exist. So it's not numpy.sqrt that's missing, but long.sqrt.

By contrast, math.sqrt knows about long. If you're going to deal with very large numbers in NumPy, use floats whenever feasible.

EDIT: Alright, you're using Python 3. While the distinction between int and long has disappeared in that version, NumPy is still sensitive to the difference between a PyLongObject that can be successfully converted to a C long using PyLong_AsLong and one that can't.



来源:https://stackoverflow.com/questions/15390858/weird-behaviour-of-np-sqrt-for-very-large-integers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!