Why can't numpy compute long objects?

Say I have a variable that is assigned the type 'long'

x = 40*2*10**30 

If then I try to take the log of this variable using numpy (imported as np):

 np.log10(x) 

I encountered an attribute error:

The 'long' object does not have the attribute 'log10'.

To get around this, I can make the variable float, and it works fine or uses the "math" package:

 math.log10(x) np.log10(float(x)) 

My question is: how are math.log10 and np.log10 different, and why is np not configured to handle long types?

+5
source share
1 answer

The problem is that numpy is written in C and it does not have a data type that can handle a number, the same as a regular python int class. If you go here: http://docs.scipy.org/doc/numpy/user/basics.types.html , it explains the different data types allowed by numpy. Pay particular attention to the int64 type, the largest numbers allowed in this type are much smaller than the integer that you have. However, float64 (which is equivalent to double in C) can handle up to an 11-bit metric, so there is no overflow error when converting to float.

+6
source

All Articles