- C 标准库
- C 标准库
- C++ 标准库
- C++ 库 - 首页
- C++ 库 - <fstream>
- C++ 库 - <iomanip>
- C++ 库 - <ios>
- C++ 库 - <iosfwd>
- C++ 库 - <iostream>
- C++ 库 - <istream>
- C++ 库 - <ostream>
- C++ 库 - <sstream>
- C++ 库 - <streambuf>
- C++ 库 - <atomic>
- C++ 库 - <complex>
- C++ 库 - <exception>
- C++ 库 - <functional>
- C++ 库 - <limits>
- C++ 库 - <locale>
- C++ 库 - <memory>
- C++ 库 - <new>
- C++ 库 - <numeric>
- C++ 库 - <regex>
- C++ 库 - <stdexcept>
- C++ 库 - <string>
- C++ 库 - <thread>
- C++ 库 - <tuple>
- C++ 库 - <typeinfo>
- C++ 库 - <utility>
- C++ 库 - <valarray>
- C++ STL 库
- C++ 库 - <array>
- C++ 库 - <bitset>
- C++ 库 - <deque>
- C++ 库 - <forward_list>
- C++ 库 - <list>
- C++ 库 - <map>
- C++ 库 - <multimap>
- C++ 库 - <queue>
- C++ 库 - <priority_queue>
- C++ 库 - <set>
- C++ 库 - <stack>
- C++ 库 - <unordered_map>
- C++ 库 - <unordered_set>
- C++ 库 - <vector>
- C++ 库 - <algorithm>
- C++ 库 - <iterator>
- C++ 高级库
- C++ 库 - <any>
- C++ 库 - <barrier>
- C++ 库 - <bit>
- C++ 库 - <chrono>
- C++ 库 - <cinttypes>
- C++ 库 - <clocale>
- C++ 库 - <condition_variable>
- C++ 库 - <coroutine>
- C++ 库 - <cstdlib>
- C++ 库 - <cstring>
- C++ 库 - <cuchar>
- C++ 库 - <charconv>
- C++ 库 - <cfenv>
- C++ 库 - <cmath>
- C++ 库 - <ccomplex>
- C++ 库 - <expected>
- C++ 库 - <format>
- C++ 库 - <future>
- C++ 库 - <flat_set>
- C++ 库 - <flat_map>
- C++ 库 - <filesystem>
- C++ 库 - <generator>
- C++ 库 - <initializer_list>
- C++ 库 - <latch>
- C++ 库 - <memory_resource>
- C++ 库 - <mutex>
- C++ 库 - <mdspan>
- C++ 库 - <optional>
- C++ 库 - <print>
- C++ 库 - <ratio>
- C++ 库 - <scoped_allocator>
- C++ 库 - <semaphore>
- C++ 库 - <source_location>
- C++ 库 - <span>
- C++ 库 - <spanstream>
- C++ 库 - <stacktrace>
- C++ 库 - <stop_token>
- C++ 库 - <syncstream>
- C++ 库 - <system_error>
- C++ 库 - <string_view>
- C++ 库 - <stdatomic>
- C++ 库 - <variant>
- C++ STL 库速查表
- C++ STL - 速查表
C++ 库 - <limits>
介绍
这是一个数值极限类型,它提供关于特定平台上算术类型(整数或浮点数)属性的信息,该平台是库编译的目标平台。
声明
以下是 std::numeric_limits 的声明。
template <class T> numeric_limits;
C++11
template <class T> numeric_limits;
参数
T − 它是一个类类型。
示例
以下示例演示 std::numeric_limits 的用法。
#include <limits>
#include <iostream>
int main() {
std::cout << "type\tlowest type\thighest type\n";
std::cout << "int\t"
<< std::numeric_limits<int>::lowest() << '\t'
<< std::numeric_limits<int>::max() << '\n';
std::cout << "float\t"
<< std::numeric_limits<float>::lowest() << '\t'
<< std::numeric_limits<float>::max() << '\n';
std::cout << "double\t"
<< std::numeric_limits<double>::lowest() << '\t'
<< std::numeric_limits<double>::max() << '\n';
}
示例输出应如下所示:
type lowest type highest type int -2147483648 2147483647 float -3.40282e+38 3.40282e+38 double -1.79769e+308 1.79769e+308
模板实例化
C++98 的基本算术类型应如下所示:
| 基本算术类型 | |
|---|---|
| 整数类型 | bool |
char |
|
wchar_t |
|
signed char |
|
short int |
|
int |
|
long int |
|
unsigned char |
|
unsigned short int |
|
unsigned int |
|
unsigned long int |
|
| 浮点数类型 | float |
double |
|
long double |
|
C++11 的基本算术类型应如下所示:
| 基本算术类型 | |
|---|---|
| 整数类型 | bool |
char |
|
char16_t |
|
char32_t |
|
wchar_t |
|
signed char |
|
short int |
|
int |
|
long int |
|
long long int |
|
unsigned char |
|
unsigned short int |
|
unsigned int |
|
unsigned long int |
|
unsigned long long int |
|
| 浮点数类型 | float |
double |
|
long double |
|
成员
| 成员 | 类型 | 属性 |
|---|---|---|
| is_specialized | bool |
对于所有类型(即,numeric_limits 已对其进行专门化的类型),该值为true;对于所有其他类型,该值为false。 |
| min() | T | 最小有限值。 对于具有非规格化(指数位数可变)的浮点类型:最小正规格化值。 等效于CHAR_MIN、SCHAR_MIN、SHRT_MIN、INT_MIN、LONG_MIN、LLONG_MIN、FLT_MIN、DBL_MIN、LDBL_MIN 或 |
| max() | T | 最大有限值。 等效于CHAR_MAX、SCHAR_MAX、UCHAR_MAX、SHRT_MAX、USHRT_MAX、INT_MAX、UINT_MAX、LONG_MAX、ULONG_MAX、LLONG_MAX、ULLONG_MAX、UINT_LEAST16_MAX、UINT_LEAST32_MAX、FLT_MAX、DBL_MAX 或LDBL_MAX,具体取决于类型。 |
| lowest() | T | 最小有限值。(自 C++11 起) 对于整数类型:与min()相同。 对于浮点类型:实现相关;通常是max()的负值。 |
| digits | int | 对于整数类型:表示中非符号位的数量(基数进制位数)。 对于浮点类型:尾数中的位数(基数进制)(等效于FLT_MANT_DIG、DBL_MANT_DIG 或LDBL_MANT_DIG)。 |
| digits10 | int |
可以不变地表示的位数(十进制基数)。 对于浮点类型,等效于FLT_DIG、DBL_DIG 或LDBL_DIG。 |
| max_digits10 | int |
确保不同值始终不同的所需位数(十进制基数)。 |
| is_signed | bool |
如果类型是有符号的,则为true。 |
| is_integer | bool |
如果类型是整数,则为true。 |
| is_exact | bool |
如果类型使用精确表示,则为true。 |
| radix | int |
对于整数类型:表示的基数。 对于浮点类型:表示的指数的基数(等效于FLT_RADIX)。 |
| epsilon() | T | 机器ε(1 与大于 1 的最小可表示值之间的差值)。 对于浮点类型,等效于FLT_EPSILON、DBL_EPSILON 或LDBL_EPSILON。 |
| round_error() | T | 最大舍入误差的度量。 |
| min_exponent | int |
最小负整数,使得基数提高到 对于浮点类型,等效于FLT_MIN_EXP、DBL_MIN_EXP 或LDBL_MIN_EXP。 |
| min_exponent10 | int |
最小负整数,使得 10 提高到该次方会生成一个规范化的浮点数。 对于浮点类型,等效于FLT_MIN_10_EXP、DBL_MIN_10_EXP 或LDBL_MIN_10_EXP。 |
| max_exponent | int |
最大整数,使得基数提高到 对于浮点类型,等效于FLT_MAX_EXP、DBL_MAX_EXP 或LDBL_MAX_EXP。 |
| max_exponent10 | int |
最大整数,使得 10 提高到该次方会生成一个规范化的有限浮点数。 对于浮点类型,等效于FLT_MAX_10_EXP、DBL_MAX_10_EXP 或LDBL_MAX_10_EXP。 |
| has_infinity | bool |
如果类型具有正无穷大的表示,则为true。 |
| has_quiet_NaN | bool |
如果类型具有静默(非信号)“非数字”的表示,则为true。 |
| has_signaling_NaN | bool |
如果类型具有信号“非数字”的表示,则为true。 |
| has_denorm | float_denorm_style | 非规格化值(具有可变数量的指数位的表示)。一个类型可以具有以下任何枚举值: denorm_absent,如果它不允许非规格化值。 denorm_present,如果它允许非规格化值。 denorm_indeterminate,如果在编译时不确定。 |
| has_denorm_loss | bool |
如果检测到精度损失是非规格化损失,而不是不精确的结果,则为true。 |
| infinity() | T | 如果可用,则表示正无穷大。 |
| quiet_NaN() | T | 如果可用,则表示静默(非信号)“非数字”。 |
| signaling_NaN() | T | 如果可用,则表示信号“非数字”。 |
| denorm_min() | T | 最小正非规格化值。 对于不允许非规格化值的类型:与 |
| is_iec559 | bool |
如果类型符合 IEC-559/IEEE-754 标准,则为 IEC-559 类型始终将has_infinity、has_quiet_NaN 和has_signaling_NaN 设置为 |
| is_bounded | bool |
如果该类型表示的值集是有限的,则为true。 |
| is_modulo | bool |
如果类型是模数,则为true。如果可以将两个正数相加并得到一个环绕到小于它们的第三个数的结果,则该类型为模数。 |
| traps | bool |
如果为该类型实现了陷阱,则为true。 |
| tinyness_before | bool |
如果在舍入之前检测到微小值,则为true。 |
| round_style | float_round_style | 舍入样式。一个类型可以具有以下任何枚举值: round_toward_zero,如果它向零舍入。 round_to_nearest,如果它向最接近的可表示值舍入。 round_toward_infinity,如果它向无穷大舍入。 round_toward_neg_infinity,如果它向负无穷大舍入。 如果舍入风格在编译时无法确定,则为round_indeterminate。 |
对于所有上述不是基本算术类型的类型,都使用默认模板定义。
C++98
template <class T> class numeric_limits {
public:
static const bool is_specialized = false;
static T min() throw();
static T max() throw();
static const int digits = 0;
static const int digits10 = 0;
static const bool is_signed = false;
static const bool is_integer = false;
static const bool is_exact = false;
static const int radix = 0;
static T epsilon() throw();
static T round_error() throw();
static const int min_exponent = 0;
static const int min_exponent10 = 0;
static const int max_exponent = 0;
static const int max_exponent10 = 0;
static const bool has_infinity = false;
static const bool has_quiet_NaN = false;
static const bool has_signaling_NaN = false;
static const float_denorm_style has_denorm = denorm_absent;
static const bool has_denorm_loss = false;
static T infinity() throw();
static T quiet_NaN() throw();
static T signaling_NaN() throw();
static T denorm_min() throw();
static const bool is_iec559 = false;
static const bool is_bounded = false;
static const bool is_modulo = false;
static const bool traps = false;
static const bool tinyness_before = false;
static const float_round_style round_style = round_toward_zero;
};
C++11
template <class T> class numeric_limits {
public:
static constexpr bool is_specialized = false;
static constexpr T min() noexcept { return T(); }
static constexpr T max() noexcept { return T(); }
static constexpr T lowest() noexcept { return T(); }
static constexpr int digits = 0;
static constexpr int digits10 = 0;
static constexpr bool is_signed = false;
static constexpr bool is_integer = false;
static constexpr bool is_exact = false;
static constexpr int radix = 0;
static constexpr T epsilon() noexcept { return T(); }
static constexpr T round_error() noexcept { return T(); }
static constexpr int min_exponent = 0;
static constexpr int min_exponent10 = 0;
static constexpr int max_exponent = 0;
static constexpr int max_exponent10 = 0;
static constexpr bool has_infinity = false;
static constexpr bool has_quiet_NaN = false;
static constexpr bool has_signaling_NaN = false;
static constexpr float_denorm_style has_denorm = denorm_absent;
static constexpr bool has_denorm_loss = false;
static constexpr T infinity() noexcept { return T(); }
static constexpr T quiet_NaN() noexcept { return T(); }
static constexpr T signaling_NaN() noexcept { return T(); }
static constexpr T denorm_min() noexcept { return T(); }
static constexpr bool is_iec559 = false;
static constexpr bool is_bounded = false;
static constexpr bool is_modulo = false;
static constexpr bool traps = false;
static constexpr bool tinyness_before = false;
};