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

初學C++的時候,對這個模板很陌生,不知道它到底是做什麼用的,今天拿起《C++標準程序庫》,出現了它的討論,所以決定好好研究一番。

1. numeric_limits是什麼?

(A)《C++標準程序庫》:


  1. 一般來說,數值型別的極值是一個與平臺相關的特性。C++標準程序庫通過template numeric_limits提供這些極值,取代傳統C語言,所採用的預處理常數。新的極值概念有兩個優點,第一是提供更好的型別安全性,第二是程序員可藉此寫出一些template以覈定這些極值。  


(B)MSDN


  1. The templateclass describes arithmetic properties of built-in numerical types.  

  2. The header defines explicit specializations for the types wchar_t, bool, char, signedchar, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, float, double, and longdouble. 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.  

  3. 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).  

  4. 上面的意思是說:  

  5. 這個模板類描述了內建類型的數值屬性。  

  6. C++標準庫顯式地爲wchar_t, bool, char, signedchar, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, float, double, and longdouble這些類型提供了特化。對於這些類型來說,is_specialized爲true,並且所有的相關的成員(成員變量或成員函數)是有意義的。這個模板也提供其他的特化。大部分的成員函數可以用float型別來描述或測試。  

  7. 對於一個任意的特化,相關的成員是沒有意義的。一個沒有意義的對象一般用0(或者false)來表示,一個沒有意義的成員函數會返回0.  



(C)我的理解


  1. 說白了,它是一個模板類,它主要是把C++當中的一些內建型別進行了封裝,比如說numeric_limits<int>是一個特化後的類,從這個類的成員變量與成員函數中,我們可以瞭解到int的很多特性:可以表示的最大值,最小值,是否是精確的,是否是有符號等等。如果用其他任意(非內建類型)來特化這個模板類,比如string,string怎麼可能有最大值?我們從MSDN上可以瞭解到,這對string,成員變量與成員函數是沒有意義的,要麼返回0要麼爲false。  


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


  1. #include <limits>

  2. #include <iostream>

  3. usingnamespace std;  

  4. int main() {  

  5.    cout << boolalpha;  

  6.    cout << "max(short): " << numeric_limits<short>::max() << endl;  

  7.    cout << "min(short): " << numeric_limits<short>::min() << endl;  

  8.    cout << "max(int): " << numeric_limits<int>::max() << endl;  

  9.    cout << "min(int): " << numeric_limits<int>::min() << endl;  

  10.    cout << "max(long): " << numeric_limits<long>::max() << endl;  

  11.    cout << "min(long): " << numeric_limits<long>::min() << endl;  

  12.    cout << endl;  

  13.    cout << "max(float): " << numeric_limits<float>::max() << endl;  

  14.    cout << "min(float): " << numeric_limits<float>::min() << endl;  

  15.    cout << "max(double): " << numeric_limits<double>::max() << endl;  

  16.    cout << "min(double): " << numeric_limits<double>::min() << endl;  

  17.    cout << "max(long double): " << numeric_limits<longdouble>::max() << endl;  

  18.    cout << "min(long double): " << numeric_limits<longdouble>::min() << endl;  

  19.    cout << endl;  

  20.    cout << "is_signed(char): "

  21.        << numeric_limits<char>::is_signed << endl;  

  22.    cout << "is_specialized(string): "

  23.        << numeric_limits<string>::is_specialized << endl;  

  24. }  


我機器上的運行結果:


  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. max(float): 3.40282e+038  

  8. min(float): 1.17549e-038  

  9. max(double): 1.79769e+308  

  10. min(double): 2.22507e-308  

  11. max(longdouble): 1.79769e+308  

  12. min(longdouble): 2.22507e-308  

  13. is_signed(char): true

  14. is_specialized(string): false

  15. 請按任意鍵繼續. . .  


關於爲什麼float的最小值竟然是正的?我也存在疑問,從結果中,我們看出,min返回的是float型別可以表示的最小的正值,

而不是最小的float數。

從這個例子中,我們差不多瞭解到numeric_limits的基本用法。


3. 基本成員函數

我以float類型來展示:


  1. #include <limits>

  2. #include <iostream>

  3. usingnamespace std;  

  4. int main() {  

  5.    cout << boolalpha;  

  6. // 可以表示的最大值

  7.    cout << "max(float): " << numeric_limits<float>::max() << endl;  

  8. // 可以表示的大於0的最小值,其他類型的實現或與此不同

  9.    cout << "min(float): " << numeric_limits<float>::min() << endl;  

  10. // 標準庫是否爲其實現了特化

  11.    cout << "is_specialized(float): " << numeric_limits<float>::is_specialized << endl;  

  12. // 是否是有符號的,即可以表示正負值

  13.    cout << "is_signed(float): " << numeric_limits<float>::is_signed << endl;  

  14. // 不否是×××的

  15.    cout << "is_integer(float): " << numeric_limits<float>::is_integer << endl;  

  16. // 是否是精確表示的

  17.    cout << "is_exact(float): " << numeric_limits<float>::is_exact << endl;  

  18. // 是否存在大小界限

  19.    cout << "is_bounded(float): " << numeric_limits<float>::is_bounded << endl;  

  20. // 兩個比較大的數相加而不會溢出,生成一個較小的值

  21.    cout << "is_modulo(float): " << numeric_limits<float>::is_modulo << endl;  

  22. // 是否符合某某標準

  23.    cout << "is_iec559(float): " << numeric_limits<float>::is_iec559 << endl;  

  24. // 不加+-號可以表示的位數

  25.    cout << "digits(float): " << numeric_limits<float>::digits << endl;  

  26. // 十進制數的個數

  27.    cout << "digits10(float): " << numeric_limits<float>::digits10 << endl;  

  28. // 一般基數爲2

  29.    cout << "radix(float): " << numeric_limits<float>::radix << endl;  

  30. // 以2爲基數的最小指數

  31.    cout << "min_exponent(float): " << numeric_limits<float>::min_exponent << endl;  

  32. // 以2爲基數的最大指數

  33.    cout << "max_exponent(float): " << numeric_limits<float>::max_exponent << endl;  

  34. // 以10爲基數的最小指數

  35.    cout << "min_exponent10(float): " << numeric_limits<float>::min_exponent10 << endl;  

  36. // 以10爲基數的最大指數

  37.    cout << "max_exponent10(float): " << numeric_limits<float>::max_exponent10 << endl;  

  38. // 1值和最接近1值的差距

  39.    cout << "epsilon(float): " << numeric_limits<float>::epsilon() << endl;  

  40. // 舍入方式

  41.    cout << "round_style(float): " << numeric_limits<float>::round_style << endl;  

  42. }  


運行結果:


  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. 請按任意鍵繼續. . .  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章