衆所周知,當您使用printf
輸出float
的值時,精度有限。
然而,還有一招,以增加輸出的精度,如下例所示:使用printf漂浮物的精度
#include <stdio.h>
int main()
{
float f = 1318926965; /* 10 random digits */
printf("%10.f\n", f); /* prints only 8 correct digits */
printf("%10d\n", *(int*)&f); /* prints all digits correctly */
return 0;
}
,我的問題是,爲什麼沒有人更頻繁地使用這一招?
因爲潛在的未定義行爲是不好的。 (編輯:從技術上講,這可能不是未定義的行爲,因爲我不確定標準對此有何評論。儘管如此,Endianess可能會咬你。) – Corbin 2012-04-01 08:53:56
對於一個好的4月的第一個笑話+1。 – Henrik 2012-04-01 09:03:02
它顯然讓我x.x – Corbin 2012-04-01 09:16:05