How to represent an infinite number in Python?

hrishikesh1990

hrishikesh1990

Posted on March 21, 2022

How to represent an infinite number in Python?

In this tutorial, we will learn how to represent an infinite number in Python. As we know Infinity is defined as an undefined value that can either be a positive or a negative value. All arithmetic operations, let it be addition, subtraction, division, multiplication, etc. performed on an infinite value will always lead to an infinite number.

Table of contents - Python Infinity

Python Infinity

Infinity is defined as something that has no end, therefore is not represented as an integer. We know that all arithmetic operations performed on an infinite value will give an infinite value. It is represented as a float value. So, Let's get to know about all the methods for representing both positive and negative infinite value.

The reason why the infinity is not an int data type, but a float data type, is dependent on the way numbers are represented in Python. An integer number is represented using its binary representation, for example the value 7 is represented as 0111.

The float numbers, however, are represented with 3 components –

The sign: This is as simple as the name. 0 represents a positive number while 1 represents a negative number.

The mantissa: The mantissa is part of a number in scientific notation or a floating-point number, consisting of its significant digits. Here we have only 2 digits, i.e. O and 1. So a normalised mantissa is one with only one 1 to the left of the decimal.

The exponent: The exponent field needs to represent both positive and negative exponents. A bias is added to the actual exponent in order to get the stored exponent

This is in accordance with the IEEE 754 standard for storing floating-point numbers. The standard reserves some values to represent special numbers. And one of these special numbers is infinity.

According to this standard, a floating-point number represents an infinity when all the bits in the exponent part are 1, and all the bits in the mantissa part are 0. Additionally, if the sign bit is 0, it is positive infinity, while a 1 in the sign bit denotes a negative infinity.

Since infinity is a special value that cannot be represented using simple binary representation. Instead its datatype is float in Python.

Using float to represent infinity in Python

Since Infinite numbers are both positive and negative, Therefore, in Python, they can be represented using float(‘inf’) and float(‘-inf’).

Input:

#Positive Infinity
positive_infinity = float('inf')
print ('Positive Infinity: ', positive_infinity)

#Negative Infinty
negative_infinity = float('-inf')
print ('Negative Infinity: ', negative_infinity)
Enter fullscreen mode Exit fullscreen mode

Output:

Positive Infinity: inf
Negative Infinity: -inf
Enter fullscreen mode Exit fullscreen mode

Using Python's math module to represent infinity

We can use the math module to represent an infinite value but it only works with 3.5 or a higher version of python. As infinite can be both positive and negative, it is represented as math.inf and -math.inf respectively.

Input:

import math

#Positive Infinity
positive_infinity = math.inf
print ('Positive Infinity: ', positive_infinity)

#Negative Infinty
negative_infinity = -math.inf
print ('Negative Infinity: ', negative_infinity)
Enter fullscreen mode Exit fullscreen mode

Do not forget to import the math module into your code because it will not work unless the math module is present.

Output:

Positive Infinity: inf
Negative Infinity: -inf
Enter fullscreen mode Exit fullscreen mode

Using Python's decimal module to represent infinity

In order to represent infinite using the decimal module, we use Decimal(‘Infinity’) for positive Infinite and Decimal(‘-Infinity’) for, as you guessed, negative Infinite.

Input:

from decimal import Decimal

#Positive Infinity
positive_infinity = Decimal('Infinity')
print ('Positive Infinity: ', positive_infinity)

#Negative Infinty
negative_infinity = Decimal('-Infinity')
print ('Negative Infinity: ', negative_infinity)
Enter fullscreen mode Exit fullscreen mode

Output:

Positive Infinity: inf
Negative Infinity: -inf
Enter fullscreen mode Exit fullscreen mode

Using the NumPy library for Python infinity

The NumPy module is another way to represent infinite in Python where np.inf and -np.inf represents positive and negative infinite respectively.

Input:

from numpy as np

#Positive Infinity
positive_infinity = np.inf
print ('Positive Infinity: ', positive_infinity)

#Negative Infinty
negative_infinity = -np.inf
print ('Negative Infinity: ', negative_infinity)
Enter fullscreen mode Exit fullscreen mode

It will not work unless The NumPy library is present in your code. So do not forget to import it.

Output:

Positive Infinity: inf
Negative Infinity: -inf
Enter fullscreen mode Exit fullscreen mode

Checking if a number is infinite in Python

To check whether the given number is infinite or not, we can use the math module's isinf() function. It returns a boolean value which means if the given number is infinite it returns true and returns false if the number is not.

Input:

import decimal from Decimal
import math

#defining a positive infinte, a neagative infinte and a finite integer
a = Decimal('Infinity')
b = Decimal('-Infinity')
c = 1000

#checking if the number is infinite or not
print(math.isinf(a))
print(math.isinf(b))
print(math.isinf(c))
Enter fullscreen mode Exit fullscreen mode

Output:

TRUE
TRUE
FALSE
Enter fullscreen mode Exit fullscreen mode

Arithmetic operations on an infinite number

As infinity is a float value, one can perform various arithmetic operations on it. The results of these operations are also defined by the IEEE standard.

Input:

# Define positive infinity value
a = float('inf')

Define Negative infinity value

b = float('-inf')

Operations on positive infinity value

print('For Positive Infinity Value:')
print('Addition value : ',a + 7)
print('Subtraction value : ',a - 7)
print('Multiplication value: ',a * 7)
print('Division value: ',a / 7)
print("-----------------------------------")

Operations on negative infinity value

print('For Negative Infinity Value:')
print('Addition value: ',b + 14)
print('Subtraction value: ',b - 14)
print('Multiplication value: ',b * 14)
print('Division value: ',b / 14)

Enter fullscreen mode Exit fullscreen mode




Output:

For Positive Infinity Value:
Addition value : inf
Subtraction value : inf
Multiplication value: inf

Division value: inf

For Negative Infinity Value:
Addition value: -inf
Subtraction value: -inf
Multiplication value: -inf
Division value: -inf

Enter fullscreen mode Exit fullscreen mode




Closing thoughts

Infinite is the concept of something that is unlimited, endless, without bound. As a result, Hermann Weyl wrote a book, "Levels of Infinity" in which he says, "Mathematics is the science of the infinite" and can be read online. Python has other great concepts, and one can read about them here.

P.S. If you are looking to hire a developer for your team and are not sure how to go about with writing a job description, check out the following pages for easy tips:

  1. Android JD
  2. Dot-net
💖 💪 🙅 🚩
hrishikesh1990
hrishikesh1990

Posted on March 21, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related