C round() function rounding incorrectly?

huangapple go评论55阅读模式
英文:

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 &lt;math.h&gt;
#include &lt;stdio.h&gt;

int main(void)
{
    printf(&quot;Integer  division: %d;        Rounded: %f\n&quot;, 1140/9, round(1140/9));
    printf(&quot;Floating division: %f; Rounded: %f\n&quot;, 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 &lt;math.h&gt;
#include &lt;stdio.h&gt;

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(&quot;Integer  division: %f; Rounded: %f\n&quot;, v1, v2);
    printf(&quot;Floating division: %f; Rounded: %f\n&quot;, v3, v4);
    return 0;
}

Output:

Integer  division: 126.000000; Rounded: 126.000000
Floating division: 126.666667; Rounded: 127.000000

huangapple
  • 本文由 发表于 2023年3月1日 11:09:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定