数值极限
定义于头文件
template< class T > class numeric_limits;
numeric_limits 类模板提供查询各种算术类型属性的标准化方式(例如 int 类型的最大可能值是 std::numeric_limits
std::numeric_limits<T>::quiet_NaN
static T quiet_NaN() throw(); | (C++11 前) | |
static constexpr T quiet_NaN() noexcept; | (C++11 起) |
返回特殊值“安静的非数”,以浮点类型 T
表示。仅若 std::numeric_limits
T | std::numeric_limits |
/* 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 | NAN 或另一实现定义的 NaN |
double | 实现定义 |
long double | 实现定义 |
NaN 与自身决不比较相等。复制 NaN 可能不保留其位表示。
- #include <iostream>
- #include <string>
- #include <limits>
- #include <cstdint>
- #include <cfloat>
- #include <cmath>
-
- struct SName
- {
- };
-
- //偏特化
- struct SPartSpec
- {
- };
-
- namespace std
- {
- template<>
- struct numeric_limits<SPartSpec>
- {
- static _GLIBCXX_USE_CONSTEXPR bool is_specialized = true;
- static _GLIBCXX_USE_CONSTEXPR bool is_signed = true;
- static _GLIBCXX_USE_CONSTEXPR bool is_integer = true;
- static _GLIBCXX_USE_CONSTEXPR bool is_exact = true;
- static _GLIBCXX_USE_CONSTEXPR bool has_infinity = true;
- static _GLIBCXX_USE_CONSTEXPR bool has_quiet_NaN = true;
- static _GLIBCXX_USE_CONSTEXPR bool has_signaling_NaN = true;
- static _GLIBCXX_USE_CONSTEXPR float_denorm_style has_denorm = denorm_present;
- static _GLIBCXX_USE_CONSTEXPR bool has_denorm_loss = true;
- static _GLIBCXX_USE_CONSTEXPR float_round_style round_style = round_toward_neg_infinity;
- static _GLIBCXX_USE_CONSTEXPR bool is_iec559 = true;
- static _GLIBCXX_USE_CONSTEXPR bool is_bounded = true;
- static _GLIBCXX_USE_CONSTEXPR bool is_modulo = true;
- static _GLIBCXX_USE_CONSTEXPR int digits = CHAR_BIT;
- static _GLIBCXX_USE_CONSTEXPR int digits10 = CHAR_BIT;
- static _GLIBCXX_USE_CONSTEXPR int max_digits10 = DECIMAL_DIG;
- static _GLIBCXX_USE_CONSTEXPR int radix = FLT_RADIX;
- static _GLIBCXX_USE_CONSTEXPR int min_exponent = FLT_MIN_EXP;
- static _GLIBCXX_USE_CONSTEXPR int min_exponent10 = FLT_MIN_10_EXP;
- static _GLIBCXX_USE_CONSTEXPR int max_exponent = FLT_MAX_EXP;
- static _GLIBCXX_USE_CONSTEXPR int max_exponent10 = FLT_MAX_EXP;
- static _GLIBCXX_USE_CONSTEXPR bool traps = true;
- static _GLIBCXX_USE_CONSTEXPR bool tinyness_before = true;
-
- static _GLIBCXX_CONSTEXPR int
- min() _GLIBCXX_USE_NOEXCEPT { return CHAR_MIN ; }
- static _GLIBCXX_CONSTEXPR int
- lowest() _GLIBCXX_USE_NOEXCEPT { return CHAR_MIN ; }
- static _GLIBCXX_CONSTEXPR int
- max() _GLIBCXX_USE_NOEXCEPT { return CHAR_MAX ; }
- static _GLIBCXX_CONSTEXPR int
- epsilon() _GLIBCXX_USE_NOEXCEPT { return LDBL_EPSILON ; }
- static _GLIBCXX_CONSTEXPR double
- round_error() _GLIBCXX_USE_NOEXCEPT { return 0.5F ; }
- static _GLIBCXX_CONSTEXPR double
- infinity() _GLIBCXX_USE_NOEXCEPT { return HUGE_VAL ; }
- static _GLIBCXX_CONSTEXPR double
- quiet_NaN() _GLIBCXX_USE_NOEXCEPT { return NAN ; }
- };
- }
-
- int main()
- {
- std::cout << std::boolalpha;
- std::cout << "std::numeric_limits
::quiet_NaN(): " - << std::numeric_limits<bool>::quiet_NaN() << std::endl;
- std::cout << "std::numeric_limits
::quiet_NaN(): " - << std::numeric_limits<char>::quiet_NaN() << std::endl;
- std::cout << "std::numeric_limits
::quiet_NaN(): " - << std::numeric_limits<signed char>::quiet_NaN() << std::endl;
- std::cout << "std::numeric_limits
::quiet_NaN(): " - << std::numeric_limits<unsigned char>::quiet_NaN() << std::endl;
- std::cout << "std::numeric_limits
::quiet_NaN(): " - << std::numeric_limits<wchar_t>::quiet_NaN() << std::endl;
- std::cout << "std::numeric_limits
::quiet_NaN(): " - << std::numeric_limits<char16_t>::quiet_NaN() << std::endl;
- std::cout << "std::numeric_limits
::quiet_NaN(): " - << std::numeric_limits<char32_t>::quiet_NaN() << std::endl;
- std::cout << "std::numeric_limits
::quiet_NaN(): " - << std::numeric_limits<short>::quiet_NaN() << std::endl;
- std::cout << "std::numeric_limits
::quiet_NaN(): " - << std::numeric_limits<unsigned short>::quiet_NaN() << std::endl;
- std::cout << "std::numeric_limits
::quiet_NaN(): " - << std::numeric_limits<int>::quiet_NaN() << std::endl;
- std::cout << "std::numeric_limits
::quiet_NaN(): " - << std::numeric_limits<unsigned int>::quiet_NaN() << std::endl;
- std::cout << "std::numeric_limits
::quiet_NaN(): " - << std::numeric_limits<long>::quiet_NaN() << std::endl;
- std::cout << "std::numeric_limits
::quiet_NaN(): " - << std::numeric_limits<unsigned long>::quiet_NaN() << std::endl;
- std::cout << "std::numeric_limits
::quiet_NaN(): " - << std::numeric_limits<long long>::quiet_NaN() << std::endl;
- std::cout << "std::numeric_limits
::quiet_NaN(): " - << std::numeric_limits<unsigned long long>::quiet_NaN() << std::endl;
- std::cout << "std::numeric_limits
::quiet_NaN(): " - << std::numeric_limits<float>::quiet_NaN() << std::endl;
- std::cout << "std::numeric_limits
::quiet_NaN(): " - << std::numeric_limits<double>::quiet_NaN() << std::endl;
- std::cout << "std::numeric_limits
::quiet_NaN(): " - << std::numeric_limits<long double>::quiet_NaN() << std::endl;
- std::cout << "std::numeric_limits
::quiet_NaN(): " - << std::numeric_limits<std::string>::quiet_NaN() << std::endl;
- //必须偏特化
- // std::cout << "std::numeric_limits
::quiet_NaN(): " - // << std::numeric_limits<SName>::quiet_NaN() << std::endl;
- std::cout << "std::numeric_limits
::quiet_NaN(): " - << std::numeric_limits<SPartSpec>::quiet_NaN() << std::endl;
- return 0;
- }
std::numeric_limits<T>::signaling_NaN
static T signaling_NaN() throw(); | (C++11 前) | |
static constexpr T signaling_NaN() noexcept; | (C++11 起) |
返回特殊值“发信的非数”,以浮点类型 T
表示。仅若 std::numeric_limits
T | std::numeric_limits |
/* 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 | 实现定义 |
- #include <iostream>
- #include <string>
- #include <limits>
- #include <cstdint>
- #include <cfloat>
- #include <cmath>
-
- struct SName
- {
- };
-
- //偏特化
- struct SPartSpec
- {
- };
-
- namespace std
- {
- template<>
- struct numeric_limits<SPartSpec>
- {
- static _GLIBCXX_USE_CONSTEXPR bool is_specialized = true;
- static _GLIBCXX_USE_CONSTEXPR bool is_signed = true;
- static _GLIBCXX_USE_CONSTEXPR bool is_integer = true;
- static _GLIBCXX_USE_CONSTEXPR bool is_exact = true;
- static _GLIBCXX_USE_CONSTEXPR bool has_infinity = true;
- static _GLIBCXX_USE_CONSTEXPR bool has_quiet_NaN = true;
- static _GLIBCXX_USE_CONSTEXPR bool has_signaling_NaN = true;
- static _GLIBCXX_USE_CONSTEXPR float_denorm_style has_denorm = denorm_present;
- static _GLIBCXX_USE_CONSTEXPR bool has_denorm_loss = true;
- static _GLIBCXX_USE_CONSTEXPR float_round_style round_style = round_toward_neg_infinity;
- static _GLIBCXX_USE_CONSTEXPR bool is_iec559 = true;
- static _GLIBCXX_USE_CONSTEXPR bool is_bounded = true;
- static _GLIBCXX_USE_CONSTEXPR bool is_modulo = true;
- static _GLIBCXX_USE_CONSTEXPR int digits = CHAR_BIT;
- static _GLIBCXX_USE_CONSTEXPR int digits10 = CHAR_BIT;
- static _GLIBCXX_USE_CONSTEXPR int max_digits10 = DECIMAL_DIG;
- static _GLIBCXX_USE_CONSTEXPR int radix = FLT_RADIX;
- static _GLIBCXX_USE_CONSTEXPR int min_exponent = FLT_MIN_EXP;
- static _GLIBCXX_USE_CONSTEXPR int min_exponent10 = FLT_MIN_10_EXP;
- static _GLIBCXX_USE_CONSTEXPR int max_exponent = FLT_MAX_EXP;
- static _GLIBCXX_USE_CONSTEXPR int max_exponent10 = FLT_MAX_EXP;
- static _GLIBCXX_USE_CONSTEXPR bool traps = true;
- static _GLIBCXX_USE_CONSTEXPR bool tinyness_before = true;
-
- static _GLIBCXX_CONSTEXPR int
- min() _GLIBCXX_USE_NOEXCEPT { return CHAR_MIN ; }
- static _GLIBCXX_CONSTEXPR int
- lowest() _GLIBCXX_USE_NOEXCEPT { return CHAR_MIN ; }
- static _GLIBCXX_CONSTEXPR int
- max() _GLIBCXX_USE_NOEXCEPT { return CHAR_MAX ; }
- static _GLIBCXX_CONSTEXPR int
- epsilon() _GLIBCXX_USE_NOEXCEPT { return LDBL_EPSILON ; }
- static _GLIBCXX_CONSTEXPR double
- round_error() _GLIBCXX_USE_NOEXCEPT { return 0.5F ; }
- static _GLIBCXX_CONSTEXPR double
- infinity() _GLIBCXX_USE_NOEXCEPT { return HUGE_VAL ; }
- static _GLIBCXX_CONSTEXPR double
- quiet_NaN() _GLIBCXX_USE_NOEXCEPT { return NAN ; }
- static _GLIBCXX_CONSTEXPR double
- signaling_NaN() _GLIBCXX_USE_NOEXCEPT { return NAN ; }
- };
- }
-
- int main()
- {
- std::cout << std::boolalpha;
- std::cout << "std::numeric_limits
::signaling_NaN(): " - << std::numeric_limits<bool>::signaling_NaN() << std::endl;
- std::cout << "std::numeric_limits
::signaling_NaN(): " - << std::numeric_limits<char>::signaling_NaN() << std::endl;
- std::cout << "std::numeric_limits
::signaling_NaN(): " - << std::numeric_limits<signed char>::signaling_NaN() << std::endl;
- std::cout << "std::numeric_limits
::signaling_NaN(): " - << std::numeric_limits<unsigned char>::signaling_NaN() << std::endl;
- std::cout << "std::numeric_limits
::signaling_NaN(): " - << std::numeric_limits<wchar_t>::signaling_NaN() << std::endl;
- std::cout << "std::numeric_limits
::signaling_NaN(): " - << std::numeric_limits<char16_t>::signaling_NaN() << std::endl;
- std::cout << "std::numeric_limits
::signaling_NaN(): " - << std::numeric_limits<char32_t>::signaling_NaN() << std::endl;
- std::cout << "std::numeric_limits
::signaling_NaN(): " - << std::numeric_limits<short>::signaling_NaN() << std::endl;
- std::cout << "std::numeric_limits
::signaling_NaN(): " - << std::numeric_limits<unsigned short>::signaling_NaN() << std::endl;
- std::cout << "std::numeric_limits
::signaling_NaN(): " - << std::numeric_limits<int>::signaling_NaN() << std::endl;
- std::cout << "std::numeric_limits
::signaling_NaN(): " - << std::numeric_limits<unsigned int>::signaling_NaN() << std::endl;
- std::cout << "std::numeric_limits
::signaling_NaN(): " - << std::numeric_limits<long>::signaling_NaN() << std::endl;
- std::cout << "std::numeric_limits
::signaling_NaN(): " - << std::numeric_limits<unsigned long>::signaling_NaN() << std::endl;
- std::cout << "std::numeric_limits
::signaling_NaN(): " - << std::numeric_limits<long long>::signaling_NaN() << std::endl;
- std::cout << "std::numeric_limits
::signaling_NaN(): " - << std::numeric_limits<unsigned long long>::signaling_NaN() << std::endl;
- std::cout << "std::numeric_limits
::signaling_NaN(): " - << std::numeric_limits<float>::signaling_NaN() << std::endl;
- std::cout << "std::numeric_limits
::signaling_NaN(): " - << std::numeric_limits<double>::signaling_NaN() << std::endl;
- std::cout << "std::numeric_limits
::signaling_NaN(): " - << std::numeric_limits<long double>::signaling_NaN() << std::endl;
- std::cout << "std::numeric_limits
::signaling_NaN(): " - << std::numeric_limits<std::string>::signaling_NaN() << std::endl;
- //必须偏特化
- // std::cout << "std::numeric_limits
::signaling_NaN(): " - // << std::numeric_limits<SName>::signaling_NaN() << std::endl;
- std::cout << "std::numeric_limits
::signaling_NaN(): " - << std::numeric_limits<SPartSpec>::signaling_NaN() << std::endl;
- return 0;
- }
std::numeric_limits<T>::denorm_min
static T denorm_min() throw(); | (C++11 前) | |
static constexpr T denorm_min() noexcept; | (C++11 起) |
若 std::numeric_limitsT
类型的最小正非正规值,否则返回 std::numeric_limits
T | std::numeric_limits |
/* 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 | 若 std::numeric_limits |
double | 若 std::numeric_limits |
long double | /* 实现定义 */ |
- #include <iostream>
- #include <string>
- #include <limits>
- #include <cstdint>
- #include <cfloat>
- #include <cmath>
-
- struct SName
- {
- };
-
- //偏特化
- struct SPartSpec
- {
- };
-
- namespace std
- {
- template<>
- struct numeric_limits<SPartSpec>
- {
- static _GLIBCXX_USE_CONSTEXPR bool is_specialized = true;
- static _GLIBCXX_USE_CONSTEXPR bool is_signed = true;
- static _GLIBCXX_USE_CONSTEXPR bool is_integer = true;
- static _GLIBCXX_USE_CONSTEXPR bool is_exact = true;
- static _GLIBCXX_USE_CONSTEXPR bool has_infinity = true;
- static _GLIBCXX_USE_CONSTEXPR bool has_quiet_NaN = true;
- static _GLIBCXX_USE_CONSTEXPR bool has_signaling_NaN = true;
- static _GLIBCXX_USE_CONSTEXPR float_denorm_style has_denorm = denorm_present;
- static _GLIBCXX_USE_CONSTEXPR bool has_denorm_loss = true;
- static _GLIBCXX_USE_CONSTEXPR float_round_style round_style = round_toward_neg_infinity;
- static _GLIBCXX_USE_CONSTEXPR bool is_iec559 = true;
- static _GLIBCXX_USE_CONSTEXPR bool is_bounded = true;
- static _GLIBCXX_USE_CONSTEXPR bool is_modulo = true;
- static _GLIBCXX_USE_CONSTEXPR int digits = CHAR_BIT;
- static _GLIBCXX_USE_CONSTEXPR int digits10 = CHAR_BIT;
- static _GLIBCXX_USE_CONSTEXPR int max_digits10 = DECIMAL_DIG;
- static _GLIBCXX_USE_CONSTEXPR int radix = FLT_RADIX;
- static _GLIBCXX_USE_CONSTEXPR int min_exponent = FLT_MIN_EXP;
- static _GLIBCXX_USE_CONSTEXPR int min_exponent10 = FLT_MIN_10_EXP;
- static _GLIBCXX_USE_CONSTEXPR int max_exponent = FLT_MAX_EXP;
- static _GLIBCXX_USE_CONSTEXPR int max_exponent10 = FLT_MAX_EXP;
- static _GLIBCXX_USE_CONSTEXPR bool traps = true;
- static _GLIBCXX_USE_CONSTEXPR bool tinyness_before = true;
-
- static _GLIBCXX_CONSTEXPR int
- min() _GLIBCXX_USE_NOEXCEPT { return CHAR_MIN ; }
- static _GLIBCXX_CONSTEXPR int
- lowest() _GLIBCXX_USE_NOEXCEPT { return CHAR_MIN ; }
- static _GLIBCXX_CONSTEXPR int
- max() _GLIBCXX_USE_NOEXCEPT { return CHAR_MAX ; }
- static _GLIBCXX_CONSTEXPR int
- epsilon() _GLIBCXX_USE_NOEXCEPT { return LDBL_EPSILON ; }
- static _GLIBCXX_CONSTEXPR double
- round_error() _GLIBCXX_USE_NOEXCEPT { return 0.5F ; }
- static _GLIBCXX_CONSTEXPR double
- infinity() _GLIBCXX_USE_NOEXCEPT { return HUGE_VAL ; }
- static _GLIBCXX_CONSTEXPR double
- quiet_NaN() _GLIBCXX_USE_NOEXCEPT { return NAN ; }
- static _GLIBCXX_CONSTEXPR double
- signaling_NaN() _GLIBCXX_USE_NOEXCEPT { return NAN ; }
- static _GLIBCXX_CONSTEXPR double
- denorm_min() _GLIBCXX_USE_NOEXCEPT { return NAN ; }
- };
- }
-
- int main()
- {
- std::cout << std::boolalpha;
- std::cout << "std::numeric_limits
::denorm_min(): " - << std::numeric_limits<bool>::denorm_min() << std::endl;
- std::cout << "std::numeric_limits
::denorm_min(): " - << std::numeric_limits<char>::denorm_min() << std::endl;
- std::cout << "std::numeric_limits
::denorm_min(): " - << std::numeric_limits<signed char>::denorm_min() << std::endl;
- std::cout << "std::numeric_limits
::denorm_min(): " - << std::numeric_limits<unsigned char>::denorm_min() << std::endl;
- std::cout << "std::numeric_limits
::denorm_min(): " - << std::numeric_limits<wchar_t>::denorm_min() << std::endl;
- std::cout << "std::numeric_limits
::denorm_min(): " - << std::numeric_limits<char16_t>::denorm_min() << std::endl;
- std::cout << "std::numeric_limits
::denorm_min(): " - << std::numeric_limits<char32_t>::denorm_min() << std::endl;
- std::cout << "std::numeric_limits
::denorm_min(): " - << std::numeric_limits<short>::denorm_min() << std::endl;
- std::cout << "std::numeric_limits
::denorm_min(): " - << std::numeric_limits<unsigned short>::denorm_min() << std::endl;
- std::cout << "std::numeric_limits
::denorm_min(): " - << std::numeric_limits<int>::denorm_min() << std::endl;
- std::cout << "std::numeric_limits
::denorm_min(): " - << std::numeric_limits<unsigned int>::denorm_min() << std::endl;
- std::cout << "std::numeric_limits
::denorm_min(): " - << std::numeric_limits<long>::denorm_min() << std::endl;
- std::cout << "std::numeric_limits
::denorm_min(): " - << std::numeric_limits<unsigned long>::denorm_min() << std::endl;
- std::cout << "std::numeric_limits
::denorm_min(): " - << std::numeric_limits<long long>::denorm_min() << std::endl;
- std::cout << "std::numeric_limits
::denorm_min(): " - << std::numeric_limits<unsigned long long>::denorm_min() << std::endl;
- std::cout << "std::numeric_limits
::denorm_min(): " - << std::numeric_limits<float>::denorm_min() << std::endl;
- std::cout << "std::numeric_limits
::denorm_min(): " - << std::numeric_limits<double>::denorm_min() << std::endl;
- std::cout << "std::numeric_limits
::denorm_min(): " - << std::numeric_limits<long double>::denorm_min() << std::endl;
- std::cout << "std::numeric_limits
::denorm_min(): " - << std::numeric_limits<std::string>::denorm_min() << std::endl;
- //必须偏特化
- // std::cout << "std::numeric_limits
::denorm_min(): " - // << std::numeric_limits<SName>::denorm_min() << std::endl;
- std::cout << "std::numeric_limits
::denorm_min(): " - << std::numeric_limits<SPartSpec>::denorm_min() << std::endl;
- return 0;
- }