macro/function
<cmath> <ctgmath>

isnan

macro
isnan(x)
function
bool isnan (float x);bool isnan (double x);bool isnan (long double x);
Is Not-A-Number
Returns whether x is a NaN (Not-A-Number) value.

The NaN values are used to identify undefined or non-representable values for floating-point elements, such as the square root of negative numbers or the result of 0/0.

In C, this is implemented as a macro that returns an int value. The type of x shall be float, double or long double.
In C++, it is implemented with function overloads for each floating-point type, each returning a bool value.

Parameters

x
A floating-point value.

Return value

A non-zero value (true) if x is a NaN value; and zero (false) otherwise.

Example

1
2
3
4
5
6
7
8
9
10
11
12
/* isnan example */
#include <stdio.h>      /* printf */
#include <math.h>       /* isnan, sqrt */

int main()
{
  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;
}

Output:

isnan(0.0)       : 0
isnan(1.0/0.0)   : 0
isnan(-1.0/0.0)  : 0
isnan(sqrt(-1.0)): 1


See also