#include <stdio.h>
int main(void) {
float score1;
float score2;
float score3;
float value;
printf("Please enter three exam score: ");
scanf("%f", &score1);
scanf("%f", &score2);
scanf("%f", &score3);
printf("First exam: %.1f", score1);
printf("%%\\n");
printf("Second exam: %.0f", score2);
printf("%%\\n");
printf("Third exam: %.0f", score3);
printf("%%\\n");
printf("-----------------------------------");
value = (score1 + score2 + score3) / 3.0;
printf("\\n");
printf("Average: %.14f",value);
printf("%%.\\n");
return 0;
}
-这是我的代码。这三个分数是 75.5 92 100
,它们的平均值是 89.16666666666667%。但我得到 89.16666412353516%。作为我运行程序时的平均值。任何帮助表示赞赏!
谢谢 -Slurpski
最佳答案
改变
浮点值;
至
双倍值;
然后您将获得所需的值,即 89.16666666666667
。
Float
很可能是 32 位 IEEE 754 单精度 float (1 位用于符号,8 位用于指数,23 位用于值)。 float
具有 7 位小数精度。
关于c - 为什么平均数在我的 C 程序中不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71668154/