Introduction Python isinf() Method with Examples

Python isinf() is a library method of math module, it is used to check whether a number is an infinity (positive or negative), it accepts a number and returns True if the given number is positive or negative infinity, else it returns False.

Python isinf()

Syntax

math.isinf(n)

Parameter(s):

  • n – a number that has to be checked whether it is infinity or not.

Return value:

  • The return value of isinf() function is either True or False

Examples of Python isinf() method

Example 1


# import math library
import math
 
print(math.isinf(2))
print(math.isinf(0.0))  # Python considered 0.0 as finite number
print(math.isinf(0/1))
print(math.isinf(0.0/1))
print(math.isinf(-500))

Output

False
False
False
False
False

Example 2

# python code to demonstrate example of 
# math.isinf() method

# importing math module
import math

# math.isinf() method test on finite value
print(math.isinf(10))
print(math.isinf(0))
print(math.isinf(10.23))
print(math.isinf(0.0))


# math.isinf() method test on infinite value
print(math.isinf(float('inf')))
print(math.isinf(float('-inf')))
print(math.isinf(float('nan')))

Output


False
False
False
True
True
False

Sometimes, we need to use positive or negative infinity as a value. For instance, it is used in algorithms where you need to compare the current answer to the best answer.
Usually, at the initiation step, we like to set the value to positive or negative infinity to make sure no other value in the input would be bigger/smaller.

Python isinf() function is used to determine whether a specific number is an infinite number or not.

Thanks for reading .

#python #programming

Introduction Python isinf() Method with Examples
1 Likes16.85 GEEK