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

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

1. numeric_limits是什么?

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

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

(B)MSDN

The template class describes arithmetic properties of built-in numerical types.

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

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



上面的意思是說:

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

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

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


(C)我的理解

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

#include <limits>  
#include <iostream>  
using namespace std;  int main() {  cout << boolalpha;  cout << "max(short): " << numeric_limits<short>::max() << endl;  cout << "min(short): " << numeric_limits<short>::min() << endl;  cout << "max(int): " << numeric_limits<int>::max() << endl;  cout << "min(int): " << numeric_limits<int>::min() << endl;  cout << "max(long): " << numeric_limits<long>::max() << endl;  cout << "min(long): " << numeric_limits<long>::min() << endl;  cout << endl;  cout << "max(float): " << numeric_limits<float>::max() << endl;  cout << "min(float): " << numeric_limits<float>::min() << endl;  cout << "max(double): " << numeric_limits<double>::max() << endl;  cout << "min(double): " << numeric_limits<double>::min() << endl;  cout << "max(long double): " << numeric_limits<long double>::max() << endl;  cout << "min(long double): " << numeric_limits<long double>::min() << endl;  cout << endl;  cout << "is_signed(char): "  << numeric_limits<char>::is_signed << endl;  cout << "is_specialized(string): "   << numeric_limits<string>::is_specialized << endl;  
}  

我機器上的運行結果:

max(short): 32767
min(short): -32768
max(int): 2147483647
min(int): -2147483648
max(long): 2147483647
min(long): -2147483648


max(float): 3.40282e+038
min(float): 1.17549e-038
max(double): 1.79769e+308
min(double): 2.22507e-308
max(long double): 1.79769e+308
min(long double): 2.22507e-308


is_signed(char): true
is_specialized(string): false
請按任意鍵繼續. . .


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

而不是最小的float數。

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

3. 基本成員函數

我以float類型來展示:

#include <limits>  
#include <iostream>  
using namespace std;int main() {cout << boolalpha;// 可以表示的最大值  cout << "max(float): " << numeric_limits<float>::max() << endl;// 可以表示的大于0的最小值,其他類型的實現或與此不同  cout << "min(float): " << numeric_limits<float>::min() << endl;// 標準庫是否為其實現了特化  cout << "is_specialized(float): " << numeric_limits<float>::is_specialized << endl;// 是否是有符號的,即可以表示正負值  cout << "is_signed(float): " << numeric_limits<float>::is_signed << endl;// 不否是整形的  cout << "is_integer(float): " << numeric_limits<float>::is_integer << endl;// 是否是精確表示的  cout << "is_exact(float): " << numeric_limits<float>::is_exact << endl;// 是否存在大小界限  cout << "is_bounded(float): " << numeric_limits<float>::is_bounded << endl;// 兩個比較大的數相加而不會溢出,生成一個較小的值  cout << "is_modulo(float): " << numeric_limits<float>::is_modulo << endl;// 是否符合某某標準  cout << "is_iec559(float): " << numeric_limits<float>::is_iec559 << endl;// 不加+-號可以表示的位數  cout << "digits(float): " << numeric_limits<float>::digits << endl;// 十進制數的個數  cout << "digits10(float): " << numeric_limits<float>::digits10 << endl;// 一般基數為2  cout << "radix(float): " << numeric_limits<float>::radix << endl;// 以2為基數的最小指數  cout << "min_exponent(float): " << numeric_limits<float>::min_exponent << endl;// 以2為基數的最大指數  cout << "max_exponent(float): " << numeric_limits<float>::max_exponent << endl;// 以10為基數的最小指數  cout << "min_exponent10(float): " << numeric_limits<float>::min_exponent10 << endl;// 以10為基數的最大指數  cout << "max_exponent10(float): " << numeric_limits<float>::max_exponent10 << endl;// 1值和最接近1值的差距  cout << "epsilon(float): " << numeric_limits<float>::epsilon() << endl;// 舍入方式  cout << "round_style(float): " << numeric_limits<float>::round_style << endl;
}

運行結果:

max(float): 3.40282e+038
min(float): 1.17549e-038
is_specialized(float): true
is_signed(float): true
is_integer(float): false
is_exact(float): false
is_bounded(float): true
is_modulo(float): false
is_iec559(float): true
digits(float): 24
digits10(float): 6
radix(float): 2
min_exponent(float): -125
max_exponent(float): 128
min_exponent10(float): -37
max_exponent10(float): 38
epsilon(float): 1.19209e-007
round_style(float): 1

參考文獻:

http://blog.163.com/wujiaxing009@126/blog/static/7198839920124135147911/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/258930.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/258930.shtml
英文地址,請注明出處:http://en.pswp.cn/news/258930.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

三層架構——配置文件

1、配置文件是什么&#xff1f; 配置文件是隨安裝程序一起被安裝到計算機上的文件&#xff0c;里面存放著安裝好的應用程序執行時所須要的參數。 應用程序配置文件是標準的XML文件&#xff0c;XML標記和屬性是區分大寫和小寫的。它能夠按須要更改&#xff0c;開發者可使用配置文…

《嵌入式系統開發之道——菜鳥成長日志與項目經理的私房菜》——02-04項目范圍(Scope)管理...

本節書摘來異步社區《嵌入式系統開發之道——菜鳥成長日志與項目經理的私房菜》一書中的第2章&#xff0c;第2.4節&#xff0c;作者&#xff1a;邱毅凌&#xff0c;更多章節內容可以訪問云棲社區“異步社區”公眾號查看 02-04項目范圍&#xff08;Scope&#xff09;管理 嵌入式…

flex(入門)之timer的使用,鍵盤,鼠標的監聽

package {import flash.display.Shape;import flash.display.Sprite;import flash.events.Event;import flash.events.KeyboardEvent;import flash.events.MouseEvent;import flash.events.TimerEvent;import flash.utils.Timer;import mx.controls.Label;//窗體大小&#xff0…

python 線程超時設置_python 條件變量Condition(36)

文章首發微信公眾號&#xff0c;微信搜索&#xff1a;猿說python對于線程與線程之間的交互我們在前面的文章已經介紹了 python 互斥鎖Lock / python事件Event , 今天繼續介紹一種線程交互方式 – 線程條件變量Condition.一.線程條件變量Condition相關函數介紹acquire() — 線程…

MsWord 操作總結

轉自&#xff08;http://www.cnblogs.com/eye-like/p/4121219.html&#xff09; Msdn上的word操作api&#xff08;不過只有英文版&#xff0c;英文差的先閃過&#xff09; Word2007的API&#xff1a;http://msdn.microsoft.com/en-us/library/bb257531(voffice.12).aspxWord201…

fwrite,fread and fprintf,fscanf的一些使用體會

這周一直在完成一個任務&#xff0c;就是將訓練出的多個model寫成一個model。其中我們使用了c語言的讀寫方法&#xff0c;搞了一星期&#xff0c; 挖了很多坑&#xff0c;最終都鏟平了。下面列舉出若干有用的知識。 1.fwrite,fread VS fprintf,fscanf的區別 fwrite,fread 讀寫…

《第一桶金怎么賺——淘寶開店創業致富一冊通》一一1.4 淘寶開店創業的流程...

本節書摘來自異步社區出版社《第一桶金怎么賺——淘寶開店創業致富一冊通》一書中的第1章&#xff0c;第1.4節&#xff0c;作者&#xff1a;葛存山&#xff0c;更多章節內容可以訪問云棲社區“異步社區”公眾號查看。 1.4 淘寶開店創業的流程 第一桶金怎么賺——淘寶開店創業致…

檢測虛擬機代碼總匯(更新中)

檢測虛擬機代碼 1 004092D0 /. 55 push ebp2 004092D1 |. 8BEC mov ebp,esp3 004092D3 |. 51 push ecx4 004092D4 |. 53 push ebx5 004092D5 |. 68 1D934000 push 0040931D 6 004092DA |. 64:FF35…

兩數之和 python_同一屏幕播放兩個視頻 視頻左右兩個畫面或視頻上下兩個畫面如何制作...

咱們在網上經常可以看到一些視頻畫面是可以在同一屏幕播放兩個視頻&#xff0c;有的是視頻左右兩個畫面或視頻上下兩個畫面這些是如何制作的呢&#xff0c;其實熟悉視頻編輯軟件的網友應該會比較了解這些操作&#xff0c;好嘞&#xff0c;來&#xff0c;現在就讓小編來演示一下…

dlib人臉特征點對齊

前面我們介紹了使用dlib進行人臉檢測&#xff0c;下面我們給出如何使用dlib進行人臉特征點檢測。我們直接貼出代碼。我們的代碼包括如下幾部分功能&#xff1a; 檢測單張圖片檢測一個視頻檢測一個camera 先給出代碼&#xff1a; #include <dlib/image_processing/frontal_…

IOS開發基礎知識--碎片13

1:運行程序報the file couldnt be opened because you dont have permission to view it 解決辦法&#xff1a;項目—>targets->build settings->build options->changed the value of the "Compiler for C/C/Objective-C" to Default Compiler. 2:百度…

《LoadRunner 12七天速成寶典》—第2章2.6節第二個性能測試案例

本節書摘來自異步社區《LoadRunner 12七天速成寶典》一書中的第2章&#xff0c;第2.6節第二個性能測試案例&#xff0c;作者陳霽&#xff0c;更多章節內容可以訪問云棲社區“異步社區”公眾號查看。 2.6 第二個性能測試案例云云&#xff1a;烤魚吃得很爽。 戀戀&#xff1a;就…

MongoDB_1

突然想去看下MongoDB的東西&#xff0c;于是有了這篇文章。其實很早以前就看過一些關于NoSql的文章&#xff0c;還記得當時里面有介紹MongoDB的&#xff0c;多瞅了2眼&#xff0c;并且在Window下安裝了MongoDB的驅動&#xff0c;小玩了會。今天重新翻出來&#xff0c;沒成想在命…

牛頓法與擬牛頓法,SDM方法的一些注記

SDM方法 考慮一般額NLS問題&#xff1a; f(x)minx||h(x)?y||2這里x為優化參數&#xff0c;h為非線性函數&#xff0c;y是已知變量&#xff0c;如下是基于梯度的迭代公式&#xff1a; ΔxαAJTh(h(x)?y)這里α是步長&#xff0c;A是縮放因子&#xff0c;Jh是h在當前參數x下的…

pyqt5從子目錄加載qrc文件_實戰PyQt5: 045-添加資源文件

添加資源文件在使用PyQt進行圖形界面開發的時候不免要用到一些外部資源&#xff0c;比如圖片&#xff0c;qss配置文件等。在前面代碼中&#xff0c;遇到這類問題&#xff0c;我們使用絕對路徑的方式來解決&#xff0c;這種方式&#xff0c;本身有其不方便之處(比如&#xff0c;…

《 Python樹莓派編程》——2.7 總結

本節書摘來自華章出版社《Python樹莓派編程》一書中的第2章&#xff0c;第2.7節&#xff0c;作者&#xff1a;[美]沃爾弗拉姆多納特&#xff08;Wolfram Donat&#xff09;著 韓德強 等譯&#xff0c;更多章節內容可以訪問云棲社區“華章計算機”公眾號查看。 2.7 總結 本章簡…

ACM的輸入輸出總結

關于ACM的輸入輸出&#xff08;一&#xff09; 一般來說ACM的現場賽會規定輸入輸出 或者是文件輸入標準輸出 也可能是文件輸入文件輸出 如果沒有規定的話那么一般就是標準的輸入輸出了 那說一下輸入輸出的重定向 一般用下面兩種方法 c常用: #include <fstream.h>ifstream…

hdu 2064漢諾塔III 遞推

漢諾塔遞推題&#xff0c;比漢諾塔多了一個限制條件&#xff0c;盤子只允許在相鄰的柱子之間移動。 分析&#xff1a; 第1步:初始狀態&#xff1b; 第2步:把上面的n-1個盤移到第3號桿上&#xff1b; 第3步:把第n個盤從1移到2&#xff1b; 第4步:把前n-1個從3移到1&#xff0c;給…

西門子ddc_鐵門關西門子兩通電動閥VVF42.25-10C+SKD60西

鐵門關西門子兩通電動閥西SIEMENS/西門子電動溫控閥、控制箱、電動蝶閥、電動球閥、超聲波熱量表、超聲波流量計、電磁流量計閥體灰口鑄鐵 EN-GJL-2502.霍尼韋爾主營&#xff1a;樓宇資料系統、熱網自控系統、風機盤管電動兩通閥、空氣壓差開關、水流開關、電動執行器、風閥執行…

swap關于指針的使用

先看下面兩個例子&#xff1a; #include <iostream> // std::cout #include <utility> // std::swapint main() {int x 10, y 20; // x:10 y:20int* p1 &x;int* p2 &y;std::swap(*p1, *p2); // x:20 y:10 …