【发布时间】:2021-01-16 19:13:31
【问题描述】:
我正在尝试编写灰度代码,但在计算时遇到了问题。谁能解释为什么这会返回 27.00000 而不是 27.66667?
#include <math.h>
#include <stdio.h>
int main(void)
{
float count = ((27 + 28 + 28) / 3);
printf("%f\n", count);
}
【问题讨论】:
-
您正在执行
int/int操作,因此是27。将其更改为(27 + 28 + 28) / 3.0或显式转换(27+28+28) / ((float) 3)
标签: c floating-point double decimal-point