Freelancer Rates Rounding Error C++

I got this error. I am not sure what it means. Can someone explain it to me?

FAILED:
  REQUIRE_THAT( apply_discount(111.11, 13.5), Catch::Matchers::WithinRel(96.11015, 0.000001) )
with expansion:
  102.8796296296 and 96.1102 are within 0.0001% of each other
at /tmp/freelancer-rates/freelancer_rates_test.cpp:25

Here is the code. I guess there is rounding problem but I am using two doubles.

// apply_discount calculates the price after a discount
double apply_discount(double before_discount, double discount) {
    // TODO: Implement a function to calculate the price after a discount.
    double d = before_discount / discount;
    return before_discount - d;
}
REQUIRE_THAT( apply_discount(111.11, 13.5), Catch::Matchers::WithinRel(96.11015, 0.000001) )

apply_discount(111.11, 13.5) will call your apply_discount function with values 111.11, 13.5. It will check that the value returned from your function is (roughly) equal to the expected value 96.11015.

with expansion:  102.8796296296 and 96.1102 are within 0.0001% of each other

shows that your function returned 102.8796296296 and that it will now check that the returned value is within 0.0001% of 96.1102 … which it is not.

In other words, when apply_discount() is called with arguments 111.11, 13.5, the expected return value is 96.11015 but your function instead returned an incorrect, too-high value of 102.8796296296.

Does that help with understanding the test output?

1 Like

@IsaacG Yes, that does help me understand the output. I will be able to fix my code now.