In Python, infinity is termed an undefined ("indeterminate" is a better word) value that can be positive or negative.
Python newbies are sometimes clueless about the function of infinity in programming.
Infinity is quite helpful in writing algorithms to find the maximum or minimum values in an array. Besides numeric comparisons, the infinite value is invaluable in data filtering and manipulation, and running numerical simulations.
In this brief guide, we'll walk you through representing and using infinity in your Python programs.
How to Represent Infinite Numbers in Python: 4 Ways
There are four primary methods to representing infinite numbers in Python. Let's quickly see them in action:
Method #1: Using float('inf') and float('-inf')
The first method involves utilizing the float() function to represent positive and negative infinity. To represent positive infinity, you can pass 'inf' as the argument, while '-inf' represents negative infinity.
pos_inf = float('inf') print('Positive Infinity:', pos_inf) neg_inf = float('-inf') print('Negative Infinity:', neg_inf)
The output of this code is:
Positive Infinity: inf Negative Infinity: -inf
Method #2: Using the math Module
Python's math module offers another way to represent infinity.
You can use the attributes math.inf and -math.inf to denote positive and negative infinity, respectively.
However, you can only use the math module to represent infinity if you're using Python 3.5 or above.
Here's a Python program using the math module to represent infinity:
import math pos_inf = math.inf print('Positive Infinity:', pos_inf) neg_inf = -math.inf print('Negative Infinity:', neg_inf)
As you might have guessed, the output of this code is:
Positive Infinity: inf Negative Infinity: -inf
Method #3: Using the decimal Module
For working with infinite numbers, you can also make use of Python's decimal module. To represent positive infinity, you can employ Decimal('Infinity'), while Decimal('-Infinity') represents negative infinity.
from decimal import Decimal pos_inf = Decimal('Infinity') print('Positive Infinity:', pos_inf) neg_inf = Decimal('-Infinity') print('Negative Infinity:', neg_inf)
Here's the output:
Positive Infinity: Infinity Negative Infinity: -Infinity
Method #4: Using the Built-in numpy Library
If you're dealing with numerical computations, the numpy module provides numpy.inf and -numpy.inf as representations of positive and negative infinity, respectively.
import numpy as np pos_inf = np.inf print('Positive Infinity:', pos_inf) neg_inf = -np.inf print('Negative Infinity:', neg_inf)
The output is:
Positive Infinity in Python: inf Negative Infinity in Python: -inf
Now that we know how to represent infinity in Python, let's explore how mathematical operations behave when dealing with infinite values.
Handling Mathematical Operations with Infinity in Python
When performing mathematical operations involving infinity, we can encounter two scenarios: operations with two infinite values and operations with one infinite value and one finite value.
Operations with Two Infinite Values
#1 Addition
int1 = float('inf') int2 = float('-inf') print("Adding two positive infinities:", int1 + int1) print("Adding two negative infinities:", int2 + int2) print("Adding positive and negative infinity:", int1 + int2)
Have a look at the output:
Adding two positive infinities: inf Adding two negative infinities: -inf Adding positive and negative infinity: nan # "nan" means Not A Number
#2 Subtraction
a = float('inf') b = float('-inf') print("Subtracting two positive infinities:", a - a) print("Subtracting two negative infinities:", b - b) print("Subtracting positive and negative infinity:", a - b)
The output is:
Subtracting two positive infinities: nan Subtracting two negative infinities: nan Subtracting positive and negative infinity: inf
#3 Multiplication
a = float('inf') b = float('-inf') print("Multiplying two positive infinities:", a * a) print("Multiplying two negative infinities:", b * b) print("Multiplying positive and negative infinity:", a * b)
Here's the output:
Multiplying two positive infinities: inf Multiplying two negative infinities: inf Multiplying positive and negative infinity: -inf
#4 Division
a = float('inf') b = float('-inf') print("Dividing two positive infinities:", a / a) print("Dividing two negative infinities:", b / b) print("Dividing positive and negative infinity:", a / b)
The output is:
Dividing two positive infinities: nan Dividing two negative infinities: nan Dividing positive and negative infinity: nan
These examples of operations with infinity make it evident how Python's outputs could have very interesting implications in data science.
For instance, in mathematical modeling and simulations, when dealing with limit calculations or asymptotic behavior, the concept of infinity becomes essential.
Mathematical Operation with One Infinity in Python and One Finite Value
Let's now dive into the mathematical operations involving one infinity and one finite value:
#1 Addition
a = float('inf') b = float('-inf') finite_number = 10 print("Adding positive infinity with a finite number:", a + finite_number) print("Adding negative infinity with a finite number:", b + finite_number)
Here's the output:
Adding positive infinity with a finite number: inf Adding negative infinity with a finite number: -inf
As you can see, adding a finite number to infinity in Python results in infinity.
#2 Subtraction
a = float('inf') b = float('-inf') finite_number = 5 print("Subtracting positive infinity with a finite number:", a - finite_number) print("Subtracting negative infinity with a finite number:", b - finite_number)
The output is:
Subtracting positive infinity with a finite number: inf Subtracting negative infinity with a finite number: -inf
Subtracting a finite number from infinity in Python also yields infinity.
#3 Multiplication
a = float('inf') b = float('-inf') finite_number = 3 print("Multiplying positive infinity with a finite number:", a * finite_number) print("Multiplying negative infinity with a finite number:", b * finite_number)
The output of this code is:
Multiplying positive infinity with a finite number: inf Multiplying negative infinity with a finite number: -inf
When multiplying infinity by a finite number, the result is infinity.
#4 Division
a = float('inf') b = float('-inf') finite_number = 2 print("Dividing positive infinity by a finite number:", a / finite_number) print("Dividing negative infinity by a finite number:", b / finite_number)
Here's the output of this code:
Dividing positive infinity by a finite number: inf Dividing negative infinity by a finite number: -inf
Dividing infinity by a finite number in Python results in infinity.
Conclusion
When performing mathematical operations with one infinity and one finite value in Python, the result is consistently infinity.
This behavior can be beneficial in certain scenarios where you need to handle calculations involving infinite values.
Remember to choose the appropriate finite number for your specific use case. Happy coding!