Search code examples
pythonnumpyelementwise-operations

Numpy elementwise multiplication (unexpected integer overflow)


I'm using Python 3.7 and numpy 1.15.2 and have encountered a behavior in elementwise multiplication that I don't understand. The following is intuitive to me:

import numpy as np
a = np.array([[30000,4000]])
b = np.array([[70000,8000]])
np.multiply(a,b)

gives

array([[2100000000,32000000]])

However, when I do

a = np.array([[30000,40000]])
b = np.array([[70000,80000]])
np.multiply(a,b)

I get

array([[ 2100000000, -1094967296]])

I would have guessed that the result should be array([[ 30000*70000, 40000*80000]]). Where does the negative number come from? And what should I do to get the expected array?


Solution

  • It looks like numpy by default interprets plain numbers as np.int32 (which has a range from -231 ... 231 - 1), which will overflow with 40000*80000, because 3200000000 > 2**31 - 1 (= 2147483647):

    import numpy as np
    
    a = np.array([[30000,40000]])
    b = np.array([[70000,80000]])
    np.multiply(a,b)
    Out: array([[ 2100000000, -1094967296]])
    
    type(a[0][0])
    Out: numpy.int32
    

    You can solve this by explicitely setting a better suited data type:

    a = np.array([[30000,40000]], dtype=np.int64)
    b = np.array([[70000,80000]], dtype=np.int64)
    np.multiply(a,b)
    Out: array([[2100000000, 3200000000]], dtype=int64)
    

    or

    a = np.array([[30000,40000]], dtype=np.uint32)
    b = np.array([[70000,80000]], dtype=np.uint32)
    np.multiply(a,b)
    Out: array([[2100000000, 3200000000]], dtype=uint32)