0

I need to operate on some pretty large numbers. Here, I am trying to take the cube of a large number inside an array.

import numpy as np
array=np.array([149597500000,3,4],dtype=np.int64)
print(array**3)

This gives

[4258029614102052864                  27                  64]

The second 2 values are correct, but the first one is off by many orders of magnitude. By contrast,

print(149597500000**3)

gives

3347904087604984375000000000000000

which is the correct result. Is this related to integer overflow? If so, why doesn't performing the operation outside the array also cause an overflow? How can I work around this problem? Sorry if this is a basic question; I never learned Python in a formal way.

Leon
  • 75
  • 5

1 Answers1

1

I would say the number of bits in the first number to the cube is at least 3*log2(149597500000)+1=113. This does not fit in a 64 bits

a = 149597500000
b= a**3
print(a.bit_length(), b.bit_length())

returns: 38, 112

you can store that bigs number in numpy using either dtype=object or np.float64, see Stocking large numbers into numpy array

Thomasi Muc
  • 164
  • 7