Python math.isinf() – Check if Number is Infinity

Python math.isinf()

math.isinf(x) function returns True if x is either positive infinity or negative infinity, and False otherwise.

Syntax

The syntax to call isinf() function is

math.isinf(x)

where

ParameterRequiredDescription
xYesA numeric value.

Examples

1. Check if positive infinity is infinity

Assign x with positive infinity, and programmatically check if x is infinity value, using math.isinf().

Python Program

import math

x = math.inf
result = math.isinf(x)
print('isinf(x) :', result)
Run Code Copy

Output

isinf(x) : True

2. Check if negative infinity is infinity

Assign x with negative infinity, and programmatically check if x is infinity value, using math.isinf().

Python Program

import math

x = -math.inf
result = math.isinf(x)
print('isinf(x) :', result)
Run Code Copy

Output

isinf(x) : True

3. Check if integer 8 is infinity

Assign x with a finite value, like 8, and programmatically check if x is infinity value, using math.isinf().

Python Program

import math

x = 8
result = math.isinf(x)
print('isinf(x) :', result)
Run Code Copy

Output

isinf(x) : False

Summary

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

Code copied to clipboard successfully 👍