数值极限
定义于头文件
template< class T > class numeric_limits;
numeric_limits 类模板提供查询各种算术类型属性的标准化方式(例如 int 类型的最大可能值是 std::numeric_limits
std::numeric_limits::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
#include
#include
#include
#include
#include
struct SName
{
};
//偏特化
struct SPartSpec
{
};
namespace std
{
template<>
struct numeric_limits
{
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::quiet_NaN() << std::endl;
std::cout << "std::numeric_limits::quiet_NaN(): "
<< std::numeric_limits::quiet_NaN() << std::endl;
std::cout << "std::numeric_limits::quiet_NaN(): "
<< std::numeric_limits::quiet_NaN() << std::endl;
std::cout << "std::numeric_limits::quiet_NaN(): "
<< std::numeric_limits::quiet_NaN() << std::endl;
std::cout << "std::numeric_limits::quiet_NaN(): "
<< std::numeric_limits::quiet_NaN() << std::endl;
std::cout << "std::numeric_limits::quiet_NaN(): "
<< std::numeric_limits::quiet_NaN() << std::endl;
std::cout << "std::numeric_limits::quiet_NaN(): "
<< std::numeric_limits::quiet_NaN() << std::endl;
std::cout << "std::numeric_limits::quiet_NaN(): "
<< std::numeric_limits::quiet_NaN() << std::endl;
std::cout << "std::numeric_limits::quiet_NaN(): "
<< std::numeric_limits::quiet_NaN() << std::endl;
std::cout << "std::numeric_limits::quiet_NaN(): "
<< std::numeric_limits::quiet_NaN() << std::endl;
std::cout << "std::numeric_limits::quiet_NaN(): "
<< std::numeric_limits::quiet_NaN() << std::endl;
std::cout << "std::numeric_limits::quiet_NaN(): "
<< std::numeric_limits::quiet_NaN() << std::endl;
std::cout << "std::numeric_limits::quiet_NaN(): "
<< std::numeric_limits::quiet_NaN() << std::endl;
std::cout << "std::numeric_limits::quiet_NaN(): "
<< std::numeric_limits::quiet_NaN() << std::endl;
std::cout << "std::numeric_limits::quiet_NaN(): "
<< std::numeric_limits::quiet_NaN() << std::endl;
std::cout << "std::numeric_limits::quiet_NaN(): "
<< std::numeric_limits::quiet_NaN() << std::endl;
std::cout << "std::numeric_limits::quiet_NaN(): "
<< std::numeric_limits::quiet_NaN() << std::endl;
std::cout << "std::numeric_limits::quiet_NaN(): "
<< std::numeric_limits::quiet_NaN() << std::endl;
std::cout << "std::numeric_limits::quiet_NaN(): "
<< std::numeric_limits::quiet_NaN() << std::endl;
//必须偏特化
// std::cout << "std::numeric_limits::quiet_NaN(): "
// << std::numeric_limits::quiet_NaN() << std::endl;
std::cout << "std::numeric_limits::quiet_NaN(): "
<< std::numeric_limits::quiet_NaN() << std::endl;
return 0;
}
std::numeric_limits::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
#include
#include
#include
#include
#include
struct SName
{
};
//偏特化
struct SPartSpec
{
};
namespace std
{
template<>
struct numeric_limits
{
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::signaling_NaN() << std::endl;
std::cout << "std::numeric_limits::signaling_NaN(): "
<< std::numeric_limits::signaling_NaN() << std::endl;
std::cout << "std::numeric_limits::signaling_NaN(): "
<< std::numeric_limits::signaling_NaN() << std::endl;
std::cout << "std::numeric_limits::signaling_NaN(): "
<< std::numeric_limits::signaling_NaN() << std::endl;
std::cout << "std::numeric_limits::signaling_NaN(): "
<< std::numeric_limits::signaling_NaN() << std::endl;
std::cout << "std::numeric_limits::signaling_NaN(): "
<< std::numeric_limits::signaling_NaN() << std::endl;
std::cout << "std::numeric_limits::signaling_NaN(): "
<< std::numeric_limits::signaling_NaN() << std::endl;
std::cout << "std::numeric_limits::signaling_NaN(): "
<< std::numeric_limits::signaling_NaN() << std::endl;
std::cout << "std::numeric_limits::signaling_NaN(): "
<< std::numeric_limits::signaling_NaN() << std::endl;
std::cout << "std::numeric_limits::signaling_NaN(): "
<< std::numeric_limits::signaling_NaN() << std::endl;
std::cout << "std::numeric_limits::signaling_NaN(): "
<< std::numeric_limits::signaling_NaN() << std::endl;
std::cout << "std::numeric_limits::signaling_NaN(): "
<< std::numeric_limits::signaling_NaN() << std::endl;
std::cout << "std::numeric_limits::signaling_NaN(): "
<< std::numeric_limits::signaling_NaN() << std::endl;
std::cout << "std::numeric_limits::signaling_NaN(): "
<< std::numeric_limits::signaling_NaN() << std::endl;
std::cout << "std::numeric_limits::signaling_NaN(): "
<< std::numeric_limits::signaling_NaN() << std::endl;
std::cout << "std::numeric_limits::signaling_NaN(): "
<< std::numeric_limits::signaling_NaN() << std::endl;
std::cout << "std::numeric_limits::signaling_NaN(): "
<< std::numeric_limits::signaling_NaN() << std::endl;
std::cout << "std::numeric_limits::signaling_NaN(): "
<< std::numeric_limits::signaling_NaN() << std::endl;
std::cout << "std::numeric_limits::signaling_NaN(): "
<< std::numeric_limits::signaling_NaN() << std::endl;
//必须偏特化
// std::cout << "std::numeric_limits::signaling_NaN(): "
// << std::numeric_limits::signaling_NaN() << std::endl;
std::cout << "std::numeric_limits::signaling_NaN(): "
<< std::numeric_limits::signaling_NaN() << std::endl;
return 0;
}
std::numeric_limits::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
#include
#include
#include
#include
#include
struct SName
{
};
//偏特化
struct SPartSpec
{
};
namespace std
{
template<>
struct numeric_limits
{
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::denorm_min() << std::endl;
std::cout << "std::numeric_limits::denorm_min(): "
<< std::numeric_limits::denorm_min() << std::endl;
std::cout << "std::numeric_limits::denorm_min(): "
<< std::numeric_limits::denorm_min() << std::endl;
std::cout << "std::numeric_limits::denorm_min(): "
<< std::numeric_limits::denorm_min() << std::endl;
std::cout << "std::numeric_limits::denorm_min(): "
<< std::numeric_limits::denorm_min() << std::endl;
std::cout << "std::numeric_limits::denorm_min(): "
<< std::numeric_limits::denorm_min() << std::endl;
std::cout << "std::numeric_limits::denorm_min(): "
<< std::numeric_limits::denorm_min() << std::endl;
std::cout << "std::numeric_limits::denorm_min(): "
<< std::numeric_limits::denorm_min() << std::endl;
std::cout << "std::numeric_limits::denorm_min(): "
<< std::numeric_limits::denorm_min() << std::endl;
std::cout << "std::numeric_limits::denorm_min(): "
<< std::numeric_limits::denorm_min() << std::endl;
std::cout << "std::numeric_limits::denorm_min(): "
<< std::numeric_limits::denorm_min() << std::endl;
std::cout << "std::numeric_limits::denorm_min(): "
<< std::numeric_limits::denorm_min() << std::endl;
std::cout << "std::numeric_limits::denorm_min(): "
<< std::numeric_limits::denorm_min() << std::endl;
std::cout << "std::numeric_limits::denorm_min(): "
<< std::numeric_limits::denorm_min() << std::endl;
std::cout << "std::numeric_limits::denorm_min(): "
<< std::numeric_limits::denorm_min() << std::endl;
std::cout << "std::numeric_limits::denorm_min(): "
<< std::numeric_limits::denorm_min() << std::endl;
std::cout << "std::numeric_limits::denorm_min(): "
<< std::numeric_limits::denorm_min() << std::endl;
std::cout << "std::numeric_limits::denorm_min(): "
<< std::numeric_limits::denorm_min() << std::endl;
std::cout << "std::numeric_limits::denorm_min(): "
<< std::numeric_limits::denorm_min() << std::endl;
//必须偏特化
// std::cout << "std::numeric_limits::denorm_min(): "
// << std::numeric_limits::denorm_min() << std::endl;
std::cout << "std::numeric_limits::denorm_min(): "
<< std::numeric_limits::denorm_min() << std::endl;
return 0;
}