数值极限
定义于头文件 | ||
template< class T > class numeric_limits; |
numeric_limits 类模板提供查询各种算术类型属性的标准化方式(例如 int 类型的最大可能值是 std::numeric_limits<int>::max() )。
鉴别准确表示的类型
std::numeric_limits<T>::is_exact
static const bool is_exact; | (C++11 前) | |
static constexpr bool is_exact; | (C++11 起) |
std::numeric_limits<T>::is_exact 的值对所有使用准确表示的算术类型 T
为 true 。
标准特化
T | std::numeric_limits<T>::is_exact 的值 |
/* non-specialized */ | false |
bool | true |
char | true |
signed char | true |
unsigned char | true |
wchar_t | true |
char8_t | true |
char16_t | true |
char32_t | true |
short | true |
unsigned short | true |
int | true |
unsigned int | true |
long | true |
unsigned long | true |
long long | true |
unsigned long long | true |
float | false |
double | false |
long double | false |
注意
尽管对于所有 std::numeric_limits<T>::is_exact==true 的基础类型 T
都是整数类型,库可以定义非整数的准确类型,例如表示分数的有理数算术类型。
调用示例
#include <iostream>
#include <string>
#include <limits>
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;
};
}
int main()
{
std::cout << std::boolalpha;
std::cout << "std::numeric_limits<bool>::is_exact: "
<< std::numeric_limits<bool>::is_exact << std::endl;
std::cout << "std::numeric_limits<char>::is_exact: "
<< std::numeric_limits<char>::is_exact << std::endl;
std::cout << "std::numeric_limits<signed char>::is_exact: "
<< std::numeric_limits<signed char>::is_exact << std::endl;
std::cout << "std::numeric_limits<unsigned char>::is_exact: "
<< std::numeric_limits<unsigned char>::is_exact << std::endl;
std::cout << "std::numeric_limits<wchar_t>::is_exact: "
<< std::numeric_limits<wchar_t>::is_exact << std::endl;
std::cout << "std::numeric_limits<char16_t>::is_exact: "
<< std::numeric_limits<char16_t>::is_exact << std::endl;
std::cout << "std::numeric_limits<char32_t>::is_exact: "
<< std::numeric_limits<char32_t>::is_exact << std::endl;
std::cout << "std::numeric_limits<short>::is_exact: "
<< std::numeric_limits<short>::is_exact << std::endl;
std::cout << "std::numeric_limits<unsigned short>::is_exact: "
<< std::numeric_limits<unsigned short>::is_exact << std::endl;
std::cout << "std::numeric_limits<int>::is_exact: "
<< std::numeric_limits<int>::is_exact << std::endl;
std::cout << "std::numeric_limits<unsigned int>::is_exact: "
<< std::numeric_limits<unsigned int>::is_exact << std::endl;
std::cout << "std::numeric_limits<long>::is_exact: "
<< std::numeric_limits<long>::is_exact << std::endl;
std::cout << "std::numeric_limits<unsigned long>::is_exact: "
<< std::numeric_limits<unsigned long>::is_exact << std::endl;
std::cout << "std::numeric_limits<long long>::is_exact: "
<< std::numeric_limits<long long>::is_exact << std::endl;
std::cout << "std::numeric_limits<unsigned long long>::is_exact: "
<< std::numeric_limits<unsigned long long>::is_exact << std::endl;
std::cout << "std::numeric_limits<float>::is_exact: "
<< std::numeric_limits<float>::is_exact << std::endl;
std::cout << "std::numeric_limits<double>::is_exact: "
<< std::numeric_limits<double>::is_exact << std::endl;
std::cout << "std::numeric_limits<long double>::is_exact: "
<< std::numeric_limits<long double>::is_exact << std::endl;
std::cout << "std::numeric_limits<std::string>::is_exact: "
<< std::numeric_limits<std::string>::is_exact << std::endl;
std::cout << "std::numeric_limits<SName>::is_exact: "
<< std::numeric_limits<SName>::is_exact << std::endl;
std::cout << "std::numeric_limits<SPartSpec>::is_exact: "
<< std::numeric_limits<SPartSpec>::is_exact << std::endl;
return 0;
}
输出
鉴别能表示特殊值“正无穷大”的浮点类型
std::numeric_limits<T>::has_infinity
static const bool has_infinity; | (C++11 前) | |
static constexpr bool has_infinity; | (C++11 起) |
std::numeric_limits<T>::has_infinity 的值对所有能够表示正无穷大为独立特殊值的类型 T
为 true 。此常量对所有浮点类型有意义,且保证若 std::numeric_limits<T>::is_iec559 == true 则为 true 。
标准特化
T | std::numeric_limits<T>::has_infinity 的值 |
/* non-specialized */ | false |
bool | false |
char | false |
signed char | false |
unsigned char | false |
wchar_t | false |
char8_t | false |
char16_t | false |
char32_t | false |
short | false |
unsigned short | false |
int | false |
unsigned int | false |
long | false |
unsigned long | false |
long long | false |
unsigned long long | false |
float | 通常为 true |
double | 通常为 true |
long double | 通常为 true |
调用示例
#include <iostream>
#include <string>
#include <limits>
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;
};
}
int main()
{
std::cout << std::boolalpha;
std::cout << "std::numeric_limits<bool>::has_infinity: "
<< std::numeric_limits<bool>::has_infinity << std::endl;
std::cout << "std::numeric_limits<char>::has_infinity: "
<< std::numeric_limits<char>::has_infinity << std::endl;
std::cout << "std::numeric_limits<signed char>::has_infinity: "
<< std::numeric_limits<signed char>::has_infinity << std::endl;
std::cout << "std::numeric_limits<unsigned char>::has_infinity: "
<< std::numeric_limits<unsigned char>::has_infinity << std::endl;
std::cout << "std::numeric_limits<wchar_t>::has_infinity: "
<< std::numeric_limits<wchar_t>::has_infinity << std::endl;
std::cout << "std::numeric_limits<char16_t>::has_infinity: "
<< std::numeric_limits<char16_t>::has_infinity << std::endl;
std::cout << "std::numeric_limits<char32_t>::has_infinity: "
<< std::numeric_limits<char32_t>::has_infinity << std::endl;
std::cout << "std::numeric_limits<short>::has_infinity: "
<< std::numeric_limits<short>::has_infinity << std::endl;
std::cout << "std::numeric_limits<unsigned short>::has_infinity: "
<< std::numeric_limits<unsigned short>::has_infinity << std::endl;
std::cout << "std::numeric_limits<int>::has_infinity: "
<< std::numeric_limits<int>::has_infinity << std::endl;
std::cout << "std::numeric_limits<unsigned int>::has_infinity: "
<< std::numeric_limits<unsigned int>::has_infinity << std::endl;
std::cout << "std::numeric_limits<long>::has_infinity: "
<< std::numeric_limits<long>::has_infinity << std::endl;
std::cout << "std::numeric_limits<unsigned long>::has_infinity: "
<< std::numeric_limits<unsigned long>::has_infinity << std::endl;
std::cout << "std::numeric_limits<long long>::has_infinity: "
<< std::numeric_limits<long long>::has_infinity << std::endl;
std::cout << "std::numeric_limits<unsigned long long>::has_infinity: "
<< std::numeric_limits<unsigned long long>::has_infinity << std::endl;
std::cout << "std::numeric_limits<float>::has_infinity: "
<< std::numeric_limits<float>::has_infinity << std::endl;
std::cout << "std::numeric_limits<double>::has_infinity: "
<< std::numeric_limits<double>::has_infinity << std::endl;
std::cout << "std::numeric_limits<long double>::has_infinity: "
<< std::numeric_limits<long double>::has_infinity << std::endl;
std::cout << "std::numeric_limits<std::string>::has_infinity: "
<< std::numeric_limits<std::string>::has_infinity << std::endl;
std::cout << "std::numeric_limits<SName>::has_infinity: "
<< std::numeric_limits<SName>::has_infinity << std::endl;
std::cout << "std::numeric_limits<SPartSpec>::has_infinity: "
<< std::numeric_limits<SPartSpec>::has_infinity << std::endl;
return 0;
}
输出
鉴别能表示特殊值“安静的非数”( NaN )的浮点类型
std::numeric_limits<T>::has_quiet_NaN
static const bool has_quiet_NaN; | ||
static constexpr bool has_quiet_NaN; |
std::numeric_limits<T>::has_quiet_NaN 的值对所有能够表示特殊值“安静的非数”的类型 T
为 true 。此常量对所有浮点类型有意义,并保证若 std::numeric_limits<T>::is_iec559 == true 则为 true 。
标准特化
T | std::numeric_limits<T>::has_quiet_NaN 的值 |
/* non-specialized */ | false |
bool | false |
char | false |
signed char | false |
unsigned char | false |
wchar_t | false |
char8_t | false |
char16_t | false |
char32_t | false |
short | false |
unsigned short | false |
int | false |
unsigned int | false |
long | false |
unsigned long | false |
long long | false |
unsigned long long | false |
float | 通常为 true |
double | 通常为 true |
long double | 通常为 true |
调用示例
#include <iostream>
#include <string>
#include <limits>
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;
};
}
int main()
{
std::cout << std::boolalpha;
std::cout << "std::numeric_limits<bool>::has_quiet_NaN: "
<< std::numeric_limits<bool>::has_quiet_NaN << std::endl;
std::cout << "std::numeric_limits<char>::has_quiet_NaN: "
<< std::numeric_limits<char>::has_quiet_NaN << std::endl;
std::cout << "std::numeric_limits<signed char>::has_quiet_NaN: "
<< std::numeric_limits<signed char>::has_quiet_NaN << std::endl;
std::cout << "std::numeric_limits<unsigned char>::has_quiet_NaN: "
<< std::numeric_limits<unsigned char>::has_quiet_NaN << std::endl;
std::cout << "std::numeric_limits<wchar_t>::has_quiet_NaN: "
<< std::numeric_limits<wchar_t>::has_quiet_NaN << std::endl;
std::cout << "std::numeric_limits<char16_t>::has_quiet_NaN: "
<< std::numeric_limits<char16_t>::has_quiet_NaN << std::endl;
std::cout << "std::numeric_limits<char32_t>::has_quiet_NaN: "
<< std::numeric_limits<char32_t>::has_quiet_NaN << std::endl;
std::cout << "std::numeric_limits<short>::has_quiet_NaN: "
<< std::numeric_limits<short>::has_quiet_NaN << std::endl;
std::cout << "std::numeric_limits<unsigned short>::has_quiet_NaN: "
<< std::numeric_limits<unsigned short>::has_quiet_NaN << std::endl;
std::cout << "std::numeric_limits<int>::has_quiet_NaN: "
<< std::numeric_limits<int>::has_quiet_NaN << std::endl;
std::cout << "std::numeric_limits<unsigned int>::has_quiet_NaN: "
<< std::numeric_limits<unsigned int>::has_quiet_NaN << std::endl;
std::cout << "std::numeric_limits<long>::has_quiet_NaN: "
<< std::numeric_limits<long>::has_quiet_NaN << std::endl;
std::cout << "std::numeric_limits<unsigned long>::has_quiet_NaN: "
<< std::numeric_limits<unsigned long>::has_quiet_NaN << std::endl;
std::cout << "std::numeric_limits<long long>::has_quiet_NaN: "
<< std::numeric_limits<long long>::has_quiet_NaN << std::endl;
std::cout << "std::numeric_limits<unsigned long long>::has_quiet_NaN: "
<< std::numeric_limits<unsigned long long>::has_quiet_NaN << std::endl;
std::cout << "std::numeric_limits<float>::has_quiet_NaN: "
<< std::numeric_limits<float>::has_quiet_NaN << std::endl;
std::cout << "std::numeric_limits<double>::has_quiet_NaN: "
<< std::numeric_limits<double>::has_quiet_NaN << std::endl;
std::cout << "std::numeric_limits<long double>::has_quiet_NaN: "
<< std::numeric_limits<long double>::has_quiet_NaN << std::endl;
std::cout << "std::numeric_limits<std::string>::has_quiet_NaN: "
<< std::numeric_limits<std::string>::has_quiet_NaN << std::endl;
std::cout << "std::numeric_limits<SName>::has_quiet_NaN: "
<< std::numeric_limits<SName>::has_quiet_NaN << std::endl;
std::cout << "std::numeric_limits<SPartSpec>::has_quiet_NaN: "
<< std::numeric_limits<SPartSpec>::has_quiet_NaN << std::endl;
return 0;
}