類模板
<limits>

std::numeric_limits

template <class T> numeric_limits;
數字限制類型
為編譯此庫的特定平臺上的所有*算術型別*(整數型別或浮點型別)提供有關屬性的資訊。

這個類模板為每個*基本算術型別*進行了特化,其成員描述了型別T的屬性。這個模板不應對任何其他型別進行特化。

模板引數

T
一個型別。
如果這是一個*基本算術型別*,那麼該類的成員將描述它的屬性。

sub_match for string literals (class)

基本算術型別
整數型別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
對於任何其他型別,將使用其預設定義。
基本算術型別
整數型別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
此模板還為所有這些型別的const和/或volatile限定版本進行了特化,其值與無限定的特化相同。

對於任何其他型別,將使用其預設定義。

生成型別T的成員是成員函式,而特定型別的成員是靜態成員常量。

成員

成員型別屬性
is_specializedbool對於所有*算術型別*(即numeric_limits特化的型別)為true
對於所有其他型別為false
min()T最小有限值。
對於具有反規格化(指數位數可變)的浮點型別:最小正規格化值。
取決於型別,等同於CHAR_MINSCHAR_MINSHRT_MININT_MINLONG_MINLLONG_MINFLT_MINDBL_MINLDBL_MIN0
max()T最大有限值。
取決於型別,等同於CHAR_MAXSCHAR_MAXUCHAR_MAXSHRT_MAXUSHRT_MAXINT_MAXUINT_MAXLONG_MAXULONG_MAXLLONG_MAXULLONG_MAXUINT_LEAST16_MAXUINT_LEAST32_MAXFLT_MAXDBL_MAXLDBL_MAX
lowest()T最小有限值。(自C++11起)
對於整數型別:與min()相同。
對於浮點型別:實現定義;通常是max()的負值。
digitsint對於整數型別:表示中非符號位的數量(radix進位制數字)。
對於浮點型別:尾數中的數字數量(radix進位制);等同於FLT_MANT_DIGDBL_MANT_DIGLDBL_MANT_DIG)。
digits10int能夠不改變地表示的(十進位制)數字數量。
對於浮點型別,等同於FLT_DIGDBL_DIGLDBL_DIG
max_digits10int為了確保不同值總是能被區分而需要的(十進位制)數字數量。
is_signedbool如果型別是有符號的,則為true
is_integerbool如果型別是整數,則為true
is_exactbool如果型別使用精確表示,則為true
radixint對於整數型別:表示的基數。
對於浮點型別:表示中指數的基數(等同於FLT_RADIX)。
epsilon()T機器epsilon(1與大於1的最小可表示值之間的差)。
對於浮點型別,等同於FLT_EPSILONDBL_EPSILONLDBL_EPSILON
round_error()T最大舍入誤差的度量。
min_exponentint最小負整數值,使得radix(min_exponent-1)次方生成一個規格化的浮點數。
對於浮點型別,等同於FLT_MIN_EXPDBL_MIN_EXPLDBL_MIN_EXP
min_exponent10int最小負整數值,使得10的該次方生成一個規格化的浮點數。
對於浮點型別,等同於FLT_MIN_10_EXPDBL_MIN_10_EXPLDBL_MIN_10_EXP
max_exponentint最大整數值,使得radix(max_exponent-1)次方生成一個可表示的有限浮點數。
對於浮點型別,等同於FLT_MAX_EXPDBL_MAX_EXPLDBL_MAX_EXP
max_exponent10int最大整數值,使得10的該次方生成一個規格化的有限浮點數。
對於浮點型別,等同於FLT_MAX_10_EXPDBL_MAX_10_EXPLDBL_MAX_10_EXP
has_infinitybool如果型別有正無窮大的表示,則為true
has_quiet_NaNbool如果型別有安靜(非訊號)“非數字”(NaN)的表示,則為true
has_signaling_NaNbool如果型別有訊號“非數字”(NaN)的表示,則為true
has_denormfloat_denorm_style反規格化值(指數位數可變的表示)。一個型別可能具有以下任一列舉值:
denorm_absent,如果它不允許反規格化值。
denorm_present,如果它允許反規格化值。
denorm_indeterminate,如果編譯時無法確定。
has_denorm_lossbool如果*精度損失*被檢測為反規格化損失,而不是不精確的結果,則為true
infinity()T如果可用,則為*正無窮大*的表示。
quiet_NaN()T如果可用,則為*安靜*(非訊號)*“非數字”*(NaN)的表示。
signaling_NaN()T如果可用,則為*訊號“非數字”*(NaN)的表示。
denorm_min()T最小正反規格化值。
對於不允許反規格化值的型別:與min()相同。
is_iec559bool如果型別符合IEC-559 / IEEE-754標準,則為true
IEC-559型別總是具有has_infinityhas_quiet_NaNhas_signaling_NaN設定為true;並且infinityquiet_NaNsignaling_NaN返回非零值。
is_boundedbool如果型別表示的值集是有限的,則為true
is_modulobool如果型別是模運算的,則為true。如果兩個正數相加後結果會迴繞成一個更小的第三個數,則該型別為*模運算*型別。
trapsbool如果為該型別實現了陷阱(trapping),則為true
tinyness_beforebool如果在舍入之前檢測到微小值(tinyness),則為true
round_stylefloat_round_style舍入風格。一個型別可能具有以下任一列舉值:
round_toward_zero,如果它向零舍入。
round_to_nearest,如果它舍入到最近的可表示值。
round_toward_infinity,如果它向無窮大舍入。
round_toward_neg_infinity,如果它向負無窮大舍入。
round_indeterminate,如果舍入風格在編譯時無法確定。

對於所有不是*基本算術型別*的型別,將使用預設的模板定義。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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;
  static constexpr float_round_style round_style = round_toward_zero;
};

所有特化也應將這些值提供為*常量表達式*。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
// numeric_limits example
#include <iostream>     // std::cout
#include <limits>       // std::numeric_limits

int main () {
  std::cout << std::boolalpha;
  std::cout << "Minimum value for int: " << std::numeric_limits<int>::min() << '\n';
  std::cout << "Maximum value for int: " << std::numeric_limits<int>::max() << '\n';
  std::cout << "int is signed: " << std::numeric_limits<int>::is_signed << '\n';
  std::cout << "Non-sign bits in int: " << std::numeric_limits<int>::digits << '\n';
  std::cout << "int has infinity: " << std::numeric_limits<int>::has_infinity << '\n';
  return 0;
}

可能的輸出

Minimum value for int: -2147483648
Maximum value for int: 2147483647
int is signed: true
Non-sign bits in int: 31
int has infinity: false


另見