余数 (x,y) 真的是 x-((round(x/y)*y) 吗?
is remainder(x,y) really x-((round(x/y)*y)?
正在阅读 @anton in this link 的答案,我试图看看 remainder(x, y)
是否真的是 x-(round(x/y)*y)
。
运行 x=5.
和 y=2.
值的代码。我得到了:
printf("the value of remainder is %f\n",remainder(x, y));
printf("the value of remainder is %f\n",x-(round(x/y)*y));
the value of remainder is 1.000000
the value of remainder is -1.000000
来自 wikipedia :
Floating point remainder. This is not like a normal modulo operation,
it can be negative for two positive numbers. It returns the exact
value of x–(round(x/y)·y).
Anton 的解释是错误的,还是我遗漏了什么?
remainder
的作用略有不同。来自 man page:
The remainder() function computes the remainder of dividing x by y.
The return value is x-n*y, where n is the value x / y, rounded to the
nearest integer. If the absolute value of x-n*y is 0.5, n is chosen to
be even.
所以在中途情况下 remainder
执行的舍入部分不是从零开始舍入,而是舍入到 最近的偶数 .
正在阅读 @anton in this link 的答案,我试图看看 remainder(x, y)
是否真的是 x-(round(x/y)*y)
。
运行 x=5.
和 y=2.
值的代码。我得到了:
printf("the value of remainder is %f\n",remainder(x, y));
printf("the value of remainder is %f\n",x-(round(x/y)*y));
the value of remainder is 1.000000
the value of remainder is -1.000000
来自 wikipedia :
Floating point remainder. This is not like a normal modulo operation, it can be negative for two positive numbers. It returns the exact value of x–(round(x/y)·y).
Anton 的解释是错误的,还是我遗漏了什么?
remainder
的作用略有不同。来自 man page:
The remainder() function computes the remainder of dividing x by y. The return value is x-n*y, where n is the value x / y, rounded to the nearest integer. If the absolute value of x-n*y is 0.5, n is chosen to be even.
所以在中途情况下 remainder
执行的舍入部分不是从零开始舍入,而是舍入到 最近的偶数 .