std::fmod(4.2, 0.12) is equal to epsilon * 1.5

Viewed 87
auto a{ 4.2 };
auto b{ 0.12 };
auto result = std::fmod(a, b);
if(result <= std::numeric_limits<double>::epsilon())
  result = 0; // <-- This line isn't triggered

In this example, 4.2 is actually equal to 4.2000000000000002 due to double imprecision.

Note that 4.2/0.12 = 35.

I would expect the output to be equal to std::numeric_limits<double>::epsilon(). Instead, result is equal to 1.5 * std::numeric_limits<double>::epsilon().

Where does this 1.5 multiplier come from?

1 Answers

The result of std::fmod may be expected to be accurate to within a ULP or so, but the machine epsilon is the ULP of 1, not of the result of any given operation.

Related