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.

2
  • 1
    If you want such large numbers, why do you explicitly request 64 bit ints? Commented Feb 11, 2022 at 19:55
  • 1
    I thought that's the highest it goes. I tried int128, but it said numpy has no attribute 'int128'. Commented Feb 11, 2022 at 20:04

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I switched to float64, and it worked.

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.