gpt4 book ai didi

c - printf float 的精度

转载 作者:太空狗 更新时间:2023-10-29 16:35:58 24 4
gpt4 key购买 nike

众所周知,当您使用 printf 输出 float 的值时,您的精度有限。
但是,有一个技巧可以提高输出的准确性,如本例所示:

#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;
}

我的问题是,为什么人们不经常使用这个技巧?

最佳答案

愚人节?

您的“随机数”1318926965 具有相同的十进制和浮点形式的底层表示。

尝试另一个值,例如 10。它将打印为:

        10
1092616192

所以回答你的问题:

and my question is, why don't people use this trick more often?

因为一年中只有一天是愚人节...其余的日子这个把戏都不管用...

关于c - printf float 的精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9963119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com