Namespaces
Variants

std::numeric_limits<T>:: signaling_NaN

From cppreference.net
Utilities library
static T signaling_NaN ( ) throw ( ) ;
(C++11 前)
static constexpr T signaling_NaN ( ) noexcept ;
(C++11 起)

返回由浮点类型 T 表示的特定值“信令 非数值”。仅当 std:: numeric_limits < T > :: has_signaling_NaN == true 时具有意义。在IEEE 754(最常用的浮点数二进制表示标准)中,任何指数位全为1且小数位至少有一位为1的值均表示NaN。小数位的哪些值表示静默NaN或信令NaN,以及符号位是否具有意义,均由实现定义。

目录

返回值

T std:: numeric_limits < T > :: signaling_NaN ( )
/* 非特化版本 */ T ( )
bool false
char 0
signed char 0
unsigned char 0
wchar_t 0
char8_t (C++20 起) 0
char16_t (C++11 起) 0
char32_t (C++11 起) 0
short 0
unsigned short 0
int 0
unsigned int 0
long 0
unsigned long 0
long long (C++11 起) 0
unsigned long long (C++11 起) 0
float 由实现定义(可能为 FLT_SNAN
double 由实现定义(可能为 DBL_SNAN
long double 由实现定义(可能为 LDBL_SNAN

注释

NaN 永远不会与自身相等。根据 IEEE-754 标准,复制 NaN 不要求保留其位表示(符号和 有效载荷 ),但大多数实现会保留。

当信号NaN被用作算术表达式的参数时,可能会引发相应的浮点异常,同时该NaN会被"静默化",即表达式将返回一个静默NaN。

示例

演示使用信令NaN引发浮点异常:

#include <cfenv>
#include <iostream>
#include <limits>
#pragma STDC_FENV_ACCESS on
void show_fe_exceptions()
{
    int n = std::fetestexcept(FE_ALL_EXCEPT);
    if (n & FE_INVALID)
        std::cout << "FE_INVALID is raised\n";
    else if (n == 0)
        std::cout << "no exceptions are raised\n";
    std::feclearexcept(FE_ALL_EXCEPT);
}
int main()
{
    double snan = std::numeric_limits<double>::signaling_NaN();
    std::cout << "After sNaN was obtained, ";
    show_fe_exceptions();
    double qnan = snan * 2.0;
    std::cout << "After sNaN was multiplied by 2, ";
    show_fe_exceptions();
    double qnan2 = qnan * 2.0;
    std::cout << "After the quieted NaN was multiplied by 2, ";
    show_fe_exceptions();
    std::cout << "The result is " << qnan2 << '\n';
}

输出:

After sNaN was obtained, no exceptions are raised
After sNaN was multiplied by 2, FE_INVALID is raised
After the quieted NaN was multiplied by 2, no exceptions are raised
The result is nan

参见

标识可表示特殊值“信令非数”(signaling NaN)的浮点类型
(公开静态成员常量)
[static]
返回给定浮点类型的静默 NaN 值
(公开静态成员函数)
(C++11)
检查给定数值是否为 NaN
(函数)