FloatingPointError
FloatingPointError is a built-in exception that indicates an error related to floating-point arithmetic operations. CPython never raises it—the Python documentation describes FloatingPointError as “Not currently used”—because the interpreter doesn’t trap the floating-point exceptions signaled by the hardware.
NumPy uses FloatingPointError to align Python with exception events as defined by IEEE 754. NumPy doesn’t raise FloatingPointError by default, though. Its default error handling is {'divide': 'warn', 'over': 'warn', 'under': 'ignore', 'invalid': 'warn'}, so a bad operation still yields inf or nan and only emits a RuntimeWarning. You get a FloatingPointError when you opt in by switching the relevant error mode to 'raise' with np.seterr() or the np.errstate() context manager.
FloatingPointError Occurs When
A library such as NumPy is configured to raise on an IEEE 754 floating-point event—division by zero, overflow, underflow, or an invalid operation—which you enable by setting that event’s handler to 'raise' via np.seterr() or np.errstate().
FloatingPointError Can Be Used When
- Explicitly handling hardware exceptions based on floating-point arithmetic in scientific computing
- Working with libraries like NumPy that allow for custom floating-point error handling
- Working with precise floating-point error detection beyond the default behavior
FloatingPointError Example
In pure Python, a FloatingPointError doesn’t naturally occur because Python doesn’t trap most IEEE 754 floating-point hardware exceptions. Instead, it typically produces special float values (inf, -inf, or nan) during overflow or invalid operations.
However, Python does raise other built-in exceptions like ZeroDivisionError or ValueError for certain floating-point related error conditions, rather than returning inf or nan.
Related Resources
Tutorial
Python Exceptions: An Introduction
In this beginner tutorial, you'll learn what exceptions are good for in Python. You'll see how to raise exceptions and how to handle them with try ... except blocks.
For additional information on related topics, take a look at the following resources:
- Working With Python's Built-in Exceptions (Course)
- Python's raise: Effectively Raising Exceptions in Your Code (Tutorial)
- Introduction to Python Exceptions (Course)
- Raising and Handling Python Exceptions (Course)
- Python Exceptions: An Introduction (Quiz)
- Using raise for Effective Exceptions (Course)
- Python's raise: Effectively Raising Exceptions in Your Code (Quiz)