Isfinite() Function In Python Math Module

Overview:

  • The function isfinite() checks whether the parameter passed in is infinite or not and returns a Boolean result.

Example:

# Example Python program that checks 
# a number for finiteness
import math

# Is a positive integer finite?
num = 12345600000000000000
result = math.isfinite(num)
print("A positive integer is finite:%s"%result)

# Is a negative integer finite?
num = -12345600000000000000
result = math.isfinite(num)
print("A negative integer is finite:%s"%result)

# Check pi is finite or infinite
num = float("3.1415926535897932384626433832795028841971693993751058"
            "209749445923078164062862089986280348253421170679821480"
            "8651328230664709384460955058223172535940812848111745028410270193") 
result = math.isfinite(num)
print("Pi is finite:%s"%result)

# Check with infinity
num = math.inf
result = math.isfinite(num)
print("Infinity is finite:%s"%result)

# Check with NaN
result = math.isfinite(float('nan'))
print("NaN is finite:%s"%result)

Output:

A positive integer is finite:True

A negative integer is finite:True

Pi is finite:True

Infinity is finite:False

NaN is finite:False

 


Copyright 2023 © pythontic.com