Weird behaviour of np.sqrt for very large integers

2019-06-17 06:54发布

问题:

>>> 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.