Is fmod() exact when y is an integer?

后端 未结 2 1695
轻奢々
轻奢々 2020-11-27 19:45

In using double fmod(double x, double y) and y is an integer, the result appears to be always exact.

(That is y a w

相关标签:
2条回答
  • 2020-11-27 20:21

    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.

    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.

    0 讨论(0)
  • 2020-11-27 20:27

    The result of fmod is always exact; whether y is an integer is irrelevant. Of course, if x and/or y are already approximations of some real numbers a and b, then fmod(x,y) is unlikely to be exactly equal to a mod b.

    0 讨论(0)
提交回复
热议问题