×

Python Infinity

Introduction

We will discover how to define an infinite number in Python in this tutorial. Infinity, as we all know, is a value that cannot be defined and can either be positive or negative. Any arithmetic operation done on an infinite value, including addition, subtraction, multiplication, and division, will always result in an infinite number.

Python Infinity

Because it has no beginning and hence cannot be represented by an integer, infinity. We are aware that every operation in mathematics that is done on an infinite value will result in an infinite value. The value is displayed as a float. Let's learn about all the ways to represent infinite values, both positive and negative.

The way that numbers are written in Python plays a role in why the infinity data type is a float rather than an int. A binary representation of an integer number is used to represent it; for instance, the value 7 is written as 0111.

However, the float numbers are represented by three components:

The sign:

It's as straightforward as the name. The representation for a positive number is 0, while the symbol for a negative number is 1.

The mantissa:

The set of significant digits in a floating-point integer or a scientific notation is known as the mantissa. Here, the only numbers present are O and 1. A mantissa that has just one 1 to the left of the decimal is said to be normalized.

The exponent:

The exponent field must contain both +ve (positive) and -ve (negative) exponent representations. The real exponent is combined with a bias to get the stored exponent.

This complies with the IEEE 754 specification for a means of storing floating-point numbers. Some values are set aside in the standard to represent special numbers. Infinity is one of these unique numbers.

This standard states that a floating-point integer represents infinity when all of the bits in the exponent and mantissa parts are 1, respectively. Additionally, a sign bit of 1 indicates a negative infinity, while a sign bit of 0 indicates positive infinity.

Since infinity is a unique value, it cannot be represented in a straightforward binary manner. In Python, it has the float datatype instead.

1. Using float(‘inf’) and float(‘-inf’):

Infinity can be expressed as a float('inf') or float('-inf') depending on whether it is positive or negative. The implementationof the previously discussed content is shown in the code below:

Code:

# Defining an infinite +ve number
positive__infinity = float('inf')
print ('Positive Infinity: ', positive__infinity)


# Defining an infinite -ve number
negative__infinity = float('-inf')
print ('Negative Infinity: ', negative__infinity)

Output:

Positive Infinity:  inf
Negative Infinity:  -inf

2. Using Python’s math module:

Python's math library can also be used to represent infinite integers. The code below demonstrates how to do it:

Code:

import math


# Defining an infinite positive number
positive__infinity = math.inf
print ('Positive Infinity: ', positive__infinity)


# Defining an infinite negative number
negative__infinity = -math.inf
print ('Negative Infinity: ', negative__infinity)

Output:

Positive Infinity:  inf
Negative Infinity:  -inf

3. Using Python's decimal module:

The Python decimal module can also be used to express infinite float values.Decimal('Infinity') and Decimal('-Infinity') are used to represent positive and negative infinite values, respectively.

Its implementation is shown in the code below:

Code:

from decimal import Decimal


# Defining an infinite positive number
positive__infinity = Decimal('Infinity')
print ('Positive Infinity: ', positive__infinity)


# Defining an infinite negative number
negative__infinity = Decimal('-Infinity')
print ('Negative Infinity: ', negative__infinity)

Output:

Positive Infinity:  Infinity
Negative Infinity:  -Infinity

4. Utilizing the Numpy library for Python

Infinite values can also be represented using the Numpy module in Python. For positive and negative infinite values, it is used as np.inf and -np.inf, respectively. The code below demonstrates how to express an infinite value using the Numpy library:

Code:

import numpy as np


# Defining an infinite positive number
positive__infinity = np.inf
print ('Positive Infinity: ', positive__infinity)


# Defining an infinite negative number
negative__infinity = -np.inf
print ('Negative Infinity: ', negative__infinity)

Output:

Positive Infinity:  inf
Negative Infinity:  -inf

5. Python Code to Determine whether a Number Is Infinite

The math library's isinf() function, which returns a boolean value, can be used to determine whether a given number is infinite or not. The isinf() method is used in the code below:

Code:

import numpy as np
import math


# Defining an infinite positive number
x = np.inf


# Defining an infinite negative number
y = -np.inf


# Define a finite integer
z = 200


# Check if an in infinite
print (math.isinf(x))


# Check if b in infinite
print (math.isinf(y))


# Check if c in infinite
print (math.isinf(z))

Output:

True
True
False

6. Procedures in arithmetic on an infinite number

Since infinity is a float value, it can be subjected to a variety of mathematical operations. The IEEE standard also defines the outcomes of these procedures.

Code:

# Define positive x value for infinity.
x = float('inf')


# Define negative x value for infinity.
y = float('-inf')


# Working with positive infinity values
print ('For Positive Infinity Value:')
print ('Addition value : ', x + 6)
print ('Subtraction value : ', x - 6)
print ('Multiplication value: ', x * 6)
print ('Division value: ', x / 6)
print ("--------------------------------------------")
# Working with negative infinity values
print ('For Negative Infinity Value:')
print ('Addition value: ', y + 12)
print ('Subtraction value: ', y - 12)
print ('Multiplication value: ', y * 12)
print ('Division value: ', y / 12)

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

7. Python comparison of infinite and finite values

It doesn't get any easier than comparing infinite values to finite values. Like how negative infinity is always lower than negative numbers and positive infinity is always greater than any natural integer. Consider the following code for a better understanding:

Code:

import numpy as np


# X is defined as a positive, infinite integer.
x = np.inf


# y is defined as a positive, infinite integer.
y = -np.inf


# Define finite z positive integer 
z = 200


# Define finite z negative integer
p = -200


# Helper method to make comparisons
def compare (x, y):
 if x>y:
  print ("True")
 else:
  print ("False")


compare (x, y)
compare (x, z)
compare (x, p)
compare (y, z)
compare (y, p)

Output:

True
True
True
False
False

Related Topics

Python String endswith() method

Python String endswith() method The string.endswith() method in Python returns a Boolean value True if the string ends with the specified suffix, otherwise it returns False. Syntax endswith(suffix[, start[, end]]) Parameter suffix – This parameter represents a string or tuple...

2 minutes read.

How to Reverse a number in Python?

In Python, conditional statements can be combined with the list() method, slice() method, recursion() method, and other predefined techniques to generate reverse logical functions. The reverse() method is the most...

7 minutes read.

How to convert Float to Int in Python?

A float value can be converted to an int via type conversion, an explicit method of transforming an operand to a certain type. But it must be noted that such...

3 minutes read.

Features of Python

Python is a powerful, easy to learn, popular programming language. It has effective data structures and its elegant syntax makes it user-friendly language. Below are the key features of Python: 1. Python...

4 minutes read.

Python for loop

 Python for loop A for loop in Python executes a block of code for a specified number of times, based on a given sequence. The for loop in Python is different than any other...

3 minutes read.

Closest Pair of Points in Python

We are given an array of n points in the plane, and our task is to find the pair of points in the array that are the closest to each...

3 minutes read.

List Subtract in Python

The List is one of the most unique Data Structures in Python. It is generally used to store multiple values in just one single variable. It is one of the...

4 minutes read.

What does the if __name__ == "__main__" do in Python

In this article, you will learn about the If__name__==__main__ is a statement in python to define modules and the names of the modules. This statement plays a vital role in...

3 minutes read.

How to check data type in python

The data type represents the nature of a variable; it determines what kind of data it can store and what operations can be carried out on it. There are five...

4 minutes read.

Palindrome program in Python

Palindrome program in python A number or string is said to be a palindrome if we invert the number or string and the string or number remains the same as the...

3 minutes read.

Python Break Statement

In Python, loops are used to automate and repeat processes in an effective manner. However, there may be occasions when you wish to entirely exit the loop, skip an iteration,...

2 minutes read.

Python locals() function

Python locals() function The locals() function in Python updates and returns a dictionary representing the current local symbol table. Syntax locals() Parameter NA Return This function returns the local symbol table as a dictionary or returns free...

1 minute read.

Wikipedia module in Python

Wikipedia module in Python: The internet has become an essential part of all of our life which is the largest source of all the information. Therefore, it becomes very important...

7 minutes read.

Python Matrix Operations

Python Matrix In this section of the Python tutorial, we will look at the introduction of the matrix in Python programming. We will perform many operations on the Python matrix using...

21 minutes read.

Math Module in Python

In this article, you are going to learn everything in detail about the “ math “ module in Python. We can normally work with general operations using python without importing or...

16 minutes read.

Face Recognition in Python

In this tutorial, we will understand what is face recognition and how it is achieved in python. Face recognition is of great utility in real-world scenarios. It is an extended step...

3 minutes read.

Tensorflow Angular in Python

TensorFlow is an open-source, Python-compatible toolkit for numerical computation that accelerates and simplifies the creation of neural networks and machine learning algorithms. They make the interesting notion that models can be...

6 minutes read.

Python program for perfect number

Python program for perfect number Before writing any program for a given problem, we have to understand the problem for which we are creating a solution program. So, let's understand what...

2 minutes read.

Amazon rekognition using python

Amazon recognition service is an AI service which is an image labelling API (Application programming interface). In this article, we shall learn to use the AWS python boto3 module to...

3 minutes read.

Enumerate() Function in Python

The enumerate() function in Python is used to convert a data collection object into an enumerate object. It returns an object that contains a counter as a key for each...

3 minutes read.