目录

C++ Library - <limits>

介绍 (Introduction)

它是一种数字限制类型,它提供有关库编译的特定平台中算术类型(整数或浮点)属性的信息。

声明 (Declaration)

以下是std :: numeric_limits的声明。

template <class T> numeric_limits; 

C++11

template <class T> numeric_limits; 

参数 (Parameters)

T - 这是一种类。

例子 (Example)

在下面的示例中为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

Members

会员 类型 属性
is_specialized bool 对所有人来说都是true (即, numeric_limits专用的那些)。 所有其他类型都是false的。
min() T

它是最小有限值。

对于具有非规范化的浮点类型(可变数量的指数位):最小正标准化值。

等效于CHAR_MINSCHAR_MINSHRT_MININT_MINLONG_MINLLONG_MINFLT_MINBL_MINLDBL_MIN或0 ,具体取决于类型。

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 如果type是整数,则返回true 。
is_exact bool 如果type使用精确表示,则为true 。
radix int

它适用于整数类型:表示的基础。

对于浮点类型:表示的指数的基数(相当于FLT_RADIX )。

epsilon() T

它是一个机器epsilon(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 如果类型具有信号“Not-a-Number”的表示,则返回true 。
has_denorm float_denorm_style

它是非规范化的值(具有可变数量的指数位的表示)。 类型可以具有以下任何枚举值 -

denorm_absent ,如果它不允许非规范化值。

denorm_present ,如果它允许非规范化的值。

denorm_indeterminate ,如果在编译时不确定。

has_denorm_loss bool 如果将loss of accuracy检测为非规范化损失而不是不精确的结果,则为true 。
infinity() T 如果可用,它代表positive infinity
quiet_NaN() T 它表示quiet (非信令) "Not-a-Number" (如果可用)。
signaling_NaN() T 如果可用,它表示signaling "Not-a-Number"
denorm_min() T

最小正非规范化值。

它适用于不允许非规范化值的类型:与min()相同。

is_iec559 bool

如果类型符合IEC-559/IEEE-754标准,则为true 。

IEC-559类型总是将has_infinityhas_quiet_NaNhas_signaling_NaN设置为true ; 无穷大quiet_NaNsignaling_NaN返回一些非零值。

is_bounded bool 如果由类型表示的值集是有限的,则返回true 。
is_modulo bool 如果类型是模数,则为true 。 如果可以添加两个正数并且结果包含到第三个更小的数字,则类型是modulo数。
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;
};
↑回到顶部↑
WIKI教程 @2018