Search code examples
pythonarraysnumpyexponent

In Python, why is y**2 sometimes incorrect when y is a numpy array?


First I compute a square in the IDLE shell of Python 3.12.0:

>>> x = 123456
>>> x**2
15241383936

This agrees with my TI-89. Now I compute what I would think would be the same square using numpy arrays:

>>> import numpy as np
>>> y = np.array([x])
>>> ysqr = y**2
array([-1938485248])
>>> ysqr[0]  
-1938485248

What is going on here? Why aren't x**2 and ysqr[0] equal?


A solution: Just to note for those using Python for scientific computing, we can make ysqr[0] equal to x**2 in the above example by changing x to a float:

>>> import numpy as np
>>> y = np.array([float(x)])
>>> ysqr = y**2
array([1.52413839e+10])
>>> ysqr[0]  
15241383936.0

Solution

  • x is of type int, which is unbounded in Python and thus x**2, which is also an int can reach arbitrarily high value.

    However, y is a numpy array with a specific data type. We can observe its data type with print(y.dtype) and see that it's np.int32. Using np.iinfo(np.int32), we can see the following: iinfo(min=-2147483648, max=2147483647, dtype=int32).

    Since 15241383936 is larger than the max value of int32, it overflows and starts counting up from -2147483648. In fact, it is so large, it cycles through to 2147483647 and does this multiple times until it finally lands on -1938485248