In the previous article, we have discussed Python Program for How To Find Cube Root
isinf() Function in Python:
The math.isinf() method determines whether or not a number is infinite.
If the specified number is positive or negative infinity, this method returns true; otherwise, it returns False.
Syntax:
math.isinf(x)
Parameters:
x: This is required. It is a number to check.
Return Value: It returns a boolean value. If x is positive or negative infinity, this statement is true. Otherwise, false.
Examples:
Example1:
Input:
Given value = 1000 Given value = -100 Given value = math.pi Given value = nan Given value = inf Given value = -inf
Output:
False False False False True True
Example2:
Input:
Given value = -70000 Given value = NaN
Output:
False False
Program for isinf() Function in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Import math module using the import keyword.
- Give the number as static input and store it in a variable.
- Apply math.isinf() function to the given number to check if the given number is infinite or not.
- Store it in another variable.
- Print the above result.
- Similarly, check for the other values and print the result.
- The Exit of Program.
Below is the implementation:
# Import math module using the import keyword. import math # Give the number as static input and store it in a variable. gvn_numb = 1000 # Apply math.isinf() function to the given number to check if the given number # is infinite or not. # Store it in another variable. rslt = math.isinf(gvn_numb) # print the above result. print("Checking if the above given number", gvn_numb, "is infinite or not :", rslt) # similarly check for the other values and print the result. print(math.isinf(-100)) print(math.isinf(math.pi)) print(math.isinf(float('nan'))) print(math.isinf(float('inf'))) print(math.isinf(float('-inf')))
Output:
Checking if the above given number 1000 is infinite or not : False False False False True True
Method #2: Using Built-in Functions (User Input)
Approach:
- Import math module using the import keyword.
- Give the number as static input and store it in a variable.
- Apply math.isinf() function to the given number to check if the given number is infinite or not.
- Store it in another variable.
- Print the above result.
- Similarly, check for the other values and print the result.
- The Exit of Program.
Below is the implementation:
# Import math module using the import keyword. import math # Give the number as user input using the int(input()) function and store it in a variable. gvn_numb = int(input('Enter some random number = ')) # Apply math.isinf() function to the given number to check if the given number # is infinite or not. # Store it in another variable. rslt = math.isinf(gvn_numb) # print the above result. print("Checking if the above given number", gvn_numb, "is infinite or not :", rslt) # similarly check for the other values b = input('Enter some random value = ') print(math.isinf(float(b)))
Output:
Enter some random number = -9999 Checking if the above given number -9999 is infinite or not : False Enter some random value = inf True
Read all the mathematical functions available in Python and understand how to implement them in your program by using the tutorial of Python Mathematical Methods Examples.