Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

PrevUpHomeNext

BOOST_<level>_CLOSE_FRACTION

BOOST_WARN_CLOSE_FRACTION(left, right, tolerance);
BOOST_CHECK_CLOSE_FRACTION(left, right, tolerance);
BOOST_REQUIRE_CLOSE_FRACTION(left, right, tolerance);

These tools are used to check on closeness using strong relationship defined by the predicate

check_is_close(left, right, tolerance)

To check for the weak relationship use BOOST_<level>_PREDICATE family of tools with explicit check_is_close invocation.

The first parameter is the left compared value. The second parameter is the right compared value. Last third parameter defines the tolerance for the comparison as fraction of absolute values being compared.

[Note] Note

It is required for left and right parameters to be of the same floating point type. You will need to explicitly resolve any type mismatch to select which type to use for comparison.

[Note] Note

Note that to use these tools you need to include additional header floating_point_comparison.hpp.

Example: BOOST_<level>_CLOSE_FRACTION usage

Code

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>
#include <boost/test/floating_point_comparison.hpp>

BOOST_AUTO_TEST_CASE( test )
{
  double v1 = 1.111e-10;
  double v2 = 1.112e-10;

  BOOST_CHECK_CLOSE_FRACTION( v1, v2, 0.0008999 );
}

Output

> example
Running 1 test case...
test.cpp(12): error in "test": difference between v1{1.111e-010} and v2{1.112e-010} exceeds 0.0008999

*** 1 failures is detected in test suite "example"

See also:


PrevUpHomeNext