BOOST_WARN_CLOSE( left, right, tolerance )
BOOST_CHECK_CLOSE( left, right, tolerance )
BOOST_REQUIRE_CLOSE( left, right, tolerance )

These tools are used to check left and right values on closeness using strong relationship defined by the predicate close_at_tolerance ( tolerance_src ). To check for the weak relationship use BOOST_<level>_PREDICATE tools. Note that to use these tools you need to include additional header floating_point_comparison.hpp.

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 in percentage units. For more details how to treat this value see close_at_tolerance description. Note that it required for left and right parameters to be of the same floating point type. You need to explicitly resolve any type mismatch to select which type to use for comparison.

Example: test.cpp

int test_main( int, char* [] ) {
    double v1 = 1.23456e-10;
    double v2 = 1.23457e-10;

    BOOST_CHECK_CLOSE( v1, v2, 0.0001 );
    // Absolute value of difference between these two values is 1e-15. They seems 
    // to be very close. But we want to checks that these values differ no more then 0.0001%
    // of their value. And this test will fail at tolerance supplied.

    return 0;
}

Output:

test.cpp(4) : error in test_main: difference between v1{1.23456e-10} and v2{1.23457e-10} exeeds 1e-04%

Example: test.cpp

int test_main( int, char* [] ) {
    double v1 = 1.23456e28;
    double v2 = 1.23457e28;

    BOOST_REQUIRE_CLOSE( v1, v2, 0.001 );
    // Absolute value of difference between these two values is 1e+23.
    // But we are interested only that it does not exeed 0.001% of a values compared
    // And this test will pass.

    return 0;
}

Output:

 

See Also

BOOST_CHECK_EQUAL, Floating point comparison algorithms