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 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_CHECK_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