-1

I have to handle very big integers in my program, but I get the following error:

    Traceback (most recent call last):
  File "[path]", line n, in <module>
    number = int(numbers[0]*(10**numbers[1]))
OverflowError: (34, 'Numerical result out of range')

for number = int(n)when I entered 8e10000000 as n.

Is there a way to solve this problem? Thanks in advance.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Matina
  • 15
  • 1
  • 5
  • Why do you need to handle numbers quite that large? `8e10000000` is an enormous number – Nick is tired Jul 20 '18 at 13:36
  • The code you give (`number = int(8e10000000)`) is different from the one that appears in the traceback (`number = int(numbers[0]*(10**numbers[1]))`, and they are very different, as the first one involves a float, while the second one only uses integers. Please clarify your question. – Thierry Lathuille Jul 20 '18 at 13:47
  • This **isn't integer overflow**, which doesn't happen in Python. The problem occurs **before** `int` can be called. – Karl Knechtel Feb 13 '23 at 14:01

3 Answers3

1

The number 8e10000000 is not an integer, it is a floating point number to Python. Any number using the e notation is treated as a float. Python uses (usually) a 64-bit float format, and that cannot hold such a large number.

So the problem is not the integer, it is the float you start with. The error is not at the line number = int(n), it is at the line n = 8e10000000 or whatever equivalent you used.

You can avoid that error by using

n = 8 * 10**10000000

This results in an integer. But be careful--that takes a lot of time and memory to build the integer in RAM. (My system took 19 seconds to execute that one command.) And if you try to print that value, the computer will take a very long time and a large amount of memory to build up the string value to be printed.

Finally, as others have pointed out, that statement that you claim does not match the error message. So it is possible that something else is going on. If you want closure from us, show an entire code snippet that shows that error.

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
0

8e10000000 is very large number, and Python it represents as a float.
CPython usually store this float in 64-bit size, which is too small for such a big number.

For such large numbers is safe to use Decimal module:

import sys
from decimal import Decimal

print('max size = ', sys.maxsize)
number = Decimal("8e10000000")
print(number)

Outputs:

max size =  9223372036854775807
8E+10000000

The number 9223372036854775807 is exactly 2^63 - 1.

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • In Python 3, there is **no** maximum integer. `sys.maxsize` returns " an integer giving the maximum value a variable of type Py_ssize_t can take. It’s usually 2* *31 - 1 on a 32-bit platform and 2**63 - 1 on a 64-bit platform.", this is completely unrelated to the question. – Thierry Lathuille Jul 20 '18 at 16:39
  • @ThierryLathuille Yes, you have right. That's Python2. I edited my answer. – Andrej Kesely Jul 20 '18 at 16:54
0

What you're running into is an issue where python is Strongly Typed but also Dynamically Typed. 8e10000000 is actually of the python (and C) type float and is a valid value for a double precision floating point binary representation, whereas the maximum valid value for a python int is 9,223,372,036,854,775,807 (found with sys.maxint).

So, python has a decimal library that has a class decimal.Decimal documentation here that does arbitrary precision numbers. It's not going to be as memory or speed efficient, but it bypasses the size-limit and precision issues that floating point numbers have, and are particularly when dealing with money.

The other option that you can consider if you are truly using inter values is to use long(n) to cast your variable to an arbitrarily large integer in python 2.5+ (in python 3.0+ int is a long) here's a link to the PEP 237 that talks about it.

VoNWooDSoN
  • 1,173
  • 9
  • 13