FloatingPointError

FloatingPointError is a built-in exception that indicates an error related to floating-point arithmetic operations. You won’t encounter it in standard-library Python because the language doesn’t trap floating-point exceptions raised by hardware.

NumPy uses FloatingPointError to align Python with exception events as defined by IEEE 754. When using NumPy, you may therefore run into a FloatingPointError when you perform operations that produce results that aren’t representable within the limits of floating-point numbers.

FloatingPointError Occurs When

You perform operations that produce results that aren’t representable within the limits of floating-point numbers, for example, in libraries like NumPy.

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 because Python doesn’t trap most IEEE 754 floating-point hardware exceptions. Instead, it typically produces special float values (inf, -inf, or nan) on 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.

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.

basics python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated March 11, 2025 • Reviewed by Martin Breuss