The following code throws me the error:
Traceback (most recent call last):
File \"\", line 25, in
sol = anna.main()
File \"\", line 17, in
I'll try to add a precise answer to those that have already been given. numpy.sqrt
has some limitations that math.sqrt
doesn't have.
import math
import numpy # version 1.13.3
print(math.sqrt(2 ** 64 - 1))
print(numpy.sqrt(2 ** 64 - 1))
print(math.sqrt(2 ** 64))
print(numpy.sqrt(2 ** 64))
returns (with Python 3.5) :
4294967296.0
4294967296.0
4294967296.0
Traceback (most recent call last):
File "main.py", line 8, in
print(numpy.sqrt(2 ** 64))
AttributeError: 'int' object has no attribute 'sqrt'
In fact, 2 ** 64
is equal to 18,446,744,073,709,551,616
and, according to the standard of C data types (version C99), the long long unsigned integer
type contains at least the range between 0
and 18,446,744,073,709,551,615
included.
The AttributeError
occurs because numpy
, seeing a type that it doesn't know how to handle (after conversion to C data type), defaults to calling the sqrt
method on the object (but that doesn't exist). If we use floats instead of integers then everything will work using numpy
:
import numpy # version 1.13.3
print(numpy.sqrt(float(2 ** 64)))
returns:
4294967296.0
So instead of replacing numpy.sqrt
by math.sqrt
, you can alternatively replace calc = np.sqrt(food ** 5)
by calc = np.sqrt(float(food ** 5))
in your code.
I hope this error will make more sense to you now.