use*_*444 8 c++ variables floating-point double
我已经阅读了对浮动使用“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;
Run Code Online (Sandbox Code Playgroud)
在代码片段中,我们可以清楚地看到 short int 的最小值-最大值,例如 -32768 - 32767。这些是可以理解的正确值,但对于 float 和 int,实际值并不清楚。
use*_*444 11
好吧。使用我从这里学到的(谢谢大家)和网络的其他部分,我写了一个关于两者的简洁总结,以防我遇到另一个这样的问题。
在 C++ 中有两种表示/存储十进制值的方法。
-179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0000000000000000双人最低
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0000000000000000双最大值
Float 的精度允许它存储最多 9 位的值(7 位实数,+2 从十进制到二进制的转换)
Double,顾名思义,可以存储两倍于浮点数的精度。它最多可以存储 17 位数字。(15位实数,十进制转二进制+2)
例如
float x = 1.426;
double y = 8.739437;
Run Code Online (Sandbox Code Playgroud)
由于浮点数能够携带 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;
Run Code Online (Sandbox Code Playgroud)
例子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 */
Run Code Online (Sandbox Code Playgroud)
这个描述大概有多准确?混淆时可以作为标准吗?
这一切都可以在 numeric_limits 中找到。
但要小心
由于某种我std::numeric_limits<float>:min()不知道的原因,不返回最小浮点数。相反,它返回以标准化形式表示的最小正浮点数。要获得最小值,请使用std::numeric_limits<float>::lowest(). 我不骗你。同样对于其他浮点类型,即double和long double。
http://en.cppreference.com/w/cpp/types/numeric_limits
标题中的std::numerics_limits类<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。| 归档时间: |
|
| 查看次数: |
43012 次 |
| 最近记录: |