任何人都可以解釋我如何使用printf中的[.precision]
與說明符「%g」?我很困惑以下輸出:printf的精度與說明符「%g」
double value = 3122.55;
printf("%.16g\n", value); //output: 3122.55
printf("%.17g\n", value); //output: 3122.5500000000002
我知道%g
使用最短的表示法。
但下面還是輸出混淆我
printf("%.16e\n", value); //output: 3.1225500000000002e+03
printf("%.16f\n", value); //output: 3122.5500000000001819
printf("%.17e\n", value); //output: 3.12255000000000018e+03
printf("%.17f\n", value); //output: 3122.55000000000018190
我的問題是:爲什麼%.16g
給出確切的數字,而%.17g
不能?
似乎有16位有效數字是準確的。有誰能告訴我原因嗎?
在IEEE浮點數中永遠不會這樣。 – user35443