Check if the value is infinity or NaN in Python - GeeksforGeeks

23 Aug, 2021
In this article, we will check whether the given value is NaN or Infinity. This can be done using the math module. Let’s see how to check each value in detail.
NaN Stands for “Not a Number” and it is a numeric datatype used as a proxy for values that are either mathematically undefined or cannot be represented.  There are various examples of them like-
0/0 is undefined and NaN is used for representing it.Sqrt(-ve number) cannot be stored as a real number so NaN is used for representing it.Log(-ve number) cannot be stored as a real number so NaN is used for representing it.Inverse sin or Inverse cos of a number < -1 or number > 1 is also NaN.0 * inf also leads to NaN.
0/0 is undefined and NaN is used for representing it.
Sqrt(-ve number) cannot be stored as a real number so NaN is used for representing it.
Log(-ve number) cannot be stored as a real number so NaN is used for representing it.
Inverse sin or Inverse cos of a number < -1 or number > 1 is also NaN.
0 * inf also leads to NaN.
Since NaN is type in itself It is used to assign variables whose values are not yet calculated.
Method 1: To check for NaN we can use math.isnan() function as NaN cannot be tested using == operator.  
Python3
import math  x = math.nanprint(f"x contains {x}") # checks if variable is equal to NaNif(math.isnan(x)):    print("x == nan")else:    print("x != nan")
x contains nan
x == nan
Method 2: NaN is not equal to NaN and therefore we can exploit this property to check for NaN. The following code demonstrates it.
Python3
import math  def isNan(number):         # This function compares number to itself.    # NaN != NaN and therefore it will return    # true if the number is Nan    return number != number # Driver Code x = math.nanprint(isNan(x))
  
True
 
Method 1: To check for infinite in python the function used is math.isinf() which only checks for infinite. To distinguish between positive and negative infinite we can add more logic that checks if the number is greater than 0 or less than 0. The code shows this in action.
 
Python3
import math # Function checks if negative or# positive infinite.def check(x):       if(math.isinf(x) and x > 0):        print("x is Positive inf")         elif(math.isinf(x) and x < 0):        print("x is negative inf")         else:        print("x is not inf")  # Creating inf using math module.number = math.infcheck(number) number = -math.infcheck(number)
  
x is Positive inf
x is negative inf
 
Method 2: Numpy also exposes two APIs to check for positive and negative infinite. which are np.isneginf() and np.isposinf(). 
 
Python3
# pip install numpyimport numpy as np print(np.isneginf([np.inf, 0, -np.inf]))print(np.isposinf([np.inf, 0, -np.inf]))
Output
[False False  True]
[ True False False]
anikaseth98
Picked
Python math-library
Python-numpy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
Selecting rows in pandas DataFrame based on conditions
How to drop one or multiple columns in Pandas Dataframe
Python | Get unique values from a list
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | os.path.join() method
Defaultdict in Python
Create a directory in Python
Python Classes and Objects