Python infinity representation
Python is a dynamic language that provides several ways of representing “Infinity”. “Infinity” is an undefined number that is defined as either “Positive” or “Negative”. The arithmetic operations such as addition, subtraction, multiplication, etc. when applied with the “Infinity” value produce a positive or negative infinite number.
This write-up explores several ways/approaches to create and utilize “infinity” in Python.
Representation of Python Infinity
The following methods are utilized to represent Python “infinity”:
Method 1: Represent an Infinity Using the “math.inf” Constant
The inbuilt “math” module provides the constants named “math.inf” and “-math.inf” to retrieve the positive and negative infinite values, respectively.
Example
The below example code is utilized to represent an infinity:
print ('Positive Infinity: ', math.inf)
print ('Negative Infinity: ', -math.inf)
In the above code, the “math” module is imported and the “math.inf” and “-math.inf” constants retrieve the positive and negative infinity values, respectively.
Output
The positive and negative infinity has been shown in the above snippet.
Method 2: Represent an Infinity Using the “np.inf” Constant
The “Numpy” library provides a constant term “np.inf” to represent the “positive infinity” in Python. It can represent an infinite value in a numerical computation or comparison.
Example
Here is an example code for representing an infinity:
print ('Positive Infinity: ', np.inf)
print ('Negative Infinity: ', -np.inf)
According to the above code, the “np.inf” and “-np.inf” constants are utilized to retrieve the positive and negative infinity values, respectively in Python.
Output
In the above output, both the positive and negative infinity values are shown accordingly.
Method 3: Represent an Infinity Using the “float()” Function
The “float()” function can be utilized to retrieve a floating-point number in Python. This function can also be utilized to represent a value of infinity that is positive or negative.
Example
The following code snippet represents an infinity:
print ('Negative Infinity: ', float('-inf'))
Here in this code, the inbuilt “float()” function takes the “inf” and “-inf” as its arguments individually and retrieves the positive and negative infinite values accordingly.
Output
Method 4: Represent an Infinity Utilizing the “decimal” Module
Python provides the “decimal” module for accurately rounding floating point arithmetic. The “positive infinity” and “negative infinity” can also be represented using the “Decimal” class in Python.
Example
The given code is utilized to represent an infinity:
print ('Positive Infinity: ', Decimal('Infinity'))
print ('Negative Infinity: ', Decimal('-Infinity'))
In these code lines, the “Decimal” class is imported from the “decimal” module. The “Decimal()” class takes “Infinity” and “-Infinity” as its arguments and retrieves the positive and negative infinite values, respectively.
Output
Conclusion
The “math.inf”, “np.inf”, “float()” function, or the “decimal” module are utilized to represent positive and negative infinity values in Python. This tutorial presented a detailed guide on “infinity representation” in Python utilizing multiple examples.
Source: linuxhint.com