List comprehension of powers of 2 in Python fails with numpy array
2^100 is too big for an int, you should set the type to object
[2**x for x in np.array([2, 100], dtype='object')]
will give
[4, 1267650600228229401496703205376]
np.array
converts the contents of the list to numpy types:
>>> import numpy as np>>> x = np.array([2, 100])>>> type(x[1])<class 'numpy.int64'>
This is numpy's fixed length 64 bit integer.
Whereas in python, we get:
>>> y = [2, 100]>>> type(y[1])<class 'int'>
which is python's arbitrary sized integer (BigInteger in other languages)
For whatever reason (this is probably specced in numpy, but perhaps not the behaviour I'd expect!), numpy's int64
overflows to zero:
>>> 2 ** np.int64(100)0
(or more directly)
>>> 2 ** x[1]0
The list comprehension in this case is a red herring, the actual cause of the difference is the conversion that np.array
is doing.