[英]What are the actual min/max values for float and double (C++)
我已經閱讀了對浮動使用“FLT_MIN”和“FLT_MAX”值的建議。 每當我這樣做時,代碼塊都會告訴我
最大值:3.40282e+038 最小值:1.17549e-038
不知道這意味着什么我試圖獲得真正的價值並得到了
最大值:47.2498237715 最小值:-34.8045265148
......但這些並不能說明問題。
這是我的代碼片段
char c; // reserve: 1 byte, store 1 character (-128 to 127)
int i; // reserve: 4 bytes, store -2147483648 to 2147483657
short int s; // reserve: 2 bytes, store -32768 to 32767
float f; // reserve: 4 bytes, store ?? - ?? (? digits)
double d; // reserve: 8 bytes, store ?? - ?? (? digits)
unsigned int u; //reserve: r bytes store 0 to 4294967295
c = 'c';
cout << c <<" lives at " << &c <<endl;
i = 40000;
cout << i <<" lives at " << &i <<endl;
s = 100;
cout << s <<" lives at " << &s <<endl;
f = 10.1;
cout << f <<" lives at " << &f <<endl;
d = 10.102;
cout << d <<" lives at " << &d <<endl;
u = 1723;
cout << u <<" lives at " << &u <<endl;
在代碼片段中,我們可以清楚地看到 short int 的最小值-最大值,例如 -32768 - 32767。這些是可以理解的正確值,但對於 float 和 int,實際值並不明確。
好吧。 使用我從這里學到的(謝謝大家)和網絡的其他部分,我寫了一個關於兩者的簡潔總結,以防我遇到另一個這樣的問題。
在 C++ 中有兩種表示/存儲十進制值的方法。
-179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0000000000000000雙人最低
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0000000000000000雙最大值
Float 的精度允許它存儲最多 9 位的值(7 位實數,+2 從十進制到二進制的轉換)
Double,顧名思義,可以存儲兩倍於浮點數的精度。 它最多可以存儲 17 位數字。 (15位實數,十進制轉二進制+2)
例如
float x = 1.426;
double y = 8.739437;
由於浮點數可以攜帶 7 位實數小數,而雙精度數可以攜帶 15 位實數小數,因此在執行計算時必須使用適當的方法將它們打印出來。
例如
typedef std::numeric_limits<double> dbl;
cout.precision(dbl::max_digits10-2); // sets the precision to the *proper* amount of digits.
cout << dbl::max_digits10 <<endl; // prints 17.
double x = 12345678.312;
double a = 12345678.244;
// these calculations won't perform correctly be printed correctly without setting the precision.
cout << endl << x+a <<endl;
例子2:
typedef std::numeric_limits< float> flt;
cout.precision(flt::max_digits10-2);
cout << flt::max_digits10 <<endl;
float x = 54.122111;
float a = 11.323111;
cout << endl << x+a <<endl; /* without setting precison this outputs a different value, as well as making sure we're *limited* to 7 digits. If we were to enter another digit before the decimal point, the digits on the right would be one less, as there can only be 7. Doubles work in the same way */
這個描述大概有多准確? 混淆時可以作為標准嗎?
這一切都可以在 numeric_limits 中找到。
但要小心
由於某種我不知道的原因, std::numeric_limits<float>:min()
不返回最小浮點數。 相反,它返回以標准化形式表示的最小正浮點數。 要獲得最小值,請使用std::numeric_limits<float>::lowest()
。 我不騙你。 同樣對於其他浮點類型,即double
和long double
。
<limits>
標頭中的std::numerics_limits
類提供有關數字類型特征的信息。
對於浮點類型T
,這里是該類型中可表示的最大值和最小值,在“最大”和“最小”的各種意義上。 我還包括常見 IEEE 754 64 位二進制類型的值,在本答案中稱為double
。 這些按降序排列:
std::numeric_limits<T>::infinity()
是最大可表示值,如果T
支持無窮大。 當然,它是無窮大。 類型T
是否支持無窮大由std::numeric_limits<T>::has_infinity
。
std::numeric_limits<T>::max()
是最大的有限值。 對於double
,這是 2 1024 −2 971 ,大約為 1.79769•10 308 。
std::numeric_limits<T>::min()
是最小的正正態值。 浮點格式通常有一個區間,其中指數不能變小,但允許有效數(數字的小數部分)變小,直到達到零。 這是以精度為代價的,但具有一些理想的數學計算特性。 min()
是這種精度損失開始的點。 對於double
,這是 2 −1022 ,大約為 2.22507•10 −308 。
std::numeric_limits<T>::denorm_min()
是最小的正值。 在具有低於正常值的類型中,它是低於正常的。 否則,它等於std::numeric_limits<T>::min()
。 對於double
,這是 2 −1074 ,大約為 4.94066•10 −324 。
std::numeric_limits<T>::lowest()
是最小有限值。 它通常是一個數量級很大的負數。 對於double
,這是 −(2 1024 −2 971 ),大約為 −1.79769•10 308 。
若std::numeric_limits<T>::has_infinity
和std::numeric_limits<T>::is_signed
為真,則-std::numeric_limits<T>::infinity()
為最小值。 當然,它是負無窮大。
您可能感興趣的另一個特征是:
std::numeric_limits<T>::digits10
是十進制數字的最大數量,因此將任何具有這么多數字的十進制數轉換為T
,然后轉換回相同數量的十進制數字將產生原始數字。 對於double
,這是 15。在 C++ 中,您可以使用std::numeric_limits
類來獲取此類信息。
如果has_infinity
為true
(現在基本上任何平台都會這樣),那么您可以使用infinity
來獲得大於或等於所有其他值(NaN 除外)的值。 它的否定將給出一個負無窮大,並且小於或等於所有其他值(再次是 NaN 除外)。
如果您需要有限值,則可以使用max
,它大於或等於所有其他有限值,而lowest
則小於或等於所有其他有限值。
有點令人困惑的是,min
實際上為您提供了最小的正標准化值,這與它提供的整數類型完全不同步(感謝 @JiveDadson 指出這一點)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.