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)
OutputPositive 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)
OutputPositive 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)
OutputPositive 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)
OutputPositive 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))
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)
OutputTrue
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:
Similar Reads
__init__ in Python
Prerequisites - Python Class and Objects, Self__init__ method in Python is used to initialize objects of a class. It is also called a constructor. It is like a default constructor in C++ and Java. Constructors are used to initialize the objectâs state.The task of constructors is to initialize (assig
5 min read
numpy.isfinite() in Python
The numpy.isfinite() function tests element-wise whether it is finite or not(not infinity or not Not a Number) and return the result as a boolean array. Syntax :Â numpy.isfinite(array [, out]) Parameters :Â array : [array_like]Input array or object whose elements, we need to test for infinity out :
2 min read
numpy.isinf() in Python
The numpy.isinf() function tests element-wise whether it is +ve or -ve infinity or not return the result as a boolean array. Syntax: numpy.isinf(array [, out]) Parameters :  array : [array_like]Input array or object whose elements, we need to test for infinity out : [ndarray, optional]Output array
2 min read
Infinite Iterators in Python
Iterator in Python is any python type that can be used with a âfor in loopâ. Python lists, tuples, dictionaries, and sets are all examples of inbuilt iterators. But it is not necessary that an iterator object has to exhaust, sometimes it can be infinite. Such type of iterators are known as Infinite
2 min read
numpy.isneginf() in Python
The numpy.isneginf() function tests element-wise whether it is negative infinity or not, and returns the result as a boolean array. Syntax :  numpy.isneginf(array, y = None) Parameters : array : [array_like]Input array or object whose elements, we need to test for infinity. y : [array_like]A boole
2 min read
numpy.isposinf() in Python
The numpy.isposinf() function tests element-wise whether it is positive infinity or not and returns the result as a boolean array. Syntax : numpy.isposinf(array, y = None) Parameters:  array : [array_like]Input array or object whose elements, we need to test for infinity. y : [array_like]A boolea
2 min read
numpy.isnan() in Python
The numpy.isnan() function tests element-wise whether it is NaN or not and returns the result as a boolean array. Syntax :Â numpy.isnan(array [, out]) Parameters :Â array : [array_like]Input array or object whose elements, we need to test for infinity out : [ndarray, optional]Output array placed wit
2 min read
Indentation in Python
In Python, indentation is used to define blocks of code. It tells the Python interpreter that a group of statements belongs to a specific block. All statements with the same level of indentation are considered part of the same block. Indentation is achieved using whitespace (spaces or tabs) at the b
2 min read
Python int() Function
The Python int() function converts a given object to an integer or converts a decimal (floating-point) number to its integer part by truncating the fractional part.Example:In this example, we passed a string as an argument to the int() function and printed it.Pythonage = "21" print("age =", int(age)
3 min read
enum.IntEnum in Python
With the help of enum.IntEnum() method, we can get the enumeration based on integer value, if we compare with normal enum based class it will fail by using enum.IntEnum() method. Syntax : enum.IntEnum Return : IntEnum doesn't have a written type. Example #1 : In this example we can see that by using
1 min read