2

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?

1
  • 1
    This is not "unintuitive", this is how numbers are being represented on computers. This wikipedia article on data types may help. Commented Jan 8, 2019 at 10:08

1 Answer 1

5

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.