`

[转][C++][STL] numeric_limits的用法

    博客分类:
  • C++
 
阅读更多

From:http://www.cplusplus.com/reference/std/limits/numeric_limits/

numeric_limits

 

浮点数据精度判断

float x=0.0;

float y=0.0f;

 

if ( abs(x -y) <= numeric_limits<float>::epsilon() )

{

 ...

}

class template
<limits>

Numeric limits type

This class is specialized for each of the fundamental types, with its members returning or set to the different values that define the properties that type has in the specific platform in which it compiles.

For all the other types (non-fundamental types) a specialization of this class should not exist.

The non-specialized class is defined as:

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



A specialization exists for each of the fundamental types: boolcharsigned charunsigned charwchar_tshortunsigned shortintunsigned intlongunsigned longfloatdouble and long double. These specializations define the specific values for the different static const members, and all have is_specialized defined as true.

Members

member type property
is_specialized bool true on all specializations of the type, false in the non-specialized version.
min() T Minimum finite value.
For floating types with denormalization (variable number of exponent bits): minimum positive normalized value.
Equivalent to CHAR_MINSCHAR_MINSHRT_MININT_MINLONG_MINFLT_MINDBL_MINLDBL_MIN or 0, depending on type.
max() T Maximum finite value.
Equivalent to CHAR_MAXSCHAR_MAXUCHAR_MAXSHRT_MAXUSHRT_MAXINT_MAXUINT_MAXLONG_MAXULONG_MAXFLT_MAXDBL_MAX or LDBL_MAX, depending on type.
digits int For integer types: number of non-sign bits (radix base digits) in the representation.
For floating types: number of digits (in radix base) in the mantissa (equivalent to FLT_MANT_DIGDBL_MANT_DIG or LDBL_MANT_DIG).
digits10 int Number of digits (in decimal base) that can be represented without change.
Equivalent to FLT_DIGDBL_DIG or LDBL_DIG for floating types.
is_signed bool true if type is signed.
is_integer bool true if type is integer.
is_exact bool true if type uses exact representations.
radix int For integer types: base of the representation.
For floating types: base of the exponent of the representation (equivalent to FLT_RADIX).
epsilon() T Machine epsilon (the difference between 1 and the least value greater than 1 that is representable).
Equivalent to FLT_EPSILONDBL_EPSILON or LDBL_EPSILON for floating types.
round_error() T Measure of the maximum rounding error.
min_exponent int Minimum negative integer value for the exponent that generates a normalized floating-point number.
Equivalent to FLT_MIN_EXPDBL_MIN_EXP or LDBL_MIN_EXP for floating types.
min_exponent10 int Minimum negative integer value such that 10 raised to that power generates a normalized floating-point number.
Equivalent to FLT_MIN_10_EXPDBL_MIN_10_EXP or LDBL_MIN_10_EXP for floating types.
max_exponent int Maximum integer value for the exponent that generates a normalized floating-point number.
Equivalent to FLT_MAX_EXPDBL_MAX_EXP or LDBL_MAX_EXP for floating types.
max_exponent10 int Maximum integer value such that 10 raised to that power generates a normalized finite floating-point number.
Equivalent to FLT_MAX_10_EXPDBL_MAX_10_EXP or LDBL_MAX_10_EXP for floating types.
has_infinity bool true if the type has a representation for positive infinity.
has_quiet_NaN bool true if the type has a representation for a quiet (non-signaling) "Not-a-Number".
has_signaling_NaN bool true if the type has a representation for a signaling "Not-a-Number".
has_denorm float_denorm_style Denormalized values (representations with a variable number of exponent bits). A type may have any of the following enum values:
denorm_absent, if it does not allow denormalized values.
denorm_present, if it allows denormalized values.
denorm_indeterminate, if indeterminate at compile time.
has_denorm_loss bool true if a loss of accuracy is detected as a denormalization loss, rather than an inexact result.
infinity() T Representation of positive infinity, if available.
quiet_NaN() T Representation of quiet (non-signaling) "Not-a-Number", if available.
signaling_NaN() T Representation of signaling "Not-a-Number", if available.
denorm_min() T Minimum positive denormalized value.
For types not allowing denormalized values: same as min().
is_iec559() T true if the type adheres to IEC-559 / IEEE-754 standard.
An IEC-559 type always has has_infinityhas_quiet_NaN and has_signaling_NaN set to true; And infinityquiet_NaN and signaling_NaN return some non-zero value.
is_bounded bool true if the set of values represented by the type is finite.
is_modulo bool true if the type is modulo. A type is modulo if it is possible to add two positive numbers and have a result that wraps around to a third number that is less.
traps bool true if trapping is implemented for the type.
tinyness_before bool true if tinyness is detected before rounding.
round_style float_round_style Rounding style. A type may have any of the following enum values:
round_toward_zero, if it rounds toward zero.
round_to_nearest, if it rounds to the nearest representable value.
round_toward_infinity, if it rounds toward infinity.
round_toward_neg_infinity, if it rounds toward negative infinity.
round_indeterminate, if the rounding style is indeterminable at compile time.

 

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// numeric_limits example
#include <iostream>
#include <limits>
using namespace std;

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



Possible output:


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

 

See also

 

 

Test:



#include <iostream> #include <cstdlib> #include <limits> #include <string> using namespace std;

int main() {     //判断各类型有无极值     cout << boolalpha;     cout << "specialized(char): "        << numeric_limits<char>::is_specialized         << endl;     cout << "specialized(wchar_t): "        << numeric_limits<wchar_t>::is_specialized         << endl;     cout << "specialized(string): "        << numeric_limits<string>::is_specialized         << endl;      cout << noboolalpha << endl;          //各个类型的最大值      cout << "max(int): "           << numeric_limits<int>::max()          << endl;     cout << "max(short): "           << numeric_limits<short>::max()          << endl;     cout << "max(unsigned int): "           << numeric_limits<unsigned int>::max()          << endl;     cout << "max(unsigned short): "           << numeric_limits<unsigned short>::max()          << endl;     cout << "max(unsigned long): "           << numeric_limits<unsigned long>::max()          << endl;     cout << "max(long): "           << numeric_limits<long>::max()          << endl;     cout << "max(long long): "           << numeric_limits<long long>::max()          << endl;     cout << "max(float): "           << numeric_limits<float>::max()          << endl;     cout << "max(double): "           << numeric_limits<double>::max()          << endl;     cout << "max(long double): "           << numeric_limits<long double>::max()          << endl;     cout << endl;                //各个类型的最小值      cout << "min(int): "           << numeric_limits<int>::min()          << endl;     cout << "min(short): "           << numeric_limits<short>::min()          << endl;     cout << "min(unsigned int): "           << numeric_limits<unsigned int>::min()          << endl;     cout << "min(unsigned short): "           << numeric_limits<unsigned short>::min()          << endl;     cout << "min(unsigned long): "           << numeric_limits<unsigned long>::min()          << endl;     cout << "min(long): "           << numeric_limits<long>::min()          << endl;     cout << "min(long long): "           << numeric_limits<long long>::min()          << endl;     cout << "min(float): "           << numeric_limits<float>::min()          << endl;     cout << "min(double): "           << numeric_limits<double>::min()          << endl;     cout << "min(long double): "           << numeric_limits<long double>::min()          << endl;          system("pause");          return 0;      }

 

分享到:
评论

相关推荐

    STL_资料(袁辉勇_整理)

    - **numeric_limits**:提供数值类型的最大值、最小值等信息。 #### 三、容器 (Container) **容器**是STL中的另一大类,它们是用来存储和管理不同类型数据的结构。STL中的容器大致可以分为序列容器和关联容器两大...

    《C++从入门到精通》的观看记录.docx

    4. 使用`numeric_limits&lt;type&gt;::max()`和`numeric_limits&lt;type&gt;::min()`获取类型的最小值和最大值。 5. `endl`用于行缓冲,同时`cin`和`cout`都在`std`命名空间内。 6. C++支持C语言的输入输出,可以通过`#include...

    C++ Standard Library

    10. **类型安全的算术运算**:例如,std::numeric_limits类提供了关于特定类型(如int、double)的边界和精度信息,防止溢出等错误。 C++ Standard Library是C++编程的强大工具箱,它极大地提升了开发者的生产力,...

    使用VC计算N个数的最小值

    在VC中,我们可以使用标准模板库(STL)或原始的C++语法来实现我们的目标。 计算最小值的基本思路是遍历给定的N个数,比较每个数与当前已知的最小值,如果找到更小的数,则更新最小值。这个过程可以通过循环结构...

    stl详解 包括各种实例代码

    ### STL详解及实例代码 #### 一、STL简介 STL(Standard Template Library,标准模板库)是一种广泛应用于...22. **numeric_limits**:数值限制。 以上是STL的一些基础介绍和示例代码,希望对学习C++编程有所帮助。

    C++头文件大全

    ### C++头文件大全 在C++编程语言中,头文件是极其重要的组成部分,它们包含了各种功能库的声明,能够帮助开发者实现复杂的功能而无需从底层编写代码。下面将详细介绍部分常用的C++标准库头文件及其主要成员函数。 ...

    The C++ Standard Library(简体中文)

    - **auto_ptrs的错误运用**:列出了常见的错误使用方法,并给出了解决方案。 - **auto_ptr运用实例**:通过实际例子展示了`std::auto_ptr`的应用场景。 - **数值极限(Numeric Limits)**:这部分详细解释了如何确定...

    C++,C头文件汇总

    * `&lt;numeric&gt;`:STL常用的数字操作 * `&lt;iomanip&gt;`:参数化输入/输出 * `&lt;ios&gt;`:基本输入/输出支持 * `&lt;iosfwd&gt;`:输入/输出系统使用的前置声明 C99增加的部分头文件 C99增加的部分头文件是C语言的最新版本中添加...

    C++.Standard.Library.[侯捷].pdf

    书中详细介绍了C++标准库的各项功能,包括其组成部分、使用方法以及背后的原理。 #### 二、C++语言特点 - **模板(Templates)**:C++中的模板是一种强大的机制,允许开发者编写能够处理多种数据类型的函数或类。它...

    C++ Standard Library: A Tutorial and Reference

    Utilities &lt;br/&gt;4.1 Pairs &lt;br/&gt;4.1.1 Convenience Function &lt;br/&gt;4.1.2 Examples of Pair Usage &lt;br/&gt;4.2 Class &lt;br/&gt;4.3 Numeric Limits &lt;br/&gt;4.4 Auxiliary Functions &lt;br/&gt;4.5 ...

    C++标准库(第二版)英文版.pdf

    5.3 Numeric Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 5.4 Type Traitsand Type Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 5.4.1 ...

    c++头文件大全

    这些知识点涵盖了从基本的类型定义到复杂的标准模板库(STL)组件,对于学习和掌握C++语言至关重要。 ### 1. C 标准库头文件 #### C99 之前的 C 标准库头文件 - **`&lt;assert.h&gt;`**:提供了断言宏 `assert()`,用于...

    【PPT-172页】改善程序设计技术的50个有效做法(第二版)

    38. **利用类型安全的算术操作**:例如使用`std::numeric_limits`防止溢出。 39. **了解并使用智能指针**:如`std::unique_ptr`、`std::shared_ptr`和`std::weak_ptr`,以避免内存泄漏。 40. **利用C++的特性实现...

    vc++库函数及其头文件 (3).pdf

    - `&lt;numeric&gt;`:STL的数值操作,如累加、平均值等。 - `&lt;iomanip&gt;`:格式化输入/输出。 - `&lt;ios&gt;`:基础输入/输出支持。 - `&lt;iosfwd&gt;`:输入/输出系统使用的前置声明。 - `&lt;iostream&gt;`:数据流输入/输出。 - `...

    C++常用的#include头文件总结

    5. `&lt;limits&gt;`:定义了各种数据类型的最小值和最大值常量,如`std::numeric_limits`,用于获取类型边界。 6. `&lt;list&gt;`:实现了线性列表容器,通过双向链表实现,支持快速插入和删除。 7. `&lt;map&gt;`:提供了映射容器...

Global site tag (gtag.js) - Google Analytics