# Python Infinity: Representation and Usage

Can you think of a value that can neither be defined nor estimated but is so crucial to the programming world? Yes, you got it right! Its infinity. But do you know that until 2020 there was no proper way to represent infinity in python? We generally used a larger/small value or a garbage value to represent infinity.

Python Infinity is a variable type that is greater than every other numeric value in python. Rather than using any garbage large value as a limit, you can use pre-defined keyword for initializing an infinite value in Python.

But now we have many different ways to represent infinity. In this article, we will understand why it is essential in Computer Science and how to express it in python.

Contents

## How to Represent Infinity in Python?

Infinity can be positive or negative. The result of any operation of any with infinity like sum, product, the division is always infinity. One more important thing is that infinity is still a float value. There is no way to represent infinity as an integer. So, let us now know about all the methods we can represent positive and negative infinity.

### a. Using float()

Python is a dynamic language, that is why we can use float() to represent infinity.

```positive_inf=float('inf')
print("positive_inf:",positive_inf)

# For negative infinity, put minus in front of positive infinity
negative_inf=-float('inf')
print("negative_inf:",negative_inf)
```
```positive_inf: inf
negative_inf: -inf```

### b. Using the math module to represent infinity in python

We can use the math module to represent infinity.

```import math
# We can use the inf method of the math module
pos_infinity=math.inf
print(pos_infinity)

neg_infinity=-math.inf
print(neg_infinity)
```
```inf
-inf```

### c. Using the Decimal module

```import decimal
# Using the Decimal function of decimal module
positive_inf=decimal.Decimal("inf")
negative_inf=-decimal.Decimal("inf")
print(positive_inf)
print(negative_inf)
```
```Infinity
-Infinity```

### d. Using the sympy module

```from sympy import oo
infinity=oo
minus_infinity=-oo
print(infinity)
print(minus_infinity)
```
```oo
-oo```

### e. Using the Numpy Module

For using the numpy library, we first need to import it using –

`pip install numpy `
```# importing the numpy library and giving alias np
import numpy as np
infinity=np.inf
negative_infinity=-np.inf
print(infinity)
print(negative_infinity)
```
```inf
-inf```

## Performing Operations on Infinity in python

### a. Adding and Subtracting a number.

```import math
infinity = math.inf
val=1000
print(infinity+val)
print(infinity-val)
```
```inf
inf```

Whatever number we add or subtract from infinity, we will get infinity.

### b. Multiply and Divide some number with Infinity

```import math
infinity = math.inf
val=1938
print(infinity*val)
print(infinity/val)
```
```inf
inf```

### c. Finding Remainder of a infinity when divides with a number

```import math
infinity = math.inf
val=121212
print(infinity%val)
```
`nan`

### d. Checking if a number is infinity using math.isinf()

We can check if a number is infinity using the isinf() method of math module.

```import math
from sympy import oo
infinity=oo
print(math.isinf(infinity))
```
`True`

### e. Are all infinity same?

Let us now check whether the values from each methods are same or not?

```import math
from decimal import Decimal
import numpy as np
from sympy import oo

infinity=oo
inf_floats=float('inf')
inf_math=math.inf
inf_Decimal=Decimal("inf")
inf_np=np.inf

print(inf_floats==inf_math)
print(inf_floats==inf_Decimal)
print(inf_floats==inf_np)
print(inf_floats==infinity) # infinity value from sympy is not equal to other infinity values
```
```True
True
True
False```

### f. Comparing the Values of python infinity with other values using python max function

Positive Infinity is the largest number possible and negative infinity is the smallest number. Let us compare some values with infinity and check whether it is true or not.

```import math
print(max(math.inf,9999))
print(min(-math.inf,9999))
```
```inf
-inf```

We can also do something like this-

```import math

large_value=9999999999
infinity=math.inf
if infinity>large_value:
print("Infinity is the largest")
else:
print(large_value,"is larger")

small_value=-9999999999
neg_infinity=-math.inf
if neg_infinity<large_value:
print("Negative Infinity is the smallest")
else:
print(large_value,"is smaller")
```
```Infinity is the largest
Negative Infinity is the smallest```

### g. Changing the type of python infinity into integer

Let us try to change the type from float into integer and see what happens.

```import math
infinity=math.inf
int_infinity=int(infinity)
print(int_infinity)
```
`Error: cannot convert float infinity to integer`

## Conclusion

The use of infinity in computer science is great. When we want to compare a number with a very large or small number, we generally use infinity. Not only this, it can be used to measure the performance of different algorithms. It is generally used when we perform computations on a very large scale.

Try to run the programs on your side and let us know if you have any queries.

Happy Coding!

Subscribe
Notify of 