math.isfinite()

Anonymous contributor's avatar
Anonymous contributor
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.

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
Loading...

All contributors

Contribute to Docs

Learn Python on Codecademy