# Python infinity(inf)

As ironic as it may seem infinity is defined as an undefined number that can either be a positive or negative value. All arithmetic operations performed on an infinite value always lead to an infinite number, say it be sum, subtraction, multiplication, or any other operation.

In the world of computer science, infinity is generally used to measure performance and optimize algorithms that perform computations on a large-scale application.

## Representing infinity as an Integer in Python

The concept of representing infinity as an integer violates the definition of infinity itself. As of 2020, there is no such way to represent infinity as an integer in any programming language so far.

But in Python, as it is a dynamic language, float values can be used to represent an infinite integer. One can use float(‘inf’) as an integer to represent it as infinity. Below is the list of ways one can represent infinity in Python.

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

As infinity can be both positive and negative they can be represented as a float(‘inf’) and float(‘-inf’) respectively.

The below code shows the implementation of the above-discussed content:

## Python3

 `# 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)`

Output:

`Positive Infinity:  infNegative Infinity:  -inf`

### 2. Using Python’s Math Module

Python’s math module can also be used for representing infinite integers.

Pythons math.inf constant return positive infinity and -math.inf returns negative infinity.

The below code shows how it is done:

## Python3

 `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)`

Output:

`Positive Infinity:  infNegative Infinity:  -inf`

### 3. Using Python’s Decimal Module

Python’s decimal module can also be used for representing infinite float values.

It is used as Decimal(‘Infinity’) for positive and Decimal(‘-Infinity’) for negative infinite value.

The code below shows its implementation:

## Python3

 `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)`

Output:

`Positive Infinity:  InfinityNegative Infinity:  -Infinity`

### 4. Using Python’s Numpy library

Python’s Numpy module can also be used for representing infinite values. It is used as np.inf for positive and -np.inf for negative infinite value. The use of Numpy library for representing an infinite value is shown in the code below:

## Python3

 `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)`

Output:

`Positive Infinity:  infNegative Infinity:  -inf`

## Checking If a Number Is Infinite in Python

To check if a given number is infinite or not, one can use isinf() method of the math library which returns a boolean value. The below code shows the use of isinf() method:

## Python3

 `import` `numpy as np``import` `math``# Defining a positive infinite integer``a ``=` `np.inf``# Defining a negative infinite integer``b ``=` `-``np.inf``# Define a finite integer``c ``=` `300``# check if a in infinite``print``(math.isinf(a))``# check if b in infinite``print``(math.isinf(b))``# check if c in infinite``print``(math.isinf(c))`

Output:

`TrueTrueFalse`

## Comparing Infinite Values to Finite Values in Python

The concept of comparing an infinite value to finite values is as simple as it gets. As positive infinity is always bigger than every natural number and negative infinity is always smaller than negative numbers.

For a better understanding look into the code below:

## Python3

 `import` `numpy as np``# Defining a positive infinite integer``a ``=` `np.inf``# Defining a negative infinite integer``b ``=` `-``np.inf``# Define a finite + ve integer``c ``=` `300``# Define a finite -ve integer``d ``=` `-``300``# helper function to make comparisons``def` `compare(x, y):``    ``if` `x>y:``        ``print``(``"True"``)``    ``else``:``        ``print``(``"False"``)``        ` `compare(a, b)``compare(a, c)``compare(a, d)``compare(b, c)``compare(b, d)`

Output:

`TrueTrueTrueFalseFalse`

Using infinity in programming is very tricky, but Python made it very easy. Python inf can be used with more than 3 methods, which makes Python very user-friendly.

Hope you can now use infinity(inf) in Python now, and use it for solutions.

Previous
Next