FloatingPointError
PythonERRORNotableArithmetic ErrorMEDIUM confidence

Floating-point operation failed

Production Risk

Rare in standard Python; more common when using the decimal module with traps enabled.

What this means

A subclass of ArithmeticError raised when a floating-point operation fails. In standard Python, this is rarely raised because the default IEEE 754 behavior returns inf or nan instead of raising. It only triggers when floating-point exceptions are explicitly enabled via the fpectl module (deprecated) or the decimal module.

Why it happens
  1. 1Floating-point traps explicitly enabled and an FP exception occurs
  2. 2Using the decimal module with a trap set for OVERFLOW, DIVISION_BY_ZERO, or INVALID_OPERATION
How to reproduce

Decimal module with DivisionByZero trap enabled.

trigger — this will error
trigger — this will error
import decimal
ctx = decimal.getcontext()
ctx.traps[decimal.DivisionByZero] = True
decimal.Decimal('1') / decimal.Decimal('0')

expected output

decimal.DivisionByZero: [<class 'decimal.DivisionByZero'>]

Fix 1

Check for zero before dividing

WHEN Performing floating-point division

Check for zero before dividing
import math

def safe_divide(a, b):
    if b == 0.0 or math.isclose(b, 0.0):
        return float('inf')
    return a / b

Why this works

Guard division by zero explicitly; standard Python float division returns inf rather than raising.

Fix 2

Use math.isnan / math.isinf for result checking

WHEN After float arithmetic that might produce special values

Use math.isnan / math.isinf for result checking
import math
result = 1.0 / 0.0  # Returns inf in standard Python (no exception)
if math.isinf(result) or math.isnan(result):
    result = 0.0  # or handle appropriately

Why this works

Standard Python float produces inf and nan instead of raising; always check results.

Code examples
Triggerpython
import decimal
ctx = decimal.getcontext()
ctx.traps[decimal.DivisionByZero] = True
decimal.Decimal("1") / decimal.Decimal("0")  # FloatingPointError
Handle with try/exceptpython
import decimal
try:
    result = x / y
except (ZeroDivisionError, decimal.FloatingPointError):
    result = float("inf")
Avoid with zero guardpython
import math
def safe_div(a, b):
    if b == 0.0 or math.isclose(b, 0.0):
        return float("inf")
    return a / b
Same error in other languages
Sources
Official documentation ↗

Python Docs — Built-in Exceptions

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Python errors