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
Also read: numpy.isinf() in Python
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.