Tag Archives: ArithmeticError

[Solved]: Python FloatingPointError

In Python, FloatingPointError is subclass of ArithmeticError. FloatingPointError occured with floating point operations when floating point exception control (fpectl) is turned on.
To Enable fpectl in Python requires an interpreter compiled with the –with-fpectl flag.

Note : In Python, fpectl module is not built by default, you have explicitly import it to control over the floating point units from several hardware manufacturer. This allow the use to turn on the generation of SIGFPE whenever any IEEE-754 exceptions Division by Zero, Overflow, or Invalid Operation occurs.

Example FloatingPointError

The below example is created to occurs FloatingPointError in Python program where just trying exponential of number with certain values before and after enabling the fpectl.

import sys
import math
import fpectl
    print ('FPECTL Control Not Enable:', math.exp(750))
    print ('FPECTL Control Enabled:', math.exp(750))
except Exception as err:
    print (err)
    print (sys.exc_type)


FPECTL Control Not Enable: 1.01423205474e+304
FPECTL Control Enabled: in math_1
<type 'exceptions.FloatingPointError'>


When dealing with any floating point number and performing operation where precision values can be more always handle Exception.

To learn more on exception handling follow the link Python: Exception Handling.

If this blog for solving FloatingPointError help you to resolve problem make comment or if you know other way to handle this problem write in comment so that it will help others.

[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.

    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)


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.


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.

[Solved] Python ZeroDivisionError: division by zero in Python

The super class of ZeroDivisionError is ArithmeticError. This exception raised when the second argument of a division or modulo operation is zero. The associated value is a string indicating the type of the operands and the operation.

In simple term in any arithmetic operation when value divided by zero then in Python throw ZeroDivisionError.

You can see complete Python exception hierarchy through this link : Python: Built-in Exceptions Hierarchy.

Example :

In the Python program will throw ZeroDivisionError in case of num_list is not having any element then it’s length become 0 and while executing this program will through ZeroDivisionError.



ZeroDivisionError : Division by Zero


While implementing any program logic and there is division operation make sure always handle ArithmeticError or ZeroDivisionError so that program will not terminate. To solve above problem follow this example:

except ZeroDivisionError:
    print ("Zero Division Error occurred")


Zero Division Error occurred.

In this above modified code applied exception handling for particular code section so that program will not terminate.

Learn Python exception handling in more detain in topic Python: Exception Handling

Let me know your thought on it.

Happy Learning !!!