Home » Python

math.isfinite() method with example in Python

Python math.isfinite() method: Here, we are going to learn about the math.isfinite() method with example in Python.
Submitted by IncludeHelp, on April 18, 2019

Python math.isfinite() method

math.isfinite() method is a library method of math module, it is used to check whether a given number is a finite number of not, it accepts a number (integer/float, finite, infinite or NaN), and returns True if number neither an infinity nor a NaN (Not A Number), else it returns False.

Syntax of math.isfinite() method:

    math.isfinite(n)

Parameter(s): n – a number that has to be checked whether it is finite or not.

Return value: bool – it returns a Boolean ("True" or "False") value.

Example:

    Input:
    a = 10
    b = float('inf')

    # function call
    print(math.isfinite(a))
    print(math.isfinite(b))

    Output:
    True
    False

Python code to demonstrate example of math.isfinite() method

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

# importing math module
import math

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

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

Output

True
True
True
True
False
False
False


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.