FloatingPointError
Plain Python never raises it — float problems show up as ZeroDivisionError, OverflowError, ValueError or a silent inf/nan. If you see it, NumPy (np.seterr) or your own code raised it.
FloatingPointError(*args)
Raised by
NumPy with np.seterr(all='raise'); your own checks
Message
whatever the raiser chose — NumPy: "divide by zero encountered in …"
Quick fix
find the inf/nan source, or np.errstate to scope the setting
Watch out
CPython itself: "Not currently used"
Constructor
| Name | Type | Required | Description |
|---|---|---|---|
| *args | object | no | Usually one message string. str(e) is that message. |
Attributes
| Attribute | Type | Meaning |
|---|---|---|
| args | tuple | args[0] is the message. No operand or operation attributes. |
| __context__ | BaseException | None | The exception being handled when this one was raised (implicit chaining). |
Common patterns
Turn silent inf/nan into an error
Plain float +, - and * return inf or nan without raising. If that should be fatal, check and raise — FloatingPointError is the natural class to use.
import math def checked(x): if not math.isfinite(x): raise FloatingPointError(f'non-finite result: {x}') return x
NumPy: raise only in one block
np.errstate is a context manager, so the stricter setting does not leak into the rest of the program.
import numpy as np with np.errstate(divide='raise', invalid='raise'): ratio = a / b
NumPy: catch it
With floating-point errors set to raise, bad elements stop the computation with FloatingPointError.
import numpy as np np.seterr(all='raise') try: result = np.log(values) except FloatingPointError as e: print('bad input:', e)
Examples
1. Construct it explicitly
str(FloatingPointError('overflow in pow'))
Returns
'overflow in pow'2. It is an ArithmeticError
issubclass(FloatingPointError, ArithmeticError)
Returns
True3. Float overflow is silent here
1e308 * 10
Returns
inf4. inf - inf is a silent nan
float('inf') - float('inf')
Returns
nan5. 0.0 / 0.0 is ZeroDivisionError
0.0 / 0.0
Returns
ZeroDivisionError: float division by zero6. Domain errors are ValueError
import math
math.sqrt(-1)
Returns
ValueError: math domain error7. Raising it from your own check
import math
def checked(x):
if not math.isfinite(x):
raise FloatingPointError(f'non-finite result: {x}')
return x
checked(1e308 * 10)
Returns
FloatingPointError: non-finite result: infPitfalls
1. Catching FloatingPointError for plain Python math
CPython raises a different class for each float problem, so this handler never runs. Catch ArithmeticError (or the specific class) instead.
except FloatingPointError
try: r = 1.0 / 0.0 except FloatingPointError: r = 'handled' r
ZeroDivisionError: float division by zero
except ArithmeticError
try: r = 1.0 / 0.0 except ArithmeticError: r = 'handled' r
'handled'
2. Expecting an exception for inf and nan
Arithmetic that overflows or has no meaningful result returns inf/nan silently and poisons later results. Test with math.isfinite / math.isnan.
No error, wrong value
x = float('inf') - float('inf') x == x
False
math.isnan
import math x = float('inf') - float('inf') math.isnan(x)
True
When to use
Use it
- Your own numeric code wants a dedicated error for non-finite results
- Catching NumPy errors after np.seterr / np.errstate set to raise
Reach for something else
- Handling plain Python float errors → ZeroDivisionError, OverflowError, ValueError
- Catch-all for arithmetic → except ArithmeticError
Notes
Docs
The built-in exceptions page describes it in three words: Not currently used.
History
The fpectl module (floating-point exception control, never enabled by default) was removed in Python 3.7
NumPy
np.seterr(all='raise') turns divide, over, invalid (and under) floating-point conditions into FloatingPointError
Catch via
except ArithmeticError also catches ZeroDivisionError and OverflowError
FAQ
There is nothing in plain Python that triggers it. The official docs describe FloatingPointError as "Not currently used": CPython reports float problems as ZeroDivisionError, OverflowError or ValueError, or returns inf/nan silently. The only ways to see it are raising it yourself (see the examples) or third-party code such as NumPy, which is not available in the demo sandbox.