Python Program for isfinite() Function

In the previous article, we have discussed Python Program for fsum() Function
isfinite() Function in Python:

The math.isfinite() method determines whether or not a number is finite.

If the specified number is a finite number, this method returns true; otherwise, it returns False.

Syntax:

math.isfinite(value)

Parameters:

Value: This is required. The value to be checked. A number (float/integer/infinite/NaN/finite) must be specified.

Return Value: It returns a boolean value. Returns True if x is finite and False if x is infinity or NaN.

Examples:

Example1:

Input:

Given Number = 20
Given number = -100
Given Number = math.pi
Given value = NaN
Given value = inf

Output:

Checking if the above given number 20 is finite or not : True
True
True
False
False

Example2:

Input:

Given Number = 10000
Given value = -math.inf
Given value = nan

Output:

Checking if the above given number 10000 is finite or not : True
False
False

Program for isfinite() 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.isfinite() function to the given number to check if the given number is finite 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 = 20
# Apply math.isfinite() function to the given number to check if the given number
# is finite or not.
# Store it in another variable.
rslt = math.isfinite(gvn_numb)
# print the above result.
print("Checking if the above given number",
      gvn_numb, "is finite or not :", rslt)
# similarly check for the other values 
print(math.isfinite(-100))
print(math.isfinite(math.pi))
print(math.isfinite(float('NaN')))
print(math.isfinite(float('inf')))

Output:

Checking if the above given number 20 is finite or not : True
True
True
False
False

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import math module using the import keyword.
  • Give the number as user input using the int(input()) function and store it in a variable.
  • Apply math.isfinite() function to the given number to check if the given number is finite 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.isfinite() function to the given number to check if the given number
# is finite or not.
# Store it in another variable.
rslt = math.isfinite(gvn_numb)
# print the above result.
print("Checking if the above given number",
      gvn_numb, "is finite or not :", rslt)
# similarly check for the other numbers
b = int(input('Enter some random number = '))
print(math.isfinite(b))

Output:

Enter some random number = -1000
Checking if the above given number -1000 is finite or not : True
Enter some random number = -19999
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.