You have to understand that what you have pointed to are boost unit test helper tools, so it may not be a good idea to rely on it outside of the testing domain.
And as a side note, in many cases simply comparing the floating point numbers to be within an epsilon of absolute distance (eg 1e-6) suffices. Otherwise, sure, the relative distance with respect to the magnitude of the comparands is necessary – when more precision is required.
But that doesn't return a boolean
Yes it returns boost::test_tools::predicate_result
but you can evaluate and convert it to a boolean.
Notwithstanding the comment on boost test tools, if you do prefer to use the test library in the way you mentioned, here’s an example for you:
#include <iostream>
#include <boost/test/floating_point_comparison.hpp>
int main() {
bool b = boost::test_tools::check_is_close(
0.01, 0.015, boost::test_tools::percent_tolerance(49.0));
std::cout << std::boolalpha << b << std::endl; // false
b = boost::test_tools::check_is_close(
0.01, 0.015, boost::test_tools::percent_tolerance(51.0));
std::cout << std::boolalpha << b << std::endl; // true, 51% tolerance is enough to ignore the difference
}
And if you need to implement the simpler comparison, you can always roll your own, along the lines of:
#include <cmath>
bool is_close(double a, double b, double epsilon = 1e-5) {
return std::fabs(a - b) < epsilon;
}