NaN is an abbreviation for not a number. It is a numeric type value, typically in a floating-point calculation, that represents a value that is undefined or cannot be represented. Also, you cannot use the equality operator (= =) to check NaN. In the program, Nan = = Nan (C/c++/python) or Nan is Nan (Python) always returns 0 or False.
C + + implementation
The isNaN macro or function in the MATH.H standard library is used to examine the Nan value in C + +, as shown in the following example code:
C Code TEST-NAN.C
/*isNaN Example*/#include<stdio.h>/*printf*/#include<math.h>/*isNaN, sqrt*/intMain () {printf ("isNaN (0.0):%d\n", isNaN (0.0)); printf ("isNaN (1.0/0.0):%d\n", isNaN (1.0/0.0)); printf ("isNaN ( -1.0/0.0):%d\n", isNaN (-1.0/0.0)); printf ("isNaN (sqrt ( -1.0)):%d\n", isNaN (sqrt (-1.0))); return 0;}
Compile and run the results as follows
gcc test-nan.c-lm$. /A.outisnan (0.0) 0isNaN (1.0/0.0) 0 isNaN (-1.0/0.0) 0isNaN (sqrt (-1.0 1
C + + code Test-nan.cpp
/*isNaN Example*/#include<cmath>/*isNaN, sqrt*/#include<iostream>using namespace std;intMain () {cout<<"isNaN (0.0):"<< isNaN (0.0) <<Endl; cout<<"isNaN (1.0/0.0):"<< isNaN (1.0/0.0) <<Endl; cout<<"isNaN ( -1.0/0.0):"<< isNaN (-1.0/0.0) <<Endl; cout<<"isNaN (sqrt ( -1.0)):"<< isNaN (sqrt (-1.0)) <<Endl; Return0;}
Compile and run the results as follows
g++ $ . /A.outisnan (0.0) 0isNaN (1.0/0.0) 0 isNaN (-1.0/0.0) 0 isNaN (sqrt (-1.01
If you add-std=c++11 at compile time and use the C + + 2011 Standard compiler, the following error may occur:
$ g++ Test-nan. cpp -std=c++... error: Call of overloaded ' isNaN (double) ' is ambiguous ...
A simple workaround is to add the domain operator (::) before all the isNaN macros or functions, and the modified sample code is as follows:
/*isNaN Example*/#include<cmath>/*isNaN, sqrt*/#include<iostream>using namespacestd;intMain () {cout<<"isNaN (0.0):"<<:: isNaN (0.0) <<Endl; cout<<"isNaN (1.0/0.0):"<<:: isNaN (1.0/0.0) <<Endl; cout<<"isNaN ( -1.0/0.0):"<<:: isNaN (-1.0/0.0) <<Endl; cout<<"isNaN (sqrt ( -1.0)):"<<:: isNaN (sqrt (-1.0)) <<Endl; return 0;}
Once saved, recompile and run.
Python Implementation
Python uses the numpy numeric math library function Np.isnan to check Nan values, and the sample code test-nan.py as follows:
#!/usr/bin/env python#-*-Coding:utf8-*-#Author:klchang from __future__ Importprint_functionImportNumPy as NPPrint("isNaN (0.0):", Np.isnan (0.0))Print("isNaN (1.0/0.0):", Np.isnan (Np.true_divide (1.0, 0.0)))Print("isNaN ( -1.0/0.0):", Np.isnan (Np.true_divide (-1.0, 0.0)))Print("isNaN (sqrt ( -1.0)):", Np.isnan (NP.SQRT (-1.0)))
Run the output as follows:
$ python test-Nan.pyisnan (0.0): False ...: runtimewarning:divide by Zero encounteredinchtrue_divide Print ("isNaN (1.0/0.0):", Np.isnan (Np.true_divide (1.0,0.0)) ) isNaN (1.0/0.0): False ...: runtimewarning:divide by Zero encounteredinchtrue_divide Print ("isNaN ( -1.0/0.0):", Np.isnan (Np.true_divide (-1.0,0.0)) ) isNaN (-1.0/0.0): False ...: runtimewarning:invalid value encounteredinchsqrt Print ("isNaN (sqrt ( -1.0)):", Np.isnan (NP.SQRT (-1.0)) ) isNaN (sqrt (-1.0)): True
References
1. isNaN macro/function-<cmath> reference. http://www.cplusplus.com/reference/cmath/isnan/
2. Nan-wikipedia, the free encyclopedia. Https://en.wikipedia.org/wiki/NaN
3. NumPy isnan-numpy Manual. Https://docs.scipy.org/doc/numpy/reference/generated/numpy.isnan.html
Check NaN data values (C/c++/python implementation)