阅读@anton的答案在这个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
执行的舍入部分不会从零舍入,而是舍入到最接近的偶数。
关于c - 余数 (x,y) 真的是 x-((round(x/y)*y) 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38747884/