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?
>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