×

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 hasattr() function

Python hasattr() function The hasattr() function in Python returns a Boolean value ‘True’ if the given object has the specified attribute, else it returns False. Syntax hasattr(object, name) Parameter object: it is a required parameter which represents an object. attribute:...

1 minute read.

Python String rindex() method

Python String rindex() method The string. rindex() method in Python returns the highest index of the substring inside the string (if found). If the substring is not found, it raises an...

2 minutes read.

Data Drop in Python

Introduction You'll understand how to delete a group of rows from a Pandas dataframe in this article.You can read this article on How to Drop Columns in Pandas to find out...

6 minutes read.

Python Logical Operators

In python, there are many operators which we use in our program. Operators are used in manipulating a particular value or operand. Logical operators are used mainly to evaluate an...

7 minutes read.

AX Contour in Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Unit Testing in Python

The process of testing whether a particular unit is working properly or not is called “UNIT TESTING”. A unit test will check small components in your application. The first and...

7 minutes read.

Python exe() function

Python exe() function The exec() function in Python executes the specified Python code and accepts large blocks of code. It supports dynamic execution of Python code where the object must be either a string...

1 minute read.

What is online python free IDE

An IDE is an acronym that stands for “Integrated development environment.” It is a software environment that has a software development package that consists of the code editor and builds...

4 minutes read.

Python lock

Before understanding the concept of Python-lock we need to understand what kind if condition we require to implement the locks in Python. Let us start with multithreading and its implementation...

5 minutes read.

Python String rstrip() method

Python String rstrip() method The string.rstrip() method in Python returns a copy of the string with trailing characters removed. Syntax string.rstrip([chars]) Parameter chars:  This argument represents a string specifying the set of characters to be...

1 minute read.

Decision Tree in Python

Decision Tree is one of the most essential algorithms in the area of machine learning for classification and regression. But let us first talk about the lifespan of every machine learning...

12 minutes read.

Python list() | List class in Python

The list() class in Python creates a list object where the list object is a collection which is ordered and changeable. Syntax class list([iterable]) Parameter Iterable: It is a required parameter which represents a sequence, collection...

1 minute read.

Python program to find Fibonacci series

Python program to find Fibonacci series A Fibonacci series is an integer sequence of 0, 1, 1, 2, 3, 5, 8.... We can identify the Fibonacci series as any number sequence...

2 minutes read.

Python Data Types

Python Data Types: The variable in Python is used to store values, and they can hold different values. Each variable in Python has its own data-type. Since Python is a...

13 minutes read.

Python Dictionary copy() method

Python Dictionary copy() method The dictionary.copy () method in Python returns a copy of the specified dictionary. Syntax dictionary.copy () Parameter NA Return None Example 1 # Python program explaining # the dictionary.copy() method # initialising the dictionary ...

1 minute read.

Screen Rotation app Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

3 minutes read.

Struct Module in Python

What is a structure in Python? An entity that holds data in form of a structure is called a data structure. In Python, the data structure includes tuples, lists, dictionaries, and...

4 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.

How to Practice Python Programming

Learning python kkis a step towards coding. Python gets one closer to programming languages. It is essential to practice every programming language to become a professional in coding. It is...

4 minutes read.

Python Program for Linear Search

Introduction We employ specific algorithms to efficiently carry out our responsibilities, such as searching for an element in a given data structure. These algorithms fall under the category of searching algorithms....

4 minutes read.