How to check if value is infinite in Python

In this post we are going to learn how to check if value is infinite in Python with the help of math module Math.isinf() and NumPy library functions np.isinf(),np.isneginf(),np.np.isposinf() using nan.The infinite value is also known by the names Inf, Infinity, PINF,infty in Python. The infinite values are positive or negative infinite values represented by numpy library attributes np.inf,-np.inf or math module attribute math.inf.To use the numpy library function first we have to install it on our system

How to check if value is infinite in Python

• using Math.isinf()
• using np.isinf()
• using np.isneginf()
• Using np.isposinf()

1. Check if the value is infinite in Python using Math.isinf()

The math module’s isinf() method checks for infinite value. It does not check for positive and negative infinite values separately but by writing some code we can check for negative infinite values too. We have defined a function that checks for given values are positive or negative infinite and prints the result True if it is infinite and False if not.

```import math

def check(val):

if(math.isinf(val) and val > 0):
print("Val is Positive infinite")

elif(math.isinf(val) and val < 0):
print("val is negative infinite")

else:
print("Value is not infinite value")

#checking for postive  infinite
posInf = math.inf
check(posInf)

#checking for neagtive infinite
Neginf = -math.inf
check(Neginf)

```

Output

```Val is Positive infinite
val is negative infinite
```

2. Check if the value is infinite in Python using np.isinf()

In this Python program, we have used the NumPy library function isinf() to check for infinite values. This function checks for both positive and negative infinite values. We can use it to check for a value or array as in the below example.

```import numpy as np
arr=[3,6,9,np.inf,-np.inf]
posInf=np.inf
negInf=-np.inf
print(np.isinf(posInf))
print(np.isinf(negInf))
print('check for array elements:',np.isinf(arr))
```

Output

```True
True
check for array elements: [False False False  True  True]
```

3. Check infinite Value Using np.isneginf()

The Numpy library np.isneginf() function checks if the given value is negative infinity. When it is negative infinite it returns True. In this below python program, it returns True for -np. inf and False for np.inf

```import numpy as np
arr=[3,6,9,np.inf,-np.inf]
posInf=np.inf
negInf=-np.inf
print(np.isneginf(posInf))
print(np.isneginf(negInf))
print('check for array elements:',np.isneginf(arr))
```

Output

```False
True
check for array elements: [False False False False  True]
```

4. Check infinite Value Using np.isposinf()

The Numpy library np.isposinf() function checks if the given value is positive infinity. When it is positive infinite it returns True. In this below python program, it returns True for np. inf and False for np.inf.

```import numpy as np
arr=[3,6,9,np.inf,-np.inf]
posInf=np.inf
negInf=-np.inf
print(np.isposinf(posInf))
print(np.isposinf(negInf))
print('check for array elements:',np.isposinf(arr))
```

Output

```True
False
check for array elements: [False False False  True False]
```

Summary

In this post, we have learned How to check if value is infinite in Python by using Math module function Math.isinf() and numpy library np.isinf(),np.isneginf(),np.np.isposinf() using different programs examples.