cft

Infinity in Python

Python's representation of infinity and examples of how it is used


user

Ashutosh Vaidya

a year ago | 5 min read

Infinity is defined as everything that is limitless, unending, or greater than any natural number. Infinity is a topic that philosophers and mathematicians frequently debate.

But what does infinite means in programming? Anyone with even a rudimentary understanding of a programming language knows that we first encounter infinity when we do some arithmetic operations and unintentionally divide a number by zero, aha, that famous Divide By Zero error.

In the field of computer science, infinity is commonly used to,

  • Evaluate and improve the performance of large-scale computing algorithms.
  • Set the minimum and maximum values.
  • To compare finite and infinite values

In Python, you cannot express infinite as an integer since it contradicts the notion of infinity, which is also true for other programming languages such as C, C++, and Java. However, because Python is a dynamic language, float values can be used to represent an infinite integer in a code.

Let us have a look at each of the numerous ways to express infinity in Python.

By Using float('inf') and float('-inf')

This is the easiest method because we don't need to import any libraries. To accomplish this, use the code snippet below.

# Defining a positive infinite integer

positive_infinity = float('inf')

print('Positive Infinity: ', positive_infinity)

# Defining a negative infinite integer

negative_infinity = float('-inf')

print('Negative Infinity: ', negative_infinity)

# Expected Output:

# Positive Infinity: inf

# Negative Infinity: -inf

By Using Math module

Python's Math module is well-known and widely utilized for any mathematical operation we want to do. It also allows for infinity. For reference, below is a code excerpt.

import math

# Defining a positive infinite integer

positive_infinity = math.inf

print('Positive Infinity: ', positive_infinity)

# Defining a negative infinite integer

negative_infinity = -math.inf

print('Negative Infinity: ', negative_infinity)

# Expected Output:

# Positive Infinity: inf

# Negative Infinity: -inf

By Using Decimal module

Python's Decimal module also provides an infinite constant that we may make advantage of.

from decimal import Decimal

# Defining a positive infinite integer

positive_infinity = Decimal('Infinity')

print('Positive Infinity: ', positive_infinity)

# Defining a negative infinite integer

negative_infinity = Decimal('-Infinity')

print('Negative Infinity: ', negative_infinity)

# Expected Output:

# Positive Infinity: Infinity

# Negative Infinity: -Infinity

By Using Numpy module

Finally, we can utilize the ever-popular NumPy module to obtain the infinity constant for future use.

import numpy as np

# Defining a positive infinite integer

positive_infinity = np.inf

print('Positive Infinity: ', positive_infinity)

# Defining a negative infinite integer

negative_infinity = -np.inf

print('Negative Infinity: ', negative_infinity)

# Expected Output:

# Positive Infinity: inf

# Negative Infinity: -inf

Arithmetic operations on infinity

Now that we know how to define Infinity, let us go a little further into it and do some arithmetic operations on it. We are well aware that any mathematical operation is done on an infinite value will result in an endless value, whether it be addition, subtraction, multiplication, or division, for example.

The code sample below validates this idea.

# Defining a positive infinite integer

positive_infinity = float('inf')

print('Positive Infinity: ', positive_infinity)

# Defining a negative infinite integer

negative_infinity = float('-inf')

print('Negative Infinity: ', negative_infinity)

print('Arithmetic operation on Positive infinity: ')

# Multiply Positive infinity number by 5

print('Multiplication : ',positive_infinity * 5)

# Addition to Positive infinity Number

print('Addition : ',positive_infinity + 5)

# Subtraction to Positive infinity Number

print('Subtraction : ',positive_infinity - 5)

# Division to Positive infinity Number

print('Division : ',positive_infinity / 5)

print('Arithmetic operation on Negative infinity: ')

# Multiply Negative infinity number by 5

print('Multiplication : ',negative_infinity * 5)

# Addition to Negative infinity Number

print('Addition : ',negative_infinity + 5)

# Subtraction to Negative infinity Number

print('Subtraction : ',negative_infinity - 5)

# Division to Negative infinity Number

print('Division : ',negative_infinity / 5)

# Expected output

# Arithmetic operation on Positive infinity:

# Multiplication : inf

# Addition : inf

# Subtraction : inf

# Division : inf

# Arithmetic operation on Negative infinity:

# Multiplication : -inf

# Addition : -inf

# Subtraction : -inf

# Division : -inf

Checking if a number is infinite

What if you are in need to check whether a number is infinite or not, we can use the math module’s isinf() function. It returns a Boolean result, which indicates that if the specified integer is infinite, it returns true; otherwise, it returns false. See the code excerpt below.

import math

# Defining a positive infinite integer

positive_infinity = float('inf')

print('Positive Infinity: ', positive_infinity)

print('The variable is infinity = ',math.isinf(positive_infinity))

# Defining a negative infinite integer

negative_infinity = float('-inf')

print('Negative Infinity: ', negative_infinity)

print('The variable is infinity = ',math.isinf(negative_infinity))

# Expected Output:

# Positive Infinity: inf

# The variable is infinity = True

# Negative Infinity: -inf

# The variable is infinity = True

The positive infinity number is the greatest, and the negative infinity number is the smallest of all numbers.

Let us verify this with a program.

if 99999999999999999 > positive_infinity:

print('Numer is greater than Positive infinity')

else:

print('Positive infinity is greater')

if -99999999999999999 < negative_infinity:

print('Numer is smaller than Negative infinity')

else:

print('Negative infinity is smaller')

#Expected Output:

# Positive infinity is greater

# Negative infinity is smaller

Conclusion

We looked at many approaches to express infinity in Python in this tutorial. We also look at the arithmetic operation on infinity and how it relates to other natural numbers. I hope you found this tutorial useful and learned something new today.

All the code snippets from the article are available on my GitHub repo if anyone wants to play around.You can get in touch with me on my LinkedIn as well.

Upvote


user
Created by

Ashutosh Vaidya

Hi there, hello! I am Full Stack Developer, and data science and artificial intelligence explorer.


people
Post

Upvote

Downvote

Comment

Bookmark

Share


Related Articles