nan(), nanf(), nanl() — Return quiet NaN

Standards

Standards / Extensions C or C++ Dependencies

C99
Single UNIX Specification, Version 3
C++ TR1 C99

both  z/OS V1R7

Format

#define _ISOC99_SOURCE
#include <math.h>

double nan(const char *tagp);
float nanf(const char *tagp);
long double nanl(const char *tagp);

General description

In the nan() family of functions, the call nan("n-char-sequence") is equivalent to strtod("NAN(n-charsequence)", (char**) NULL) and the call nan("") is equivalent to strtod("NAN()", (char**) NULL). If tagp does not point to an n-char sequence or an empty string, the call is equivalent to strtod("NAN", (char**) NULL). Calls to nanf() and nanl() are equivalent to the corresponding calls strtof() and strtold().
Note: The following table shows the viable formats for these functions. See IEEE binary floating-point for more information about IEEE Binary Floating-Point.
Function Hex IEEE
nan X X
nanf X X
nanl X X

Returned value

If successful, they return a quiet NaN with content indicated by tagp.

Special behavior in hex: The nan() family of functions always return 0.

Example

/*
 * This program illustrates the use of the nan() function
 *
 * It calls both nan and strtod with equivalent arguments
 * and displays output of both. Output should be identical.
 *
 */
#define _ISOC99_SOURCE
#include <stdio.h>
#include <stdlib.h>         /* needed of strtod */
#include <math.h>


#define TESTVALS 5
struct {
   const char * str;
} nan_vals[] = {
/*0*/  { "0"           },
/*1*/  { "1"           },
/*2*/  { "something"   },   /* invalid n-char seq. */
/*3*/  { "2147483647"  },   /* int max      */
/*4*/  { "2147483648"  }    /* int max  +1  */
},

strod_vals[] = {
/*0*/  { "NAN(0)"          },
/*1*/  { "NAN(1)"          },
/*2*/  { "NAN"             },
/*3*/  { "NAN(2147483647)" },
/*4*/  { "NAN(2147483648)" }
};


void main()
{
  double outnan,
         outstrtod;
  int i;
  char *tagp = (char *)NULL;

  printf("Illustrates the nan() function\n");
  printf("Output for both nan() and strtod() should be identical.\n\n");
  for (i=0; i<TESTVALS; i++) {
        outnan = nan(nan_vals[i].str);
        outstrtod = strtod(strod_vals[i].str, &tagp);

        printf("nan(%s)         returned = %g\n",nan_vals[i].str, outnan);
        printf("strtod(%s) returned = %g\n\n",strod_vals[i].str, outstrtod);
  }

}

Output

Illustrates the nan() function
Output for both nan() and strtod() should be identical.

nan(0)         returned = 0
strtod(NAN(0)) returned = 0

nan(1)         returned = NaNQ(1)
strtod(NAN(1)) returned = NaNQ(1)

nan(something)         returned = NaNQ(1)
strtod(NAN) returned = NaNQ(1)

nan(2147483647)         returned = NaNQ(2147483647)
strtod(NAN(2147483647)) returned = NaNQ(2147483647)

nan(2147483648)         returned = 0
strtod(NAN(2147483648)) returned = 0

Related information