API · Python

Python math.isfinite() Method

The math.isfinite() is an in-built python function used to check whether a number is infinite or not.

On this page

Python math.isfinite() method is an efficient way of finding whether a number is finite or not. Note that 0.0 is considered a finite number.

Syntax

math.isfinite(x)

Parameters

x The required value (negative or positive) to check.

Returns

This method returns a Boolean value. It returns True if x is a finite value or not a NaN; otherwise, False.

Example Code: Use of the math.isfinite() Method

Example Code:

import math

x = 100
value = math.isfinite(x)
print(f"Is {x} a finite number? {value}")

x = 0
value = math.isfinite(x)
print(f"Is {x} a finite number? {value}")

x = -98.67
value = math.isfinite(x)
print(f"Is {x} a finite number? {value}")

x = math.inf
value = math.isfinite(x)
print(f"Is {x} a finite number? {value}")

x = -math.inf
value = math.isfinite(x)
print(f"Is {x} a finite number? {value}")

value = math.isfinite((float("nan")))
print(f"Is NaN a finite number? {value}")

Output:

Is 100 a finite number? True
Is 0 a finite number? True
Is -98.67 a finite number? True
Is inf a finite number? False
Is -inf a finite number? False
Is NaN a finite number? False

Note that the above code shows how we can use this method.