英文:
C round() function rounding incorrectly?
问题
当在1140/9上使用round函数时,例如输出结果是126,但1140/9=126.666666667。输出难道不应该是127吗?
我检查了round()函数的定义,但我不明白它是如何返回126而不是127的。
英文:
When using the round function on 1140/9 for example the output is 126, but 1140/9=126.666666667. Shouldn't the output be 127?
Code
I checked the definition of round() and I do not see how it returns 126 instead of 127.
答案1
得分: 3
No — you used integer division! (You said you had 1140/9
which divides two integers, producing an integer result.) The integer result of the division is converted to a double
because the prototype for round()
indicates that's the correct type to pass to round()
, and the function returns the floating-point equivalent of the integer result. Experiment with 1140.0/9.0
— then you'll get the result you expect. You can use a floating-point number for either numerator or denominator or both — all will produce a floating-point result.
#include <math.h>
#include <stdio.h>
int main(void)
{
printf("Integer division: %d; Rounded: %f\n", 1140/9, round(1140/9));
printf("Floating division: %f; Rounded: %f\n", 1140.0/9.0, round(1140.0/9.0));
return 0;
}
Output:
Integer division: 126; Rounded: 126.000000
Floating division: 126.666667; Rounded: 127.000000
Here's a somewhat better demonstration:
#include <math.h>
#include <stdio.h>
int main(void)
{
double v1 = 1140 / 9;
double v2 = round(1140 / 9);
double v3 = 1140.0 / 9.0;
double v4 = round(1140.0 / 9.0);
printf("Integer division: %f; Rounded: %f\n", v1, v2);
printf("Floating division: %f; Rounded: %f\n", v3, v4);
return 0;
}
Output:
Integer division: 126.000000; Rounded: 126.000000
Floating division: 126.666667; Rounded: 127.000000
英文:
No — you used integer division! (You said you had 1140/9
which divides two integers, producing an integer result.) The integer result of the division is converted to a double
because the prototype for round()
indicates that's the correct type to pass to round()
, and the function returns the floating-point equivalent of the integer result. Experiment with 1140.0/9.0
— then you'll get the result you expect. You can use a floating-point number for either numerator or denominator or both — all will produce a floating-point result.
#include <math.h>
#include <stdio.h>
int main(void)
{
printf("Integer division: %d; Rounded: %f\n", 1140/9, round(1140/9));
printf("Floating division: %f; Rounded: %f\n", 1140.0/9.0, round(1140.0/9.0));
return 0;
}
Output:
Integer division: 126; Rounded: 126.000000
Floating division: 126.666667; Rounded: 127.000000
Here's a somewhat better demonstration:
#include <math.h>
#include <stdio.h>
int main(void)
{
double v1 = 1140 / 9;
double v2 = round(1140 / 9);
double v3 = 1140.0 / 9.0;
double v4 = round(1140.0 / 9.0);
printf("Integer division: %f; Rounded: %f\n", v1, v2);
printf("Floating division: %f; Rounded: %f\n", v3, v4);
return 0;
}
Output:
Integer division: 126.000000; Rounded: 126.000000
Floating division: 126.666667; Rounded: 127.000000
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论