
- 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; };