nan()、nanf()、nanl() - 静止 NaN を戻す

標準

標準/拡張機能 C/C++ 依存項目

C99
Single UNIX Specification、バージョン 3
C++ TR1 C99

両方  z/OS V1R7

形式

#define _ISOC99_SOURCE
#include <math.h>

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

機能説明

nan() ファミリーの関数では、呼び出し nan("n-char-sequence") は strtod("NAN(n-charsequence)", (char**) NULL) と等価で、呼び出し nan("") は strtod("NAN()", (char**) NULL) と等価です。 tagp が n 文字シーケンスを示していないか、空ストリングである場合、呼び出しは strtod("NAN", (char**) NULL) と等価です。nanf() および nanl() の呼び出しは、対応する呼び出し strtof() および strtold() と等価です。
注: 下表は、これらの関数の実行可能な形式を示しています。 IEEE 2 進数浮動小数点の詳細は、IEEE 2 進数浮動小数点を参照してください。
関数 Hex IEEE
nan X X
nanf X X
nanl X X

戻り値

正常に実行された場合、tagp によって指示された内容の静止 NaN を戻します。

16 進の場合の特殊な動作: nan() ファミリーの関数は常に 0 を戻します。

/*
 * 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);
  }

}

出力:

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

関連情報