c - 余数 (x,y) 真的是 x-((round(x/y)*y) 吗?

标签 c fmod

阅读@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/

相关文章:

c++ - 使用 FMOD 和 MinGW 编译项目时出现很多错误

c++ - 没有用于调用 FMOD::Studio::System::setListenerAttributes() 的匹配函数

c++ - 我可以在没有 tputs 或 putp 的情况下使用 tparm()

objective-c - Objective-C c block 中的代码未按预期执行

c - 修改作为参数传递的* int []

C# FMOD 从流中播放

c - 这两个结构等效吗?

c - 如何在 C 语言中将 4 个字母的 ascii 单词复制到缓冲区?

iphone - 在多个类中使用 FMOD (iOS)

c++ - FMOD - 未处理的异常,没有可用的源代码