std::numeric_limits
来自 zh.cppreference.net
| 定义于头文件 <limits>
|
||
template< class T > class numeric_limits;
|
||
类模板 std::numeric_limits 提供了一种标准化方式来查询算术类型的各种属性(例如,类型 int 的最大可能值是 std::numeric_limits<int>::max())。
这些信息通过 std::numeric_limits 模板的特化提供。标准库 为所有算术类型提供了特化(仅列出 cv 未限定算术类型的特化):
| 定义于头文件 <limits>
|
||
template<> class numeric_limits<bool>;
|
||
template<> class numeric_limits<char>;
|
||
template<> class numeric_limits<signed char>;
|
||
template<> class numeric_limits<unsigned char>;
|
||
template<> class numeric_limits<wchar_t>;
|
||
template<> class numeric_limits<char8_t>;
|
(自 C++20 起) | |
template<> class numeric_limits<char16_t>;
|
(自 C++11 起) | |
template<> class numeric_limits<char32_t>;
|
(自 C++11 起) | |
template<> class numeric_limits<short>;
|
||
template<> class numeric_limits<unsigned short>;
|
||
template<> class numeric_limits<int>;
|
||
template<> class numeric_limits<unsigned int>;
|
||
template<> class numeric_limits<long>;
|
||
template<> class numeric_limits<unsigned long>;
|
||
template<> class numeric_limits<long long>;
|
(自 C++11 起) | |
template<> class numeric_limits<unsigned long long>;
|
(自 C++11 起) | |
template<> class numeric_limits<float>;
|
||
template<> class numeric_limits<double>;
|
||
template<> class numeric_limits<long double>;
|
||
对于 cv 限定类型 std::numeric_limitscv 上的 T 特化,每个成员的值等于未限定类型 T 上特化对应成员的值。例如,std::numeric_limits<int>::digits 等于 std::numeric_limits<const int>::digits。
算术类型的别名(例如 std::size_t 或 std::streamsize)也可以使用 std::numeric_limits 类型特性进行检查。
非算术标准类型(例如 std::complex<T> 或 std::nullptr_t)没有特化。
|
如果实现定义了任何 整数类类型,则必须为它们提供 |
(自 C++20 起) |
实现可以为实现特定的类型提供 std::numeric_limits 的特化:例如,GCC 提供了 std::numeric_limits<__int128>。非标准库可以 为库提供的类型添加特化,例如 OpenEXR 为 16 位浮点类型提供了 std::numeric_limits<half>。
模板参数
| T | - | 用于获取数值属性的类型 |
成员常量
|
[static]
|
标识已特化
std::numeric_limits
的类型
(公开静态成员常量) |
|
[static]
|
标识有符号类型
(公开静态成员常量) |
|
[static]
|
标识整数类型
(公开静态成员常量) |
|
[static]
|
标识精确类型
(公开静态成员常量) |
|
[static]
|
标识可表示特殊值"正无穷大"的浮点类型
(公开静态成员常量) |
|
[static]
|
标识可表示特殊值"静默非数" (NaN) 的浮点类型
(公开静态成员常量) |
|
[static]
|
标识可表示特殊值"信号非数" (NaN) 的浮点类型
(公开静态成员常量) |
|
[static]
|
标识浮点类型使用的非规范化风格
(公开静态成员常量) |
|
[static]
|
标识将精度损失检测为非规范化损失而非不精确结果的浮点类型
(公开静态成员常量) |
|
[static]
|
标识类型使用的舍入风格
(公开静态成员常量) |
|
[static]
|
标识符合 IEC 559/IEEE 754 标准的浮点类型
(公开静态成员常量) |
|
[static]
|
标识表示有限值集的类型
(公开静态成员常量) |
|
[static]
|
标识使用模运算处理溢出的类型
(公开静态成员常量) |
|
[static]
|
可无更改表示的
radix
进制位数
(公开静态成员常量) |
|
[static]
|
可无更改表示的十进制位数
(公开静态成员常量) |
|
[static]
(C++11)
|
区分此类型所有值所需的十进制位数
(公开静态成员常量) |
|
[static]
|
给定类型表示所使用的基数
(公开静态成员常量) |
|
[static]
|
基数的最大负整数幂加一,该幂是有效的规范化浮点值
(公开静态成员常量) |
|
[static]
|
10 的最大负整数幂,该幂是有效的规范化浮点值
(公开静态成员常量) |
|
<a href="numeric_ |
成员函数
|
[static]
|
返回给定非浮点类型的最小有限值,或给定浮点类型的最小正规格化值
(公开静态成员函数) |
|
[static]
(C++11)
|
返回给定类型的最低有限值,即有符号类型的最负值,无符号类型的
0
(公开静态成员函数) |
|
[static]
|
返回给定类型的最大有限值
(公开静态成员函数) |
|
[static]
|
返回
1.0
与给定浮点类型的下一个可表示值之间的差值
(公开静态成员函数) |
|
[static]
|
返回给定浮点类型的最大舍入误差
(公开静态成员函数) |
|
[static]
|
返回给定浮点类型的正无穷大值
(公开静态成员函数) |
|
[static]
|
返回给定浮点类型的静默 NaN 值
(公开静态成员函数) |
|
[static]
|
返回给定浮点类型的信号 NaN 值
(公开静态成员函数) |
|
[static]
|
返回给定浮点类型的最小正次正规值
(公开静态成员函数) |
辅助类
|
指示浮点数舍入模式
(enum) |
|
|
指示浮点数非规范化模式
(enum) |
与 C 库宏常量的关系
特化
std::numeric_limits<T>
其中
T
为
|
成员 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
min()
|
lowest()
(C++11) |
max()
|
radix
|
||||||||
| bool | false | false | true | 2 | |||||||
| char | CHAR_MIN | CHAR_MIN | CHAR_MAX | 2 | |||||||
| signed char | SCHAR_MIN | SCHAR_MIN | SCHAR_MAX | 2 | |||||||
| unsigned char | 0 | 0 | UCHAR_MAX | 2 | |||||||
| wchar_t | WCHAR_MIN | WCHAR_MIN | WCHAR_MAX | 2 | |||||||
| char8_t | 0 | 0 | UCHAR_MAX | 2 | |||||||
| char16_t | 0 | 0 | UINT_LEAST16_MAX | 2 | |||||||
| char32_t | 0 | 0 | UINT_LEAST32_MAX | 2 | |||||||
| short | SHRT_MIN | SHRT_MIN | SHRT_MAX | 2 | |||||||
| signed short | |||||||||||
| unsigned short | 0 | 0 | USHRT_MAX | 2 | |||||||
| int | INT_MIN | INT_MIN | INT_MAX | 2 | |||||||
| signed int | |||||||||||
| unsigned int | 0 | 0 | UINT_MAX | 2 | |||||||
| long | LONG_MIN | LONG_MIN | LONG_MAX | 2 | |||||||
| signed long | |||||||||||
| unsigned long | 0 | 0 | ULONG_MAX | 2 | |||||||
| long long | LLONG_MIN | LLONG_MIN | LLONG_MAX | 2 | |||||||
| signed long long | |||||||||||
| unsigned long long | 0 | 0 | ULLONG_MAX | 2 | |||||||
特化
std::numeric_limits<T>
其中
T
为
|
成员 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
denorm_min()
|
min()
|
lowest()
(C++11) |
max()
|
epsilon()
|
digits
|
digits10
|
|||||
| float | FLT_TRUE_MIN | FLT_MIN | - FLT_MAX | FLT_MAX | FLT_EPSILON | FLT_MANT_DIG | FLT_DIG | ||||
| double | DBL_TRUE_MIN | DBL_MIN | - DBL_MAX | DBL_MAX | DBL_EPSILON | DBL_MANT_DIG | DBL_DIG | ||||
| long double | LDBL_TRUE_MIN | LDBL_MIN | - LDBL_MAX | LDBL_MAX | LDBL_EPSILON | LDBL_MANT_DIG | LDBL_DIG | ||||
特化
std::numeric_limits<T>
其中
T
为
|
成员(续) | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
min_exponent
|
min_exponent10
|
max_exponent
|
max_exponent10
|
radix
|
|||||||
| float | FLT_MIN_EXP | FLT_MIN_10_EXP | FLT_MAX_EXP | FLT_MAX_10_EXP | FLT_RADIX | ||||||
| double | DBL_MIN_EXP | DBL_MIN_10_EXP | DBL_MAX_EXP | DBL_MAX_10_EXP | FLT_RADIX | ||||||
| long double | LDBL_MIN_EXP | LDBL_MIN_10_EXP | LDBL_MAX_EXP | LDBL_MAX_10_EXP | FLT_RADIX | ||||||
示例
运行此代码
#include <iostream> #include <limits> int main() { std::cout << "type\t│ lowest()\t│ min()\t\t│ max()\n" << "bool\t│ " << std::numeric_limits<bool>::lowest() << "\t\t│ " << std::numeric_limits<bool>::min() << "\t\t│ " << std::numeric_limits<bool>::max() << '\n' << "uchar\t│ " << +std::numeric_limits<unsigned char>::lowest() << "\t\t│ " << +std::numeric_limits<unsigned char>::min() << "\t\t│ " << +std::numeric_limits<unsigned char>::max() << '\n' << "int\t│ " << std::numeric_limits<int>::lowest() << "\t│ " << std::numeric_limits<int>::min() << "\t│ " << std::numeric_limits<int>::max() << '\n' << "float\t│ " << std::numeric_limits<float>::lowest() << "\t│ " << std::numeric_limits<float>::min() << "\t│ " << std::numeric_limits<float>::max() << '\n' << "double\t│ " << std::numeric_limits<double>::lowest() << "\t│ " << std::numeric_limits<double>::min() << "\t│ " << std::numeric_limits<double>::max() << '\n'; }
可能的输出:
type │ lowest() │ min() │ max() bool │ 0 │ 0 │ 1 uchar │ 0 │ 0 │ 255 int │ -2147483648 │ -2147483648 │ 2147483647 float │ -3.40282e+38 │ 1.17549e-38 │ 3.40282e+38 double │ -1.79769e+308 │ 2.22507e-308 │ 1.79769e+308
缺陷报告
下列行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
| 缺陷报告 | 适用范围 | 发布时行为 | 正确行为 |
|---|---|---|---|
| LWG 201 | C++98 | 需要提供所有基本类型的特化 | 排除非算术类型 |
| LWG 559 | C++98 |
未明确说明
std::numeric_limits
对cv限定类型的特化是否与对应非cv限定类型 的特化行为一致 |
二者具有
相同行为 |