Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 868
LRP Avatar asked Jul 24 '15 14:07

LRP


People also ask

What is the range of unit 8 data type in NumPy?

So, what I mean by this is that if your working with a number range from 100 to 200 you could use the Unit8 data type because it covers all the integers from 0 to 255.

What does size attribute in NumPy use to find?

size: This attribute calculates the total number of elements present in the NumPy array.

What is long object in Python?

In Python 2, an int is a C long, and a long is like BigInterger in Java, it's a type that can hold any size of integer (it allocates memory if it's too big) – Cilyan.

What is NumPy uint8?

Character code 'B' Alias on this platform (Linux x86_64) numpy. uint8: 8-bit unsigned integer (0 to 255). Most often this is used for arrays representing images, with the 3 color channels having small integer values (0 to 255). Follow this answer to receive notifications. answered Jul 15, 2021 at 2:41.


1 Answers

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.

like image 191
jfish003 Avatar answered Oct 20 '22 18:10

jfish003