Python math.isfinite() - Check if Number is not Infinity or NaN


Python math.isfinite()

math.isfinite(x) function returns True if x is neither infinity nor a NaN, and False otherwise.

Syntax

The syntax to call isfinite() function is

math.isfinite(x)

where

ParameterRequiredDescription
xYesA numeric value.

Examples

1. Check if integer 8 is finite

In the following example, we take value of 8 in variable x, and check if this value is finite or not using math.isfinite() function.

Python Program

import math

x = 8
result = math.isfinite(x)
print('isfinite(x) :', result)

Output

isfinite(x) : True

2. Check if positive infinity is finite

Let us take positive infinity in x, and check if x is finite or not programmatically using math.isfinite() function.

Python Program

import math

x = math.inf
result = math.isfinite(x)
print('isfinite(x) :', result)

Output

isfinite(x) : False

3. Check if nan is finite

Check if x is finite, with x = nan.

Python Program

import math

x = math.nan
result = math.isfinite(x)
print('isfinite(x) :', result)

Output

isfinite(x) : False

Summary

In this Python Math tutorial, we learned the syntax of, and examples for math.isfinite() function.