[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
try:
    print ('FPECTL Control Not Enable:', math.exp(750))
    fpectl.turnon_sigfpe()
    print ('FPECTL Control Enabled:', math.exp(750))
except Exception as err:
    print (err)
    print (sys.exc_type)

Output

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

Solution

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.