注册 登录
编程论坛 C语言论坛

写了一个简易计算器但计算结果出了问题,有大佬可以帮下忙吗

haozhuo 发布于 2023-08-06 22:15, 1008 次点击
代码如下:
#include <stdio.h>

void main()
{
    float a, b;
    char c;
    scanf("%f%c%f", &a, &c, &b);
    switch(c)
    {
    case 43:printf("结果为%g\n", a+b);break;
    case 45:printf("结果为%g\n", a-b);break;
    case 42:printf("结果为%g\n", a*b);break;
    case 47:printf("结果为%g\n", a/b);break;
    }
}
---------------------------结束---------------------------
我键入了一串“1.08274-1”,但电脑的结果却是0.0827399,而正确答案应该是0.08274,有大佬可以解答为什么会这样吗
4 回复
#2
s962812023-08-06 22:30
[url]https://0.[/url]
同一件事

程序代码:

void main()
{
    printf("%.17f",0.1);
}
->0.10000000000000001
#3
haozhuo2023-08-06 22:33
我刚才又试了一下,好像把%g改为%f结果就变成0.082740,就对了,有大佬知道是为什么吗
#4
haozhuo2023-08-06 22:36
回复 2楼 s96281
#5
apull2023-08-07 11:41
浮点数存储的近似值,输出不限定默认输出6位小数。


[此贴子已经被作者于2023-8-7 11:43编辑过]

1