gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:37:32 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com