nan, nanf, nanl

Defined in header <math.h>
float       nanf( const char* arg );
(since C99)
double      nan( const char* arg );
(since C99)
long double nanl( const char* arg );
(since C99)

Converts the implementation-defined character string arg into the corresponding quiet NaN value, as if by calling strtof, strtod, or strtold, respectively, as follows:

The call nan("n-char-sequence"), where n-char-sequence is a sequence of digits, Latin letters, and underscores, is equivalent to the call strtod("NAN(n-char-sequence)", (char**)NULL);.

The call nan("") is equivalent to the call strtod("NAN()", (char**)NULL);.

The call nan("string"), where string is neither an n-char-sequence nor an empty string, is equivalent to the call strtod("NAN", (char**)NULL);.


arg - narrow character string identifying the contents of a NaN

Return value

The quiet NaN value that corresponds to the identifying string arg or zero if the implementation does not support quiet NaNs.


#include <stdio.h>
#include <math.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
int main(void)
    double f1 = nan("1");
    uint64_t f1n; memcpy(&f1n, &f1, sizeof f1);
    printf("nan(\"1\")   = %f (%" PRIx64 ")\n", f1, f1n);
    double f2 = nan("2");
    uint64_t f2n; memcpy(&f2n, &f2, sizeof f2);
    printf("nan(\"2\")   = %f (%" PRIx64 ")\n", f2, f2n);
    double f3 = nan("0xF");
    uint64_t f3n; memcpy(&f3n, &f3, sizeof f3);
    printf("nan(\"0xF\") = %f (%" PRIx64 ")\n", f3, f3n);

Possible output:

nan("1")   = nan (7ff8000000000001)
nan("2")   = nan (7ff8000000000002)
nan("0xF") = nan (7ff800000000000f)

See also

checks if the given number is NaN
evaluates to a quiet NaN of type float
(macro constant)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.