The nan functions (C99) - nan, nanf, nanl

文字列を NaN (非数) に変換します.

nan (C99)

#include <math.h>
double nan(
    const char *tagp
);

nan 関数は文字列を double 型の NaN (非数) に変換します.

nanf (C99)

#include <math.h>
float nanf(
    const char *tagp
);

nanf 関数は文字列を float 型の NaN (非数) に変換します.

nanl (C99)

#include <math.h>
long double nanl(
    const char *tagp
);

nanl 関数は文字列を long double 型の NaN (非数) に変換します.


nan("n 文字列") の呼び出しは以下の式と等価になります.

strtod("NAN(n文字列)", (char **)NULL);

nan("") の呼び出しは以下の式と等価になります.

strtod("NAN()", (char **)NULL);

tagp が文字列を指していない場合の動作は以下の式と等価になります.

strtod("NAN", (char **)NULL);

戻り値

C言語サンプルプログラム

以下に nan 関数を使用したサンプルプログラムを示します.

/* header files */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* main */
int main(void) {
    char s[] = "snoopy";
    char empty[] = "";

    if ( isnan(nan(s)) ) {
        printf("nan(s): NaN\n");
    }

    if ( isnan(nan(empty)) ) {
        printf("nan(empty): NaN\n");
    }

    if ( isnan(nan(NULL)) ) {
        printf("nan(NULL): NaN\n");
    }

    return EXIT_SUCCESS;
}

実行例

サンプルプログラムの実行結果は以下のようになります.

nan(s): NaN
nan(empty): NaN
nan(NULL): NaN