数值极限
提供查询所有基础数值类型的性质的接口
定义于头文件 <limits>
template< class T > class numeric_limits;
numeric_limits 类模板提供查询各种算术类型属性的标准化方式(例如 int 类型的最大可能值是 std::numeric_limits<int>::max() )。
成员函数
返回给定类型的最小有限值
std::numeric_limits<T>::min
|------------------------------------|---|-----------|
| static T min() throw(); | | (C++11 前) |
| static constexpr T min() noexcept; | | (C++11 起) |
返回数值类型 T
可表示的最小有限值。
对于有非正规的浮点类型, min
返回最小正正规值。注意此行为可能是非预期的 ,特别是在与 min
对整数类型的行为比较时。为找到没有比它更小的值的值,请用 numeric_limits::lowest 。
min
仅对有界类型和无界无符号类型意义,即表示负值的无限集的类型无有意义的最小值。
返回值
|-------------------------|---------------------------------|
| T
| std::numeric_limits<T>::min() |
| /* non-specialized */ | T()
|
| bool | false |
| char | CHAR_MIN |
| signed char | SCHAR_MIN |
| unsigned char | 0 |
| wchar_t | WCHAR_MIN |
| char8_t | 0 |
| char16_t | 0 |
| char32_t | 0 |
| short | SHRT_MIN |
| unsigned short | 0 |
| int | INT_MIN |
| unsigned int | 0 |
| long | LONG_MIN |
| unsigned long | 0 |
| long long | LLONG_MIN |
| unsigned long long | 0 |
| float | FLT_MIN |
| double | DBL_MIN |
| long double | LDBL_MIN |
调用示例
#include <iostream>
#include <string>
#include <limits>
#include <cstdint>
#include <cfloat>
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 ; }
};
}
int main()
{
std::cout << std::boolalpha;
std::cout << "std::numeric_limits<bool>::min(): "
<< std::numeric_limits<bool>::min() << std::endl;
std::cout << "std::numeric_limits<char>::min(): "
<< std::numeric_limits<char>::min() << std::endl;
std::cout << "std::numeric_limits<signed char>::min(): "
<< std::numeric_limits<signed char>::min() << std::endl;
std::cout << "std::numeric_limits<unsigned char>::min(): "
<< std::numeric_limits<unsigned char>::min() << std::endl;
std::cout << "std::numeric_limits<wchar_t>::min(): "
<< std::numeric_limits<wchar_t>::min() << std::endl;
std::cout << "std::numeric_limits<char16_t>::min(): "
<< std::numeric_limits<char16_t>::min() << std::endl;
std::cout << "std::numeric_limits<char32_t>::min(): "
<< std::numeric_limits<char32_t>::min() << std::endl;
std::cout << "std::numeric_limits<short>::min(): "
<< std::numeric_limits<short>::min() << std::endl;
std::cout << "std::numeric_limits<unsigned short>::min(): "
<< std::numeric_limits<unsigned short>::min() << std::endl;
std::cout << "std::numeric_limits<int>::min(): "
<< std::numeric_limits<int>::min() << std::endl;
std::cout << "std::numeric_limits<unsigned int>::min(): "
<< std::numeric_limits<unsigned int>::min() << std::endl;
std::cout << "std::numeric_limits<long>::min(): "
<< std::numeric_limits<long>::min() << std::endl;
std::cout << "std::numeric_limits<unsigned long>::min(): "
<< std::numeric_limits<unsigned long>::min() << std::endl;
std::cout << "std::numeric_limits<long long>::min(): "
<< std::numeric_limits<long long>::min() << std::endl;
std::cout << "std::numeric_limits<unsigned long long>::min(): "
<< std::numeric_limits<unsigned long long>::min() << std::endl;
std::cout << "std::numeric_limits<float>::min(): "
<< std::numeric_limits<float>::min() << std::endl;
std::cout << "std::numeric_limits<double>::min(): "
<< std::numeric_limits<double>::min() << std::endl;
std::cout << "std::numeric_limits<long double>::min(): "
<< std::numeric_limits<long double>::min() << std::endl;
std::cout << "std::numeric_limits<std::string>::min(): "
<< std::numeric_limits<std::string>::min() << std::endl;
//必须偏特化
// std::cout << "std::numeric_limits<SName>::min(): "
// << std::numeric_limits<SName>::min() << std::endl;
std::cout << "std::numeric_limits<SPartSpec>::min(): "
<< std::numeric_limits<SPartSpec>::min() << std::endl;
return 0;
}
输出
返回给定类型的最低有限值
std::numeric_limits<T>::lowest
|---------------------------------------|---|-----------|
| static constexpr T lowest() noexcept; | | (C++11 起) |
返回数值类型 T
可表示的最低有限值,即满足无其他有限值 y
符合 y < x
的有限值 x
。这对于浮点类型不同于 std::numeric_limits<T>::min() 。仅对有界类型有意义。
返回值
|-------------------------|------------------------------------|
| T
| std::numeric_limits<T>::lowest() |
| /* non-specialized */ | T();
|
| bool | false |
| char | CHAR_MIN |
| signed char | SCHAR_MIN |
| unsigned char | 0 |
| wchar_t | WCHAR_MIN |
| char8_t | 0 |
| char16_t | 0 |
| char32_t | 0 |
| short | SHRT_MIN |
| unsigned short | 0 |
| int | INT_MIN |
| unsigned int | 0 |
| long | LONG_MIN |
| unsigned long | 0 |
| long long | LLONG_MIN |
| unsigned long long | 0 |
| float | -FLT_MAX |
| double | -DBL_MAX |
| long double | -LDBL_MAX |
注意
尽管对于基础 C++ 浮点类型非真,可存在满足 std::numeric_limits<T>::lowest() != -std::numeric_limits<T>::max() 的第三方浮点类型 T
。\
调用示例
#include <iostream>
#include <string>
#include <limits>
#include <cstdint>
#include <cfloat>
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 ; }
};
}
int main()
{
std::cout << std::boolalpha;
std::cout << "std::numeric_limits<bool>::lowest(): "
<< std::numeric_limits<bool>::lowest() << std::endl;
std::cout << "std::numeric_limits<char>::lowest(): "
<< std::numeric_limits<char>::lowest() << std::endl;
std::cout << "std::numeric_limits<signed char>::lowest(): "
<< std::numeric_limits<signed char>::lowest() << std::endl;
std::cout << "std::numeric_limits<unsigned char>::lowest(): "
<< std::numeric_limits<unsigned char>::lowest() << std::endl;
std::cout << "std::numeric_limits<wchar_t>::lowest(): "
<< std::numeric_limits<wchar_t>::lowest() << std::endl;
std::cout << "std::numeric_limits<char16_t>::lowest(): "
<< std::numeric_limits<char16_t>::lowest() << std::endl;
std::cout << "std::numeric_limits<char32_t>::lowest(): "
<< std::numeric_limits<char32_t>::lowest() << std::endl;
std::cout << "std::numeric_limits<short>::lowest(): "
<< std::numeric_limits<short>::lowest() << std::endl;
std::cout << "std::numeric_limits<unsigned short>::lowest(): "
<< std::numeric_limits<unsigned short>::lowest() << std::endl;
std::cout << "std::numeric_limits<int>::lowest(): "
<< std::numeric_limits<int>::lowest() << std::endl;
std::cout << "std::numeric_limits<unsigned int>::lowest(): "
<< std::numeric_limits<unsigned int>::lowest() << std::endl;
std::cout << "std::numeric_limits<long>::lowest(): "
<< std::numeric_limits<long>::lowest() << std::endl;
std::cout << "std::numeric_limits<unsigned long>::lowest(): "
<< std::numeric_limits<unsigned long>::lowest() << std::endl;
std::cout << "std::numeric_limits<long long>::lowest(): "
<< std::numeric_limits<long long>::lowest() << std::endl;
std::cout << "std::numeric_limits<unsigned long long>::lowest(): "
<< std::numeric_limits<unsigned long long>::lowest() << std::endl;
std::cout << "std::numeric_limits<float>::lowest(): "
<< std::numeric_limits<float>::lowest() << std::endl;
std::cout << "std::numeric_limits<double>::lowest(): "
<< std::numeric_limits<double>::lowest() << std::endl;
std::cout << "std::numeric_limits<long double>::lowest(): "
<< std::numeric_limits<long double>::lowest() << std::endl;
std::cout << "std::numeric_limits<std::string>::lowest(): "
<< std::numeric_limits<std::string>::lowest() << std::endl;
//必须偏特化
// std::cout << "std::numeric_limits<SName>::lowest(): "
// << std::numeric_limits<SName>::lowest() << std::endl;
std::cout << "std::numeric_limits<SPartSpec>::lowest(): "
<< std::numeric_limits<SPartSpec>::lowest() << std::endl;
return 0;
}
输出
返回给定类型的最大有限值
std::numeric_limits<T>::max
|------------------------------------|---|-----------|
| static T max() throw(); | | (C++11 前) |
| static constexpr T max() noexcept; | | (C++11 起) |
返回数值类型 T
所能表示的最大有限值。对所有有界类型有意义。
返回值
|-------------------------|---------------------------------|
| T
| std::numeric_limits<T>::max() |
| /* non-specialized */ | T()
|
| bool | true |
| char | CHAR_MAX |
| signed char | SCHAR_MAX |
| unsigned char | UCHAR_MAX |
| wchar_t | WCHAR_MAX |
| char8_t | UCHAR_MAX |
| char16_t | UINT_LEAST16_MAX |
| char32_t | UINT_LEAST32_MAX |
| short | SHRT_MAX |
| unsigned short | USHRT_MAX |
| int | INT_MAX |
| unsigned int | UINT_MAX |
| long | LONG_MAX |
| unsigned long | ULONG_MAX |
| long long | LLONG_MAX |
| unsigned long long | ULLONG_MAX |
| float | FLT_MAX |
| double | DBL_MAX |
| long double | LDBL_MAX |
调用示例
#include <iostream>
#include <string>
#include <limits>
#include <cstdint>
#include <cfloat>
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 ; }
};
}
int main()
{
std::cout << std::boolalpha;
std::cout << "std::numeric_limits<bool>::max(): "
<< std::numeric_limits<bool>::max() << std::endl;
std::cout << "std::numeric_limits<char>::max(): "
<< std::numeric_limits<char>::max() << std::endl;
std::cout << "std::numeric_limits<signed char>::max(): "
<< std::numeric_limits<signed char>::max() << std::endl;
std::cout << "std::numeric_limits<unsigned char>::max(): "
<< std::numeric_limits<unsigned char>::max() << std::endl;
std::cout << "std::numeric_limits<wchar_t>::max(): "
<< std::numeric_limits<wchar_t>::max() << std::endl;
std::cout << "std::numeric_limits<char16_t>::max(): "
<< std::numeric_limits<char16_t>::max() << std::endl;
std::cout << "std::numeric_limits<char32_t>::max(): "
<< std::numeric_limits<char32_t>::max() << std::endl;
std::cout << "std::numeric_limits<short>::max(): "
<< std::numeric_limits<short>::max() << std::endl;
std::cout << "std::numeric_limits<unsigned short>::max(): "
<< std::numeric_limits<unsigned short>::max() << std::endl;
std::cout << "std::numeric_limits<int>::max(): "
<< std::numeric_limits<int>::max() << std::endl;
std::cout << "std::numeric_limits<unsigned int>::max(): "
<< std::numeric_limits<unsigned int>::max() << std::endl;
std::cout << "std::numeric_limits<long>::max(): "
<< std::numeric_limits<long>::max() << std::endl;
std::cout << "std::numeric_limits<unsigned long>::max(): "
<< std::numeric_limits<unsigned long>::max() << std::endl;
std::cout << "std::numeric_limits<long long>::max(): "
<< std::numeric_limits<long long>::max() << std::endl;
std::cout << "std::numeric_limits<unsigned long long>::max(): "
<< std::numeric_limits<unsigned long long>::max() << std::endl;
std::cout << "std::numeric_limits<float>::max(): "
<< std::numeric_limits<float>::max() << std::endl;
std::cout << "std::numeric_limits<double>::max(): "
<< std::numeric_limits<double>::max() << std::endl;
std::cout << "std::numeric_limits<long double>::max(): "
<< std::numeric_limits<long double>::max() << std::endl;
std::cout << "std::numeric_limits<std::string>::max(): "
<< std::numeric_limits<std::string>::max() << std::endl;
//必须偏特化
// std::cout << "std::numeric_limits<SName>::max(): "
// << std::numeric_limits<SName>::max() << std::endl;
std::cout << "std::numeric_limits<SPartSpec>::max(): "
<< std::numeric_limits<SPartSpec>::max() << std::endl;
return 0;
}