当前位置: C语言 -- 标准库 -- <math.h> -- nanf

nanf函数


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

描述:

该函数根据以下规则转换参数tagp指向的字符串:

调用函数nanf("n-char-sequence")等价于调用函数strtof("NAN(n-char-sequence)", nullptr);调用函数nanf("")等价于调用函数strtof("NAN()", nullptr)。

如果参数tagp未指向n-char-sequence序列或者空字符串,调用该函数等价于调用函数strtof("NAN", nullptr)。


参数:
const char *tagp

参数为指向n-char-sequence序列或者空字符串的指针。


返回值:

函数返回安静非数值(如果可用),其内容通过参数tagp确定。如果实现不支持安静非数值,函数返回0。


范例:
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
/*函数nanf范例*/

#include <math.h>
#include <stdio.h>
#include <string.h>

void funcNan(const char *tagp)
{
    float a;
    size_t size = sizeof(float);
    unsigned char arr[size];
    
    a = nanf(tagp);
    memcpy(arr,&a,size);
    
    printf("nanf(\"%s\"): ", tagp);
    for(size_t i=0; i<size; i++)
    {
        printf("%02x", arr[size-1-i]);
    }
    
    printf("\n");
}

int main(void)
{
    funcNan("");
    funcNan("1");

    return 0;
}


输出:
nanf(""): 7fc00000
nanf("1"): 7fc00001

(注:使用ideone编译。)

根据IEEE Std 754标准,7fc00000、7fc00001表示的均为非数值。详细解释见浮点数的数值范围。


相关内容:
nan double类型的生成安静非数值的函数。
nanl long double类型的生成安静非数值的函数。
nand32 _Decimal32类型的生成安静非数值的函数。
nand64 _Decimal64类型的生成安静非数值的函数。
nand128 _Decimal128类型的生成安静非数值的函数。


主要参考资料:

1、ISO/IEC 9899:2024

2、ISO/IEC 9899:2018

3、IEEE Std 754-2019