Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use floating point tolerances in the Catch framework?

I'm using the Catch test framework.

In the introductory blog post the author mentions the following feature:

  • Floating point tolerances supported in an easy to use way

I couldn't find any documentation on how to do this. How is this done in Catch?

like image 285
R. Martinho Fernandes Avatar asked Jul 16 '11 15:07

R. Martinho Fernandes


1 Answers

It's simple. There is a class called Approx that lets you do this test in a very readable manner:

#include <limits> TEST_CASE("demo/approx", "Approx demo") {     double a = 1.0;     double b = a + std::numeric_limits<double>::epsilon();     REQUIRE_FALSE(b == a);     REQUIRE(b == Approx(a)); } 

The tolerance can be changed by using the member functions epsilon() and scale() of the Approx object, like so: Approx(a).epsilon(e).

like image 142
R. Martinho Fernandes Avatar answered Sep 19 '22 12:09

R. Martinho Fernandes