Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why np.array([1e5])**2 is different from np.array([100000])**2 in Python?

May someone please explain me why np.array([1e5])**2 is not the equivalent of np.array([100000])**2? Coming from Matlab, I found it confusing!

>>> np.array([1e5])**2
array([  1.00000000e+10])   # correct

>>> np.array([100000])**2
array([1410065408])         # Why??

I found that this behaviour starts from 1e5, as the below code is giving the right result:

>>> np.array([1e4])**2
array([  1.00000000e+08])   # correct

>>> np.array([10000])**2
array([100000000])          # and still correct
like image 997
Amin Avatar asked Mar 14 '14 16:03

Amin


2 Answers

1e5 is a floating point number, but 10000 is an integer:

In [1]: import numpy as np

In [2]: np.array([1e5]).dtype
Out[2]: dtype('float64')

In [3]: np.array([10000]).dtype
Out[3]: dtype('int64')

But in numpy, integers have a fixed width (as opposed to python itself in which they are arbitrary length numbers), so they "roll over" when they get larger than the maximum allowed value.

(Note that in your case you are using a 32-bit build, so in fact the latter would give you dtype('int32'), which has a maximum value 2**32-1=2,147,483,647, roughly 2e9, which is less than 1e10.)

like image 110
Andrew Jaffe Avatar answered Sep 29 '22 10:09

Andrew Jaffe


You're system is defaulting to np.int32, which can't handle 100000**2. If you use 64-bit precision, you'll be fine:

In [6]: np.array([100000], dtype=np.int32)**2
Out[6]: array([1410065408], dtype=int32)

In [7]: np.array([100000], dtype=np.int64)**2
Out[7]: array([10000000000])

What the default is (32 vs 64) depends on your numpy build.

like image 26
JoshAdel Avatar answered Sep 29 '22 09:09

JoshAdel