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

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
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

The floating point comparison tools are automatically added if the Unit Test Framework is included as indicated in the previous sections. The tools are implemented is in the header boost/test/tools/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/tools/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