To use negative infinity in Python and create variables which represent negative infinity, there are four different ways.

The easiest way to get negative infinity in Python is with the **float()** function.

```
negative_infinity = -float('inf')
print(negative_infinity)
#Output:
-inf
```

Another way you can represent negative infinity in Python is with the math module.

```
import math
negative_infinity = -math.inf
print(negative_infinity)
#Output:
-inf
```

Another way you can represent negative infinity in Python is with the decimal module.

```
import decimal
negative_infinity = -decimal.Decimal("inf")
print(negative_infinity)
#Output:
-inf
```

One final way to represent negative infinity is with the numpy module.

```
import numpy as np
negative_infinity = -np.inf
print(negative_infinity)
#Output:
-inf
```

Each of these methods create variables which are equal to negative infinity and they are all have the same representations in Python.

```
import math
import decimal
import numpy as np
print(-float('inf') == -math.inf == -decimal.Decimal('inf') == -np.inf)
#Output:
True
```

## How to Use Negative Infinity in Python

After you’ve initialized a variable and assigned it a value of negative infinity with one of the four methods shown above, you can use negative infinity just like any other number in Python.

For example, you can perform any of the basic arithmetic operations such as addition, subtraction, division or multiplication.

You can also do things such as comparing negative infinity with other numbers or finding the maximum or minimum of numbers including infinity.

```
negative_infinity = -float('inf')
print(negative_infinity + 100)
print(negative_infinity - 100)
print(negative_infinity * 100)
print(100 / negative_infinity)
print(max(negative_infinity, 100))
print(min(negative_infinity, 100))
#Output:
-inf
-inf
-inf
-0.0
100
-inf
```

## Using Positive Infinity in Python

If you want to use infinity in Python, then don’t multiply by -1 and you will get positive infinity.

For example, if you want to use the **float()** function to create infinity, then don’t multiply by -1.

Below shows a simple example of how you can get positive infinity in Python.

```
infinity = float('inf')
print(infinity)
#Output:
inf
```

Then after you’ve created a variable representing infinity, you can use it just like negative infinity.

Hopefully this article has helped you understand how to create variables representing negative infinity in Python.

## Leave a Reply