gpt4 book ai didi

c - 四舍五入 float 程序

转载 作者:太空宇宙 更新时间:2023-11-04 03:35:48 25 4
gpt4 key购买 nike

我正在尝试编写一个 C 程序来找到最小的正整数 x,以便 (1/x) * x 不等于 1,使用单精度。我再次以 double 进行。我知道单精度的 x 是 41,但是当我通过编写 C 代码测试它时,我仍然得到 1.00000

这是我的测试代码

int main()
{
float x = 41;
float div = 1/x;
float test = div * x;

printf("%f\n", test);
}

我得到 1.0000 而不是 0.999999

最佳答案

你没有看到问题使用

printf("%f\n", test);

因为 printf 对 float 使用的默认精度是 6。如果将精度提高到 10,您将看到该数字不是 1.0

printf("%.10f\n", test);

为我打印 0.9999999404

关于c - 四舍五入 float 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32852294/

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