...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
Assertions in the Unit Test Framework use two kinds
of comparison. For u
being close to zero with absolute tolerance eps
:
abs(u) <= eps; // (abs)
For u
and v
being close with relative tolerance
eps
:
abs(u - v)/abs(u) <= eps && abs(u - v)/abs(v) <= eps; // (rel)
For rationale for choosing these formulae, see section Floating point comparison algorithms.
Assertion BOOST_TEST
(when comparing floating-point
numbers) uses the following algorithm:
u
or v
is zero, evaluates
formula (abs) on the other value.
u
and v
.
u
and v
.
Note | |
---|---|
Therefore in order to check if a number is close to zero with tolerance, you need to type: BOOST_TEST(v == T(0), tt::tolerance(eps)); |
The compatibility assertions BOOST_<level>_CLOSE
and BOOST_<level>_CLOSE_FRACTION
perform formula
(rel).
The compatibility assertion BOOST_<level>_SMALL
performs formula (abs).
The Unit Test Framework also provides unary predicate
small_with_tolerance
and
binary predicate predicate close_at_tolerance
that
implement formula (abs) and (rel) respectively.
operator<
Tolerance-based computations also apply to operator<
and other relational operators. The
semantics are defined as follows:
Note | |
---|---|
This implies that the exactly one of these: |
Caution | |
---|---|
Relation less-at-tolerance is not a Strict
Weak Ordering as it lacks the transitivity of
the equivalence; using it as predicate in |