[Solved]: python OverflowError : (34, ‘Result too large’)


In Python, OverflowError is subclass of ArithmeticError. This error occurred for floating points numbers when size exceed the limit of variable type.

In case of Integer when size grow variable convert to long value. If variable size exceed the limit of Long also then Python throw MemoryError.

Example of OverFlowError

In this Python program we are just continually multiplying the value for floating numbers with 2 as long as for loop condition match (50 times) because it’s floating number and having limit of size once this size limit will exceed will throw exception as OverflowError.

i=1
try:
    f = (2.0**i)
    for i in range(50):
        print (i, f)
        f = f ** 2
except OverflowError as err:
    print ('Overflowed on power ', f, err)

Output

0 2.0
1 4.0
2 16.0
3 256.0
4 65536.0
5 4294967296.0
6 1.8446744073709552e+19
7 3.402823669209385e+38
8 1.157920892373162e+77
9 1.3407807929942597e+154
Overflowed on power  1.3407807929942597e+154 (34, 'Result too large')

If you noticed this Python program this is throwing error as “OverflowError: (34, ‘Result too large’)” because floating variable size is continuesly increasing once it will reach to 34 for precision will throw OverflowError.

Solution

In Python, when you are performing operation of floating number and these operations are inside the recursive method or loops then always handle the OverflowError by try catch block so that your program will not terminate.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s