STL:Numberic_limit()

(1):与MFC的兼容性

MFC程序过程中使用STL一些类编译出错,开始我认为是我写错了,放到Console Application里一切正常。
比如:
void CMyDialog::OnBnClickedButton1()
{
    double min=std::numeric_limits<double>::max();
    void *p=:perator new(count);
}

若在Console中根本没问题。但在MFC中numeric_limits错误提示:
Error 2 error C2589: '(' : illegal token on right side of '::' 

operator new错误提示:
Error 1 error C2665: 'operator new' : none of the 5 overloads could convert all the argument types 


解决方案:

跟Windows中定义的宏想混淆了
可以用括号改变语句的顺序,强制转换为stl中的函数名
double min=(std::numeric_limits<double>::max)();


(2): 小例展示numeric_limits的基本用法:

[cpp]  view plain copy
  1. #include <limits>  
  2. #include <iostream>  
  3. using namespace std;  
  4.   
  5. int main() {  
  6.     cout << boolalpha;  
  7.   
  8.     cout << "max(short): " << numeric_limits<short>::max() << endl;  
  9.     cout << "min(short): " << numeric_limits<short>::min() << endl;  
  10.   
  11.     cout << "max(int): " << numeric_limits<int>::max() << endl;  
  12.     cout << "min(int): " << numeric_limits<int>::min() << endl;  
  13.   
  14.     cout << "max(long): " << numeric_limits<long>::max() << endl;  
  15.     cout << "min(long): " << numeric_limits<long>::min() << endl;  
  16.   
  17.     cout << endl;  
  18.   
  19.     cout << "max(float): " << numeric_limits<float>::max() << endl;  
  20.     cout << "min(float): " << numeric_limits<float>::min() << endl;  
  21.   
  22.     cout << "max(double): " << numeric_limits<double>::max() << endl;  
  23.     cout << "min(double): " << numeric_limits<double>::min() << endl;  
  24.   
  25.     cout << "max(long double): " << numeric_limits<long double>::max() << endl;  
  26.     cout << "min(long double): " << numeric_limits<long double>::min() << endl;  
  27.   
  28.     cout << endl;  
  29.   
  30.     cout << "is_signed(char): "  
  31.         << numeric_limits<char>::is_signed << endl;  
  32.     cout << "is_specialized(string): "   
  33.         << numeric_limits<string>::is_specialized << endl;  
  34. }  

我机器上的运行结果:

[c-sharp]  view plain copy
  1. max(short): 32767  
  2. min(short): -32768  
  3. max(int): 2147483647  
  4. min(int): -2147483648  
  5. max(long): 2147483647  
  6. min(long): -2147483648  
  7.   
  8. max(float): 3.40282e+038  
  9. min(float): 1.17549e-038  
  10. max(double): 1.79769e+308  
  11. min(double): 2.22507e-308  
  12. max(long double): 1.79769e+308  
  13. min(long double): 2.22507e-308  
  14.   
  15. is_signed(char): true  
  16. is_specialized(string): false  
  17. 请按任意键继续. . .  

关于为什么float的最小值竟然是正的?我也存在疑问,从结果中,我们看出,min返回的是float型别可以表示的最小的正值,

而不是最小的float数。从这个例子中,我们差不多了解到numeric_limits的基本用法。

 

3. 基本成员函数

我以float类型来展示:

[c-sharp]  view plain copy
  1. #include <limits>  
  2. #include <iostream>  
  3. using namespace std;  
  4.   
  5. int main() {  
  6.     cout << boolalpha;  
  7.     // 可以表示的最大值  
  8.     cout << "max(float): " << numeric_limits<float>::max() << endl;  
  9.     // 可以表示的大于0的最小值,其他类型的实现或与此不同  
  10.     cout << "min(float): " << numeric_limits<float>::min() << endl;  
  11.     // 标准库是否为其实现了特化  
  12.     cout << "is_specialized(float): " << numeric_limits<float>::is_specialized << endl;  
  13.     // 是否是有符号的,即可以表示正负值  
  14.     cout << "is_signed(float): " << numeric_limits<float>::is_signed << endl;  
  15.     // 不否是整形的  
  16.     cout << "is_integer(float): " << numeric_limits<float>::is_integer << endl;  
  17.     // 是否是精确表示的  
  18.     cout << "is_exact(float): " << numeric_limits<float>::is_exact << endl;  
  19.     // 是否存在大小界限  
  20.     cout << "is_bounded(float): " << numeric_limits<float>::is_bounded << endl;  
  21.     // 两个比较大的数相加而不会溢出,生成一个较小的值  
  22.     cout << "is_modulo(float): " << numeric_limits<float>::is_modulo << endl;  
  23.     // 是否符合某某标准  
  24.     cout << "is_iec559(float): " << numeric_limits<float>::is_iec559 << endl;  
  25.     // 不加+-号可以表示的位数  
  26.     cout << "digits(float): " << numeric_limits<float>::digits << endl;  
  27.     // 十进制数的个数  
  28.     cout << "digits10(float): " << numeric_limits<float>::digits10 << endl;  
  29.     // 一般基数为2  
  30.     cout << "radix(float): " << numeric_limits<float>::radix << endl;  
  31.     // 以2为基数的最小指数  
  32.     cout << "min_exponent(float): " << numeric_limits<float>::min_exponent << endl;  
  33.     // 以2为基数的最大指数  
  34.     cout << "max_exponent(float): " << numeric_limits<float>::max_exponent << endl;  
  35.     // 以10为基数的最小指数  
  36.     cout << "min_exponent10(float): " << numeric_limits<float>::min_exponent10 << endl;  
  37.     // 以10为基数的最大指数  
  38.     cout << "max_exponent10(float): " << numeric_limits<float>::max_exponent10 << endl;  
  39.     // 1值和最接近1值的差距  
  40.     cout << "epsilon(float): " << numeric_limits<float>::epsilon() << endl;  
  41.     // 舍入方式  
  42.     cout << "round_style(float): " << numeric_limits<float>::round_style << endl;  
  43. }  

运行结果:

[cpp]  view plain copy
  1. max(float): 3.40282e+038  
  2. min(float): 1.17549e-038  
  3. is_specialized(float): true  
  4. is_signed(float): true  
  5. is_integer(float): false  
  6. is_exact(float): false  
  7. is_bounded(float): true  
  8. is_modulo(float): false  
  9. is_iec559(float): true  
  10. digits(float): 24  
  11. digits10(float): 6  
  12. radix(float): 2  
  13. min_exponent(float): -125  
  14. max_exponent(float): 128  
  15. min_exponent10(float): -37  
  16. max_exponent10(float): 38  
  17. epsilon(float): 1.19209e-007  
  18. round_style(float): 1  
  19. 请按任意键继续. . .


(3):关于STL:Numberic_limit()

原文链接:http://www.cplusplus.com/reference/limits/numeric_limits/

class template
<limits>

std::numeric_limits

template <class T> numeric_limits;
Numeric limits type
Provides information about the properties of  arithmetic types (either integral or floating-point) in the specific platform for which the library compiles.

This class template is specialized for every  fundamental arithmetic type, with its members describing the properties of type  T. This template shall not be specialized for any other type.

Template parameters

T
A type.
If this is a  fundamental arithmetic type, the members of the class describe its properties.

Template instantiations

  • C++98
  • C++11
fundamental arithmetic types
integral types bool
char
wchar_t
signed char
short int
int
long int
unsigned char
unsigned short int
unsigned int
unsigned long int
floating point types float
double
long double
For any other type, its default definition is used.

Members that produce a value of type  T are member functions, while members of specific types are static member constants:

Members

member type property
is_specialized bool true for all arithmetic types (i.e., those for which numeric_limits is specialized).
false for all other types.
min() T Minimum finite value.
For floating types with denormalization (variable number of exponent bits): minimum positive normalized value.
Equivalent to CHAR_MIN, SCHAR_MIN, SHRT_MIN, INT_MIN, LONG_MIN, LLONG_MIN, FLT_MIN,DBL_MIN, LDBL_MIN or 0, depending on type.
max() T Maximum finite value.
Equivalent to 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 or LDBL_MAX, depending on type.
lowest() T Minimum finite value. (since C++11)
For integral types: the same as min().
For floating-point types: implementation-dependent; generally, the negative ofmax().
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 toFLT_MANT_DIG, DBL_MANT_DIG or LDBL_MANT_DIG).
digits10 int Number of digits (in decimal base) that can be represented without change.
Equivalent to FLT_DIG, DBL_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 toFLT_RADIX).
epsilon() T Machine epsilon (the difference between 1 and the least value greater than 1 that is representable).
Equivalent to FLT_EPSILON, DBL_EPSILON or LDBL_EPSILON for floating types.
round_error() T Measure of the maximum rounding error.
min_exponent int Minimum negative integer value such that radix raised to (min_exponent-1)generates a normalized floating-point number.
Equivalent to FLT_MIN_EXP, DBL_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_EXP, DBL_MIN_10_EXP or LDBL_MIN_10_EXP for floating types.
max_exponent int Maximum integer value such that radix raised to (max_exponent-1) generates a representable finite floating-point number.
Equivalent to FLT_MAX_EXP, DBL_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_EXP, DBL_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 bool true if the type adheres to IEC-559 / IEEE-754 standard.
An IEC-559 type always has has_infinity, has_quiet_NaN and has_signaling_NaN set to true; And infinity, quiet_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.

下面是参数的解释

digits10

返回目标类型在十进制下可以表示的最大位数

epsilon

返回目标数据类型能表示的最逼近1的正数和1的差的绝对值

has_denorm

测试目标类型是不是可以非规范化表示示

has_denorm_loss

测试所有类型是不是能测出因为非规范化而造成的精度损失(不是因为结果本身的不精确)

has_infinity

测试目标类型是不是能表示无限(比如被0除,或者其他一些情况)

has_quiet_NaN

检查目标类型是不是支持安静类型的NaN

has_signaling_NaN

检查目标类型是不是支持信号类型的NaN

infinity

检查目标类型的无限类型(如果支持无限表示)

is_bounded

检查目标类型的取值是否有限

is_exact

测试目标类型的计算结果是不是不会造成舍入误差(比如float是0)

is_iec559

测试目标类型是不是符合IEC559标准

is_integer

测试目标类型是不是可以用整型来表示(比如char是1,float是0)

is_modulo

Tests if a type has a modulo representation.

is_signed

测试目标类型是否是带符号的

is_specialized

测试目标类型是不是在numeric_limits .模板类中有特殊定义

max

返回可取的有限最大值

max_exponent

Returns the maximum positive integral exponent that the floating-point type can represent as a finite value when a base of radix is raised to that power.

max_exponent10

Returns the maximum positive integral exponent that the floating-point type can represent as a finite value when a base of ten is raised to that power.

min

返回可取的最小值(规范化)

min_exponent

Returns the maximum negative integral exponent that the floating-point type can represent as a finite value when a base of radix is raised to that power.

min_exponent10

Returns the maximum negative integral exponent that the floating-point type can represent as a finite value when a base of ten is raised to that power.

quiet_NaN

返回目标类型的安静NAN的表示

radix

Returns the integral base, referred to as radix, used for the representation of a type.

round_error

返回目标类型的最大可能的舍入误差

round_style

Returns a value that describes the various methods that an implementation can choose for rounding a floating-point value to an integer value.

signaling_NaN

返回目标类型关于信号NAN的表示

tinyness_before

测试目标类型是不是能测定出微小的舍入误差

traps

Tests whether trapping that reports on arithmetic exceptions is implemented for a type.




For all types that are not  fundamental arithmetic types, the default template definition is used:
  • C++98
  • C++11
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;
};


Example

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


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

你可能感兴趣的:(STL:Numberic_limit())