Open In App

Python infinity (inf)

Last Updated : 16 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

But in Python, as it is a dynamic language, float values can be used to represent an infinite integer. One can use float('inf') as an integer to represent it as infinity. Below is the list of ways one can represent infinity in Python. 

1. Using float('inf') and float('-inf')

As infinity can be both positive and negative they can be represented as a float('inf') and float('-inf') respectively.

Python
positive_infinity = float('inf')
print('Positive Infinity: ', positive_infinity)

negative_infinity = float('-inf')
print('Negative Infinity: ', negative_infinity)

Output
Positive Infinity:  inf
Negative Infinity:  -inf

2. Using Python's Math Module

Python's math module can also be used for representing infinite integers. Pythons math.inf constant return positive infinity and -math.inf returns negative infinity.

Python
import math

positive_infinity = math.inf
print('Positive Infinity: ', positive_infinity)

negative_infinity = -math.inf
print('Negative Infinity: ', negative_infinity)

Output
Positive Infinity:  inf
Negative Infinity:  -inf

3. Using Python's Decimal Module

Python's decimal module can also be used for representing infinite float values. It is used as Decimal('Infinity') for positive and Decimal('-Infinity') for negative infinite value.   

Python
from decimal import Decimal

positive_infinity = Decimal('Infinity')
print('Positive Infinity: ', positive_infinity)

negative_infinity = Decimal('-Infinity')
print('Negative Infinity: ', negative_infinity)

Output
Positive Infinity:  Infinity
Negative Infinity:  -Infinity

4. Using Python's Numpy library

Python's Numpy module can also be used for representing infinite values. It is used as np.inf for positive and -np.inf for negative infinite value. The use of Numpy library for representing an infinite value is shown in the code below: 

Python
import numpy as np

positive_infinity = np.inf
print('Positive Infinity: ', positive_infinity)

negative_infinity = -np.inf
print('Negative Infinity: ', negative_infinity)

Output
Positive Infinity:  inf
Negative Infinity:  -inf

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: 

Python
import numpy as np
import math

a = np.inf
b = -np.inf

c = 300

print(math.isinf(a))
print(math.isinf(b))
print(math.isinf(c))

Output
True
True
False

Explanation: This code checks if a number is infinite using the math.isinf() function. It returns True for positive infinity (np.inf) and negative infinity (-np.inf), and False for finite numbers (like 300). This helps to identify if a value is infinite in mathematical operations.

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.

Python
import numpy as np

a = np.inf
b = -np.inf

c = 300
d = -300

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

Explanation: This code compares positive and negative infinity with finite values using the compare() function. It checks if one value is greater than another and prints "True" or "False". The comparisons involve infinity (np.inf and -np.inf) and finite numbers (300 and -300).

Related Articles:


Next Article

Similar Reads