C++
应用 | Utilities

std::numeric_limits::signaling_NaN

STD::数字[医]限制::信令[医]南

static T signaling_NaN((until C++11)
static constexpr T signaling_NaN((since C++11)

返回特殊值“信令”。无号“,由浮点类型表示。T.只有在以下情况下才有意义std::numeric_limits<T>::has_signaling_NaN==true在ieee 754中,浮点数最常见的二进制表示形式,指数集的所有位数和分数集的至少一位的任何值都代表NaN。它是实现-定义的分数的值代表安静或信令的nans,以及符号位是否有意义。

返回值

Tstd::numeric_limits::signaling_NaN()
/* non-specialized */T()
boolfalse
char​0​
signed char​0​
unsigned char​0​
wchar_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​
floatimplementation-defined
doubleimplementation-defined
long doubleimplementation-defined

例外

(none)(until C++11)
noexcept specification: noexcept(since C++11)

注记

NaN从来都比不上它自己。复制NaN并不是ieee-754所要求的,以保留其位表示形式%28符号和有效载荷%29,尽管大多数实现都是这样做的。

当信号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

二次

另见

has_signaling_NaN staticidentifies floating-point types that can represent the special value "signaling not-a-number" (NaN) (public static member constant)
quiet_NaN staticreturns a quiet NaN value of the given floating-point type (public static member function)
isnan (C++11)checks if the given number is NaN (function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/type/NUMERIC[医]限制/信号[医]南