Is fmod() exact when y is an integer?

后端 未结 2 1723
轻奢々
轻奢々 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.

提交回复
热议问题