Weird behaviour of np.sqrt for very large integers
Asked Answered
T

1

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

Tallage answered 13/3, 2013 at 16:22 Comment(1)
Learned something new here. Thanks for posting!Penis
A
10

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.

Adventitia answered 13/3, 2013 at 16:26 Comment(6)
But, but, but, that doesn't explain the AttributeError ... How does that accidentally remove sqrt from the numpy namespace? That's gotta be a bug ...Penis
@mgilson: I was getting to that :)Adventitia
(I'm sure this is the reason for the error btw -- But it is a very strange error)Penis
Oh, interesting. And since np.sqrt is a ufunc, a "proper" traceback isn't generated (since the exception is raised in C code).Penis
Actually, I'm using python3 so they're all builtins.intTallage
The error message is better now - TypeError: loop of ufunc does not support argument 0 of type int which has no callable sqrt methodTallage

© 2022 - 2024 — McMap. All rights reserved.