Infinity is a term used to describe the uncountable size, length in life, and programming languages. Programming languages provide from simple to complex data structures in order to represent numbers like integer, decimal, float, double, positive, negative, etc. but infinity or an infinite number is a special case. In this tutorial, we will examine the infinity and infinite numbers for Python programming language by asking how to create, define, compare, calculate infinite variables.
What Is Infinite Number or Infinity?
When we talk about numbers we know that all numbers are precisely countable and represented easily with numbers like 1, 2, 3, 10.5, etc. But the infinite number is different from all other numbers and can not be counted according to its definition. Infinity or infinite number can be only represented and can not be counted or used like regular numbers for mathematical operations. Like positive and negative numbers there is positive infinity which is lower than all countable numbers and positive infinity which is higher than all countable numbers. Negative infinity is represented with -∞ and positive infinity is represented with +∞.
Representing Infinity In Python
Even the term infinity seems a bit undefinable as a popular, dynamic programming language Python provides different methods to define and use infinite values and variables. Below you can find the methods to define infinity in Python. Infinity is defined with the IEEE-754-1985 standard and provided by most of the modern programming languages. According to this standard, the data or variable type of the infinity or infinite number should be float type.
- Represent Infinity with Float
- Represent Infinity with Decimal
- Represent Infinity with math.inf
- Represent Infinity with NumPy
- Represent Infinity with SumPy
- Represent Infinity with Kvesteri Infinity Class
Create Infinite Variable/Value with float() Function
Positive and negative infinite number or infinity can be defined and represented by using the float data type. We will just use the float() function and provide the definition string inf for positive infinity and -inf for negative infinity.
# positive infinity positive_inf = float("inf") # negative infinity negative_inf = float("-inf") print(positive_inf) # inf print(negative_inf) # -inf print(type(positive_inf))
Create Infinite Variable/Value with Decimal
Another data type to create or represent positive and negative infinite is decimal in Python. The creation of the infinity with the decimal type of very similar to the float type. We will provide the Infinity for positive infinity and -Infinity for negative infinity. In order to use the Decimal type, you should import it from the decimal module like below.
from decimal import Decimal positive_infinity = Decimal("Infinity") negative_infinity = Decimal("-Infinity") print(positive_infinity) # The output is "Infinity " print(negative_infinity) # The output is "-Infinity" type(positive_infinity) # The output is "<class 'decimal.Decimal'>"
Create Infinite Variable/Value with math.inf
Python provides the math module for advanced mathematical calculations and definitions. Infinity also provided by the Python math module. Both positive and negative infinity can be represented with the math module math.inf and -math.inf . But in order to use math module infinity, you should import the math module like below.
import math positive_infinity = math.inf negative_infinity = -math.inf print(positive_infinity) # The output is "inf" print(negative_infinity) # The output is "-inf" type(positive_infinity) # The output is "<class 'float'>" type(negative_infinity) # The output is "<class 'float'>"
You can see from the type() function output that the math.inf is the same with the float type infinity and derived from it.
Create Infinite Variable/Value with NumPy
NumPy is a popular Python module that provides extensive functionality to work with numbers. As a number infinity can be represented and created with the NumPy module. NumPy provides positive infinity with numpy.inf and negative infinity with -numpy.inf. NumPy is an external module which is provided by 3rd party package provide PyPy. It is not installed by default but can be easily installed with the package manage or pip command like below.
$ pip3 install numpy
Now you can use the positive and negative infinity with NumPy.
import numpy positive_infinity = numpy.inf negative_infinity = -numpy.inf print(positive_infinity) #The output is "inf" print(negative_infinity) #The output is "-inf" type(positive_infinity) #The output is "<class 'float'>" type(negative_infinity) #The output is "<class 'float'>"
Check If Infinite Number or Infinity
Python math module provides the isinf() function in order to check the given value, variable, or number if it is infinite. It will check if the given number positive or negative infinite and returns true if it is negative or positive infinite. You should import the math module for this operation.
import math a = float("inf") b = float("-inf") c = 99999999 d = -9999999999 print(math.isinf(a)) #The output is "True" print(math.isinf(b)) #The output is "True" print(math.isinf(c)) #The output is "False" print(math.isinf(d)) #The output is "False"
Compare Different Infinite Value Presentations
Let’s compare different infinite numbers of presentations with each other by using the if-else conditionals. We will create infinite variables with different methods and compare them with each other.
import math import decimal a = float("inf") b = decimal.Decimal("Infinity") c = math.inf print(a==b) #The out is "True" print(a==c) #The out is "True" print(b==c) #The out is "True"
Calculations with Infinite Values
Calculations with the infinity or infinite value are based on Mathematical theories. Simply when we sum infinite with 5 results will be infinite. We can make most of the mathematical calculations with infinite numbers. Let’s look at the examples.
import math a = float("inf") b = float("inf") c = float("-inf") print(a+5 == b) # True print(a-5 == b) # True print(a == c) # False print(a > c) # True print(a/b) # nan print(a/c) # nan print(a*b) # inf print(a*b == b) # True
Iterating To Infinity with For and While Loops
Infinity represents unaccessible but can we iterate to the infinite. This is also called an infinite loop. Using for loop you can not iterate to the infinity because the iteration works with integer types but the infinite type is float. Even you try different methods there is no feasible way to iterate with for loop. You will get the following errors if you try to use for to iterate infinity.
for x in range(0,int(math.inf)): print("I go to the infinite...") #Traceback (most recent call last): # File "<stdin>", line 1, in <module> #OverflowError: cannot convert float infinity to integer for x in range(0,math.inf): print("I go to the infinite...") #Traceback (most recent call last): # File "<stdin>", line 1, in <module> #TypeError: 'float' object cannot be interpreted as an integer
You can use the while loop in order to iterate to the infinity because we will simply compare given finite numbers with the infinite number and iterate to the next step which will continue indefinitely unless we cancel the process.
x = 0 while x < math.inf: print("I go to the infinite...")
Infinite vs NaN
NaN or nan is a data type like float, int, list, etc in Python. NaN is simply created to store nothing and no value. In some cases, it may be confused with the infinite number but they are completely different. Infinity or infinite number is a float type number that can be counted but NaN is not a data or empty type. So they are not the same and comparing the infinite number and Nan type will return a false value. For example, we can use an infinite number with while iteration in a countable way but can not use NaN in a countable way.
import math print(math.inf == math.nan) #The output is "False" print(math.inf != math.nan) #The output is "True"