Python isinf() is an inbuilt method that is used to find whether a number is infinite or not. It accepts a number (integer, float, NaN, inf) and checks whether it is infinite or not. The isinf() function is under python math library, so we have to import math if we want to use the isinf() function.
The isinf() method is a method of the math module that 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.
The isinf() function takes n as an argument and checks whether n is infinite or not.
Here n can be an integer, float, double, inf, NaN, etc.
The math.isinf() function returns two types of output:
True: If the number is infinite.
False: If the number is not infinite.
See the following code.
# app.py # Importing math library import math # Checking working of isinf() # When the number is integer print(math.isinf(109)) # When the number is float print(math.isinf(100.55)) # When the number is negative print(math.isinf(-14)) # When the number is not finite print(math.isinf(float('inf')))
False False False True
In this program, we have the first imported math library, and then we have checked the output of isinf() function using various input.
We have checked output for integer, float, and negative numbers, respectively; for all these cases, the output is False. Then we have checked output for inf, here the output is True.
See another example.
# app.py import math print(math.isinf(21)) print(math.isinf(0.0)) print(math.isinf(0/1)) print(math.isinf(0.0/1.0)) print(math.isinf(-500))
python3 app.py False False False False 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.
Finally, Python isinf() Method Example is over.