std::numeric_limits<T>::signaling_NaN

来自cppreference.com
 
 
 
类型支持
基本类型
基础类型
定宽整数类型 (C++11)
数值极限
C 数值极限接口
运行时类型信息
类型特性
类型类别
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
类型属性
(C++11)
(C++11)
(C++14)
(C++11)
(C++11)(C++20 前)
(C++11)(C++20 中弃用)
(C++11)
类型特性常量
元函数
(C++17)
常量求值语境
受支持操作
关系与属性查询
类型修改
(C++11)(C++11)(C++11)
类型变换
(C++11)
(C++11)
(C++17)
(C++11)(C++20 前)(C++17)
 
 
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 ,及符号位是否有意义,是实现定义的。

返回值

 
T std::numeric_limits<T>::signaling_NaN()
/* non-specialized */ T()
bool false
char 0
signed char 0
unsigned char 0
wchar_t 0
char8_t 0
char16_t 0
char32_t 0
short 0
unsigned short 0
int 0
unsigned int 0
long 0
unsigned long 0
long long 0
unsigned long long 0
float 实现定义
double 实现定义
long double 实现定义

注意

NaN 决不与自身比较相等。 IEEE-754 不要求复制 NaN 保留其位表示(符号与载荷),尽管大多数实现都会保留。

当发信 NaN 被用于算术表达式的参数时,可能引发适当的浮点异常,而且 NaN 被“安静化”,即表达式返回安静的 NaN 。

示例

演示用发信的 NaN 引发浮点异常

#include <iostream>
#include <limits>
#include <cfenv>
#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

参阅

鉴别能表示特殊值“发信的非数”( NaN )的浮点类型
(公开静态成员常量)
[静态]
返回给定浮点类型的安静 NaN 值
(公开静态成员函数)
(C++11)
检查给定的数是否 NaN
(函数)