C++ limits头文件的用法(numeric_limits)

初学C++的时候,对这个模板很陌生,不知道它到底是做什么用的,今天拿起《C++标准程序库》,出现了它的讨论,所以决定好好研究一番。

1. numeric_limits是什么?

(A)《C++标准程序库》:

[cpp]  view plain cop
  1. 一般来说,数值型别的极值是一个与平台相关的特性。C++标准程序库通过template numeric_limits提供这些极值,取代传统C语言,所采用的预处理常数。新的极值概念有两个优点,第一是提供更好的型别安全性,第二是程序员可借此写出一些template以核定这些极值。  

(B)MSDN

[cpp]  view plain copy
  1. The template class describes arithmetic properties of built-in numerical types.  
  2.   
  3. The header defines explicit specializations for the types wchar_tboolcharsigned char, unsigned charshort, unsigned shortint, unsigned intlong, unsigned longfloatdouble, and long double. For these explicit specializations, the member numeric_limits::is_specialized is true, and all relevant members have meaningful values. The program can supply additional explicit specializations. Most member functions of the class describe or test possible implementations of float.  
  4.   
  5. For an arbitrary specialization, no members have meaningful values. A member object that does not have a meaningful value stores zero (or false) and a member function that does not return a meaningful value returns Type(0).  
  6.   
  7.   
  8.   
  9. 上面的意思是说:  
  10.   
  11. 这个模板类描述了内建类型的数值属性。  
  12.   
  13. C++标准库显式地为wchar_tboolcharsigned char, unsigned charshort, unsigned shortint, unsigned intlong, unsigned longfloatdouble, and long double这些类型提供了特化。对于这些类型来说,is_specialized为true,并且所有的相关的成员(成员变量或成员函数)是有意义的。这个模板也提供其他的特化。大部分的成员函数可以用float型别来描述或测试。  
  14.   
  15. 对于一个任意的特化,相关的成员是没有意义的。一个没有意义的对象一般用0(或者false)来表示,一个没有意义的成员函数会返回0.  

 

(C)我的理解

[cpp]  view plain copy
  1. 说白了,它是一个模板类,它主要是把C++当中的一些内建型别进行了封装,比如说numeric_limits<int>是一个特化后的类,从这个类的成员变量与成员函数中,我们可以了解到int的很多特性:可以表示的最大值,最小值,是否是精确的,是否是有符号等等。如果用其他任意(非内建类型)来特化这个模板类,比如string,string怎么可能有最大值?我们从MSDN上可以了解到,这对string,成员变量与成员函数是没有意义的,要么返回0要么为false。  

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. 请按任意键继续. . .  
原文链接
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值