阅读@anton在此链接中的答案后,我尝试验证是否真的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));
余数的值为1.000000。
余数的值为-1.000000。
来自维基百科:
浮点数余数。这不像正常的取模运算,对于两个正数它可能是负数。它返回x - (round(x/y)·y)的确切值。
安东的解释是错误的,还是我漏掉了什么?
nearbyint()
函数是否使用银行家舍入法? - rondinoround()
在维基百科的上下文中是什么意思?您能否编辑您的问题并添加round()
函数的定义? - Luis Colorado