Checking If a Number Is Infinite in Python

To check if a given number is infinite or not, one can use isinf() method of the math library which returns a boolean value. The below code shows the use of isinf() method: 

Python3




import numpy as np
import math
# Defining a positive infinite integer
a = np.inf
# Defining a negative infinite integer
b = -np.inf
# Define a finite integer
c = 300
# check if a in infinite
print(math.isinf(a))
# check if b in infinite
print(math.isinf(b))
# check if c in infinite
print(math.isinf(c))


Output: 

True
True
False

Python infinity(inf)

As ironic as it may seem infinity is defined as an undefined number that can either be a positive or negative value. All arithmetic operations performed on an infinite value always lead to an infinite number, say it be sum, subtraction, multiplication, or any other operation.

In the world of computer science, infinity is generally used to measure performance and optimize algorithms that perform computations on a large-scale application.

Similar Reads

Representing infinity as an Integer in Python

The concept of representing infinity as an integer violates the definition of infinity itself. As of 2020, there is no such way to represent infinity as an integer in any programming language so far....

Checking If a Number Is Infinite in Python

...

Comparing Infinite Values to Finite Values in Python

...