[英]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.