Name

isnan

Synopsis

Tests whether a given floating-point value is “not a number”

#include <math.h>
intisnan( float x );
int isnan( double x );
int isnan( long double x );

The macro isnan() yields a nonzero value (that is, true) if its argument is a NaN, or “not a number” (see the section on float.h in Chapter 15). Otherwise, isnan() yields 0. The argument must be a real floating-point type. The rule that floating-point types are promoted to at least double precision for mathematical calculations does not apply here; the argument’s properties are determined based on its representation in its actual semantic type.

Example

double dMax( double a, double b )
{
  // NaN overrides all comparison:
  if (isnan( a ) ) return a;
  if ( isnan( b ) ) return b;
  // Anything is greater than -inf:
  if ( isinf( a ) && signbit( a ) ) return b;
  if ( isinf( b ) && signbit( b ) ) return a;

  return ( a > b ? a : b );
}

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.