Python math.isfinite()

NeemaJoju's avatar
Published Aug 28, 2024
Contribute to Docs

The math.isfinite() function returns True when a number is finite and False otherwise. A finite number is neither infinite nor NaN.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

math.isfinite(x)
  • x: The number to be checked. It can be an integer, float, or any numerical value.

Example

The following example uses math.isfinite() to check whether the specified values are finite or not:

import math
# Check if 0.1 is a finite number
print(math.isfinite(0.1))
# Check if positive infinity is a finite number
print(math.isfinite(math.inf))
# Check if the float representation of infinity is a finite number
print(math.isfinite(float("inf")))
# Check if the float representation of NaN (Not a Number) is a finite number
print(math.isfinite(float("NaN")))

The output generated is as follows:

True
False
False
False

Codebyte Example

Run the following example that uses the math.isfinite() function to understand it works:

Code
Output

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours