目录搜索
文字
分享

在头文件<math.h>中定义



float       nanf( const char* arg );


(since C99)

double      nan( const char* arg );


(since C99)

long double nanl( const char* arg );


(since C99)

实现定义字符串转换arg成相应的静态NaN值,则通过调用strtofstrtod或者strtold,分别说明如下:

通话nan("string")等同于通话strtod("NAN(string)", (char**)NULL);

通话nan("")等同于通话strtod("NAN()", (char**)NULL);

通话nan(NULL)等同于通话strtod("NAN", (char**)NULL);

参数

arg

-

窄字符串标识NaN的内容

返回值

安静的NaN值与标识字符串相对应,arg或者如果实现不支持安静的NaN,则为零。

#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);}

可能的输出:

nan("1")   = nan (7ff8000000000001)nan("2")   = nan (7ff8000000000002)nan("0xF") = nan (7ff800000000000f)
上一篇:modfl下一篇:NAN