为FMOD()确切当y是一个整数? [英] Is fmod() exact when y is an integer?
问题描述
在使用双FMOD(双X,双Y)
和是
是一个整数,结果出现是的总是的准确。
In using double fmod(double x, double y)
and y
is an integer, the result appears to be always exact.
(即是
整体确切的数字,没有意思 INT
在这里。)
(That is y
a whole exact number, not meaning int
here.)
也许C没有的需要的 FMOD()
来提供这些选择的情况下一个确切的答案,但对编译器我已经试过了,结果是准确的,甚至当 X / Y
是不完全的重新presentable。
Maybe C does not require fmod()
to provide an exact answers in these select cases, but on compilers I've tried, the result is exact, even when the quotient of x/y
is not exactly representable.
- 预计确切的答案时,
是
是一个整数? - 如果不是,请提供一个反例。
- Are exact answers expected when
y
is an integer? - If not, please supply a counter example.
例如:
double x = 1e10;
// x = 10000000000
printf("%.50g\n", fmod(x, 100));
// prints 0
x = 1e60;
// x = 999999999999999949387135297074018866963645011013410073083904
printf("%.50g\n", fmod(x, 100));
// prints 4
x = DBL_MAX;
// x = 179769313486231570...6184124858368
printf("%.50g\n", fmod(x, 100));
// prints 68
x = 123400000000.0 / 9999;
// x = 12341234.1234123408794403076171875
printf("%.50g %a\n", fmod(x, 100), fmod(x, 100));
// prints 34.1234123408794403076171875 0x1.10fcbf9cp+5
注:结果
我的双击
出现在IEEE 754 binary64标准。结果的printf的限制()
不成问题在这里,只需 FMOD()
。
Notes:
My double
appears to the IEEE 754 binary64 compliant.
The limitations of printf()
are not at issue here, just fmod()
.
注:按是准确的答案预期,我问,如果在 FMOD()
结果和数学结果是完全一样的。
Note: By "Are exact answers expected", I was asking if the the fmod()
result and the mathematical result are exactly the same.
推荐答案
在IEEE 754标准定义,其余操作 X REM是
作为数学运算 X - (圆(X / Y)* Y)
。其结果是准确的定义,即使中间业务 X / Y
,圆(X / Y)
等。有不精确重新presentations。
The IEEE Standard 754 defines the remainder operation x REM y
as the mathematical operation x - (round(x/y)*y)
. The result is exact by definition, even when the intermediate operations x/y
, round(x/y)
, etc. have inexact representations.
正如aka.nice指出,上面的定义匹配的libm
库函数剩余
。 FMOD
以不同的方式被定义,要求其结果具有相同的符号 X
。然而,由于之间的差异 FMOD
和剩余
是 0
或是
,我认为,这仍然解释了为什么结果是准确的。
As pointed out by aka.nice, the definition above matches the library function remainder
in libm
. fmod
is defined in a different way, requiring that the result has the same sign as x
. However, since the difference between fmod
and remainder
is either 0
or y
, I believe that this still explains why the result is exact.
这篇关于为FMOD()确切当y是一个整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!