BOOST_CHECK_CLOSE( left, right, tolerance_src )

This tool is 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_CHECK_PREDICATE tool. Note that to use this tool you need to include additional header floating_point_comparison.hpp, since tools implementation depends on this file that does not get included automatically to minimize code dependency.

If comparison is successful, the tool produces a confirmation message (note: to manage what messages appear in the test output stream set the proper log level) in other case it produces an error message in a form "error in <test case name>: test <left argument name> (==) <right argument name> failed [ <left argument value> != <right argument value> (<real predicate value>)]".

The tool's first parameter is the left compared value. The tool's second parameter is the right compared value. Last third parameter defines the tolerance for the comparison. It could be floating point or integer value. 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, 1e-6 ); // should fail at tolerance supplied

    return 0;
}

Output:

test.cpp(4) : error in test_main: test v1 (==) v2 failed [1.23456e-10 != 1.23457e-10 (1e-06)]

Example: test.cpp

int test_main( int, char* [] ) {
    double v1 = 4.1;

    v1 = v1 * v1;
    BOOST_CHECK_CLOSE( v1, 16.81, 1+2 );
    // 1(arithmetic operation) + 
    // 2(decimal to binary conversions) - 
    // number of rounding errors; should pass

    return 0;
}

Output:

 

See Also

BOOST_CHECK_EQUAL, Floating point comparison algorithms