akr*_*hit 0 c++ floating-point
对于下面的程序,我得到的精度损失为1,我无法理解.需要帮忙.
void main()
{
typedef std::numeric_limits< double > dbl;
cout.precision(dbl::digits10);
double x = -53686781.0;
float xFloat = (float) x;
cout << "x :: " << x << "\n";
cout << "xFloat :: " << xFloat << "\n";
}
Outpput:
x :: -53686781
xFloat :: -53686780
Run Code Online (Sandbox Code Playgroud)
53686781在二进制文件中看起来像这样:11001100110011000111111101.那是26位.
您的浮点数在其尾数部分最多只能存储24位,因此,最终存储了110011001100110001111111.最后两个二进制数字01被截断.
11001100110011000111111100是53686780.
就如此容易.
| 归档时间: |
|
| 查看次数: |
1280 次 |
| 最近记录: |