LRP
LRP

Reputation: 118

why can't numpy compute long objects?

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

x = 40*2*10**30

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

np.log10(x)

I am faced with an attribute error:

'long' object has no attribute 'log10'.

To get around this I can make the variable a float and it works just fine or use the 'math' package:

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

My question is: how does math.log10 and np.log10 differ and why is np not set up to handle 'long' types?

Upvotes: 7

Views: 1746

Answers (1)

jfish003
jfish003

Reputation: 1332

The problem is that numpy is written in C and it does not have a data type that can handle a number as large as the 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 in numpy. Take special note of the int64 dtype, the largest numbers allowed in that type are much smaller than the integer number that you have input. However float64 (which is equivalent to double in C) can handle up to an 11 bit exponent, which is why when you convert to float there is no overflow error.

Upvotes: 8

Related Questions