# Python Program for Setting a Variable Value to Infinity

Your dataset cannot be represented by a single number? In Python, how about setting your variable value to infinity? That’s exactly what we’re going to speak about today!

We frequently need to initialize a variable in Python with a large positive or large negative value. When comparing variables to determine the minimum or maximum in a set, this is fairly common.

In Python, positive infinity is considered the largest positive value, whereas negative infinity is considered the largest negative value.

Python, representation of infinity as an integer:
The concept of portraying the infinite as an integer contradicts the definition of infinity. In any programming language as of 2020, there is no mechanism to represent infinity as an integer. Float values, on the other hand, can be used to represent an infinite integer in Python because it is a dynamic language. To represent infinity, use float(‘inf’) as an integer.

The following is a list of Python infinite representations.

#### 1)Using float variables to initialize with infinity:

Float is the most simple approach to set a variable to positive or negative infinity without utilizing any modules.

Since infinity can be both positive and negative, it can be expressed as a float(‘inf’) and a float(‘-inf’) respectively.

For Example:

Approach:

• Pass ‘inf’ as an argument to the float() function to define a positive infinite integer and store it in a variable.
• Print the positive Infinity.
• Pass ‘-inf’ as an argument to the float() function to define a negative infinite integer and store it in another variable.
• Print the negative Infinity.
• The Exit of the Program.

Below is the implementation:

# Pass 'inf' as an argument to the float() function to define a positive
# infinite integer and store it in a variable
pos_infnty = float('inf')
# Print the positive Infinity
print('The result of positive Infinity = ', pos_infnty)

# Pass '-inf' as an argument to the float() function to define a negative
# infinite integer and store it in another variable
neg_infnty = float('-inf')
#  Print the negative Infinity
print('The result of negative Infinity = ', neg_infnty)


Output:

The result of positive Infinity =  inf
The result of negative Infinity =  -inf

#### Using Math module:

The math module in Python can also be used to generate infinite integers.

Approach:

• Import math module using the import keyword.
• Get the positive Infinity value by using the math.inf and store it in a variable.
• Print the positive Infinity.
• Get the negative Infinity value by using the -math.inf and store it in another variable.
• Print the negative Infinity.
• The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Get the  positive Infinity value by using the math.inf and store it in a variable
pos_infnty = math.inf
# Print the positive Infinity
print('The result of positive Infinity = ', pos_infnty)

# Get the negative Infinity value by using the -math.inf and store it in another
# variable
neg_infnty = -math.inf
# Print the negative Infinity
print('The result of negative Infinity = ', neg_infnty)


Output:

The result of positive Infinity =  inf
The result of negative Infinity =  -inf

#### By using the decimal module in Python

The decimal module in Python can also be used to generate endless float values.
It is written as Decimal(‘Infinity’) for a positive infinite value and Decimal(‘-Infinity’) for a negative infinite value.

Approach:

• Import Decimal function from the decimal module using the import keyword.
• Pass ‘Infinity’ as an argument to the Decimal() function to define a positive infinite integer and store it in a variable.
• Print the positive Infinity.
• Pass ‘-Infinity’ as an argument to the Decimal() function to define a negative infinite integer and store it in another variable.
• Print the negative Infinity.
• The Exit of the Program.

Below is the implementation:

# Import Decimal function from decimal module using the import keyword.
from decimal import Decimal
# Pass 'Infinity' as an argument to the Decimal() function to define a positive
# infinite integer and store it in a variable
pos_infnty = Decimal('Infinity')
# Print the positive Infinity
print('The result of positive Infinity = ', pos_infnty)

# Pass '-Infinity' as an argument to the Decimal() function to define a negative
# infinite integer and store it in another variable
neg_infnty = Decimal('-Infinity')
#  Print the negative Infinity
print('The result of negative Infinity = ', neg_infnty)


Output:

The result of positive Infinity =  Infinity
The result of negative Infinity =  -Infinity

#### By using the Numpy library in Python

The Numpy module in Python can also be used to represent infinite values. It is denoted as np.inf for positive infinite value and -np.inf for negative infinite value.

Approach:

• Import numpy library as np using the import keyword.
• Get the positive Infinity value by using the np.inf and store it in a variable.
• Print the positive Infinity.
• Get the negative Infinity value by using the -np.inf and store it in another variable.
• Print the negative Infinity.
• The Exit of the Program.

Below is the implementation:

# Import numpy library as np using the import keyword.
import numpy as np
# Get the  positive Infinity value by using the np.inf and store it in a variable
pos_infnty = np.inf
# Print the positive Infinity
print('The result of positive Infinity = ', pos_infnty)

# Get the negative Infinity value by using the -np.inf and store it in another
# variable
neg_infnty = -np.inf
# Print the negative Infinity
print('The result of negative Infinity = ', neg_infnty)


Output:

The result of positive Infinity = inf
The result of negative Infinity = -inf

Python Checking to See if a Number Is Infinite

To determine whether a given integer is infinite or not, use the math library’s isinf() method, which returns a boolean result.

Approach:

• Import math module using the import keyword.
• Get the positive Infinity value by using the math.inf and store it in a variable.
• Print the positive Infinity.
• Get the negative Infinity value by using the -math.inf and store it in another variable.
• Print the negative Infinity.
• Check whether the above-obtained results are infinite or not using the isinf() functionÂ andÂ print the result.
• Give some random number as static input and store it in another variable.
• Check whether the given number is infinite or not using the isinf() function andÂ print the result.
• The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Get the  positive Infinity value by using the math.inf and store it in a variable
pos_infnty = math.inf
# Print the positive Infinity
print('The result of positive Infinity = ', pos_infnty)

# Get the negative Infinity value by using the -math.inf and store it in another
# variable
neg_infnty = -math.inf
# Print the negative Infinity
print('The result of negative Infinity = ', neg_infnty)
# Check whether the above obtained results are infinite or not using the
# isinf() function andÂ print the result.
print(math.isinf(pos_infnty))
print(math.isinf(neg_infnty))
# Give some random number as static input and store it in another variable
gvn_numb = 1000
# Check whether the given number is infinite or not using the isinf() function and
# print the result.
print(math.isinf(gvn_numb))


Output:

The result of positive Infinity =  inf
The result of negative Infinity =  -inf
True
True
False

Python Arithmetic Operations on Infinity

Adding or removing a value to or from infinity has no effect.

import math
pos_infnty = math.inf
neg_infnty = -math.inf
# Arithmetic operations
# Adding or removing a value to or from infinity has no effect.
rslt = pos_infnty + 500
print(rslt)
rslt = neg_infnty + 700
print(rslt)

rslt = pos_infnty - 500
print(rslt)
rslt = neg_infnty - 700
print(rslt)


Output:

inf
-inf
inf
-inf

Arithmetic operations involving two infinities

import math
pos_infnty = math.inf
neg_infnty = -math.inf
# Arithmetic operations involving two infinities
rslt = pos_infnty + neg_infnty
print("Addition of positive and negative infinities = ", rslt)

# Multiplication
rslt = pos_infnty * neg_infnty
print("Multiplication of positive and negative infinities = ", rslt)

# Division
rslt = pos_infnty / neg_infnty
print("Division of positive and negative infinities = ", rslt)


Output:

Addition of positive and negative infinities =  nan
Multiplication of positive and negative infinities =  -inf
Division of positive and negative infinities =  nan