2

From the following code:

import numpy as np

print 2**np.array([32, 33])

I get the negative numbers [-2147483648 -2147483648]. I'm using python 2.6. Is it some sort of bug? On the contrary print 2**32 gives the correct answer 4294967296.

user2983638
  • 903
  • 1
  • 13
  • 20
  • Are you on Windows? – ayhan Jan 21 '17 at 13:46
  • Yes, I'm on Windows, but I'm not sure it is overflow. When I write print 2**32 I get 4294967296 – user2983638 Jan 21 '17 at 13:47
  • May I ask why a bug in a built-in operator was your first guess? – TigerhawkT3 Jan 21 '17 at 13:49
  • 4
    Doing plain `2**32` gives the right result because it's using Python integers, which can grow arbitrarily large (although in Python 2 integers that are larger than the CPU native integer size will be `long` rather than `int`) . But Numpy is designed to use fixed sized numbers, not Python integers. – PM 2Ring Jan 21 '17 at 13:50
  • @MYGz Even with 64-bit Windows that can be an issue: http://stackoverflow.com/a/36279549/2285236 – ayhan Jan 21 '17 at 13:54

2 Answers2

8

These values are too big to store in a 32-bit int which numpy uses by default. If you set the datatype to float (or 64-bit int) you get the proper results:

import numpy as np

print 2 ** np.array([32, 33], dtype=np.float)
# [  4.2946730e+09  8.58993459e+09  ]

print 2 ** np.array([32, 33], dtype=np.int64) # 64-bit int as suggested by PM 2Ring
# [  4294967296  8589934592]
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • 4
    Or if you want exact integer results use `dtype=np.int64` – PM 2Ring Jan 21 '17 at 13:48
  • 1
    Working fine on Python2.7.6 without specifying dtype. Maybe new version of numpy handles it? Has it got to do with 32 bit Operating system by any chance? – Mohammad Yusuf Jan 21 '17 at 13:49
  • @MYGz Of course. ;) See what `np.array([32, 33]).dtype` prints. And of course, it's possible to install a 32 bit Python on a 64 bit system. – PM 2Ring Jan 21 '17 at 13:53
  • @MYGz I'm not sure. I've tested it with Python 2.7.9 64-bit, numpy 1.9.0 on Windows 10 64-bit and can reproduce the behavior OP describes. – DeepSpace Jan 21 '17 at 13:54
  • Interesting. I guess there's an argument to make `int32` the default type to maintain backwards compatibility. But I don't have a 64 bit machine, and I don't use Windows, so I'll shut up. ;) – PM 2Ring Jan 21 '17 at 13:56
  • If I specify `dtype=np.int32` then I can reproduce it. With a warning `/usr/local/lib/python2.7/dist-packages/ipykernel/__main__.py:2: RuntimeWarning: invalid value encountered in power from ipykernel import kernelapp as app` – Mohammad Yusuf Jan 21 '17 at 13:58
3

It is because when using numpy the numbers given will have a certain size, for example a 32 or 64 bit int. These will overflow in the given calculations, and give negative numbers.

Try making them floats, it should help:

print 2**np.array([32., 33.])
J. P. Petersen
  • 4,871
  • 4
  • 33
  • 33