Comparing Infinite Values to Finite Values in Python

The concept of comparing an infinite value to finite values is as simple as it gets. As positive infinity is always bigger than every natural number and negative infinity is always smaller than negative numbers.

For a better understanding look into the code below: 

Python3




import numpy as np
# Defining a positive infinite integer
a = np.inf
# Defining a negative infinite integer
b = -np.inf
# Define a finite + ve integer
c = 300
# Define a finite -ve integer
d = -300
# helper function to make comparisons
def compare(x, y):
    if x>y:
        print("True")
    else:
        print("False")
         
compare(a, b)
compare(a, c)
compare(a, d)
compare(b, c)
compare(b, d)


Output: 

True
True
True
False
False

Using infinity in programming is very tricky, but Python made it very easy. Python inf can be used with more than 3 methods, which makes Python very user-friendly.

Hope you can now use infinity(inf) in Python now, and use it for solutions. 



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

...