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_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()的負值。

digits int

對於整數型別:表示中非符號位的數量(基數進位制位數)。

對於浮點型別:尾數中的位數(基數進位制)(等效於FLT_MANT_DIGDBL_MANT_DIGLDBL_MANT_DIG)。

digits10 int

可以不變地表示的位數(十進位制基數)。

對於浮點型別,等效於FLT_DIGDBL_DIGLDBL_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_EPSILONDBL_EPSILONLDBL_EPSILON

round_error() T 最大舍入誤差的度量。
min_exponent int

最小負整數,使得基數提高到(min_exponent-1)次方會生成一個規範化的浮點數。

對於浮點型別,等效於FLT_MIN_EXPDBL_MIN_EXPLDBL_MIN_EXP

min_exponent10 int

最小負整數,使得 10 提高到該次方會生成一個規範化的浮點數。

對於浮點型別,等效於FLT_MIN_10_EXPDBL_MIN_10_EXPLDBL_MIN_10_EXP

max_exponent int

最大整數,使得基數提高到(max_exponent-1)次方會生成一個可表示的有限浮點數。

對於浮點型別,等效於FLT_MAX_EXPDBL_MAX_EXPLDBL_MAX_EXP

max_exponent10 int

最大整數,使得 10 提高到該次方會生成一個規範化的有限浮點數。

對於浮點型別,等效於FLT_MAX_10_EXPDBL_MAX_10_EXPLDBL_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

最小正非規格化值。

對於不允許非規格化值的型別:與min()相同。

is_iec559 bool

如果型別符合 IEC-559/IEEE-754 標準,則為true

IEC-559 型別始終將has_infinityhas_quiet_NaNhas_signaling_NaN 設定為true;並且infinityquiet_NaNsignaling_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;
};
廣告