代碼:
int main(void)
{
double a = 12;
double b = 0.5;
double c = 0.1;
std::cout.precision(25);
std::cout << a << std::endl;
std::cout << b << std::endl;
std::cout << c << std::endl;
std::cout << a + b << std::endl;
std::cout << a + c << std::endl;
return 0;
}
輸出:
12
0.5
0.1000000000000000055511151
12.5
12.09999999999999964472863
爲什麼GCC代表0.1和0.5不同?添加時,它們的表示方式不同。它看起來是0.5,整個數字與其他浮標不同。或者這只是在io庫中發生的事情? 是什麼導致了這種行爲?
[相關](http://stackoverflow.com/questions/5098558/float-vs-double-precision)。 – iammilind
雙打沒有無限精度。您分配的值不能完全使用雙精度來表示。 – MatthewD
我澄清了我的問題。這不關乎精確性。 –