Isfinite() Function In Python Math Module


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


# 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"
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)


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 2022 ©