Namespaces
Variants

std:: nan, std:: nanf, std:: nanl

From cppreference.net
Common mathematical functions
Nearest integer floating point operations
(C++11)
(C++11)
(C++11) (C++11) (C++11)
Floating point manipulation functions
(C++11) (C++11)
(C++11)
(C++11)
Classification and comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Types
(C++11)
(C++11)
(C++11)
Macro constants
定义于头文件 <cmath>
float nanf ( const char * arg ) ;
(1) (C++11 起)
double nan ( const char * arg ) ;
(2) (C++11 起)
long double nanl ( const char * arg ) ;
(3) (C++11 起)

将字符串 arg 转换为对应的静默 NaN 值,转换方式分别类似于调用 std::strtof std::strtod std::strtold 函数。

1) 调用 std :: nanf ( " n-char-sequence ") (其中 n-char-sequence 是数字、ASCII字母和下划线的序列)等价于调用 std:: strtof ( "NAN( n-char-sequence ) " , ( char ** ) nullptr ) ;
调用 std :: nanf ( "" ) 等价于调用 std:: strtof ( "NAN()" , ( char ** ) nullptr ) ;
当调用 std :: nanf ( " string ") 时,若 string 既非 n-char-sequence 也非空字符串,则等效于调用 std:: strtof ( "NAN" , ( char ** ) nullptr ) ;
2) (1) 相同,但调用 std::strtod 而非 std::strtof
3) (1) 相同,但调用 std::strtold 而非 std::strtof

目录

参数

arg - 标识 NaN 内容的窄字符串

返回值

与标识字符串 arg 对应的静默 NaN 值,若实现不支持静默 NaN 则返回零。

如果实现支持 IEEE 浮点算术(IEC 60559),则它也支持静默 NaN。

错误处理

此函数不受 math_errhandling 中指定的任何错误条件约束。

示例

#include <cmath>
#include <cstdint>
#include <cstring>
#include <iostream>
int main()
{
    double f1 = std::nan("1");
    std::uint64_t f1n; std::memcpy(&f1n, &f1, sizeof f1);
    std::cout << "nan(\"1\") = " << f1 << " (" << std::hex << f1n << ")\n";
    double f2 = std::nan("2");
    std::uint64_t f2n; std::memcpy(&f2n, &f2, sizeof f2);
    std::cout << "nan(\"2\") = " << f2 << " (" << std::hex << f2n << ")\n";
}

可能的输出:

nan("1") = nan (7ff0000000000001)
nan("2") = nan (7ff0000000000002)

参见

(C++11)
检查给定数值是否为非数值(NaN)
(函数)
(C++11)
生成 float 类型的静默非数值(NaN)
(宏常量)
标识可表示特殊值"静默非数值"(quiet NaN)的浮点类型
( std::numeric_limits<T> 的公开静态成员常量)
标识可表示特殊值"信号非数值"(signaling NaN)的浮点类型
( std::numeric_limits<T> 的公开静态成员常量)
[static]
返回给定浮点类型的静默非数值(NaN)
( std::numeric_limits<T> 的公开静态成员函数)
返回给定浮点类型的信号非数值(NaN)
( std::numeric_limits<T> 的公开静态成员函数)
C 文档 for nanf , nan , nanl