3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>Tolerance-based comparisons</title>
5 <link rel="stylesheet" href="../../../../boostbook.css" type="text/css">
6 <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7 <link rel="home" href="../../../../index.html" title="Boost.Test">
8 <link rel="up" href="../floating_point.html" title="Floating point comparison">
9 <link rel="prev" href="customizing_for_tolerance.html" title="Enabling tolerance for user-defined types">
10 <link rel="next" href="floating_points_comparison_theory.html" title="Theory behind floating point comparisons">
12 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13 <table cellpadding="2" width="100%"><tr>
14 <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../../boost.png"></td>
15 <td align="center"><a href="../../../../../../../../index.html">Home</a></td>
16 <td align="center"><a href="../../../../../../../../libs/libraries.htm">Libraries</a></td>
17 <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18 <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19 <td align="center"><a href="../../../../../../../../more/index.htm">More</a></td>
22 <div class="spirit-nav">
23 <a accesskey="p" href="customizing_for_tolerance.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../floating_point.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="floating_points_comparison_theory.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
26 <div class="titlepage"><div><div><h5 class="title">
27 <a name="boost_test.testing_tools.extended_comparison.floating_point.floating_points_comparison_impl"></a><a class="link" href="floating_points_comparison_impl.html" title="Tolerance-based comparisons">Tolerance-based
29 </h5></div></div></div>
31 Assertions in the <span class="emphasis"><em>Unit Test Framework</em></span> use two kinds
32 of comparison. For <code class="computeroutput"><span class="identifier">u</span></code>
33 being close to zero with absolute tolerance <code class="computeroutput"><span class="identifier">eps</span></code>:
35 <pre class="programlisting"><span class="identifier">abs</span><span class="special">(</span><span class="identifier">u</span><span class="special">)</span> <span class="special"><=</span> <span class="identifier">eps</span><span class="special">;</span> <span class="comment">// (abs)</span>
38 For <code class="computeroutput"><span class="identifier">u</span></code> and <code class="computeroutput"><span class="identifier">v</span></code> being close with relative tolerance
39 <code class="computeroutput"><span class="identifier">eps</span></code>:
41 <pre class="programlisting"> <span class="identifier">abs</span><span class="special">(</span><span class="identifier">u</span> <span class="special">-</span> <span class="identifier">v</span><span class="special">)/</span><span class="identifier">abs</span><span class="special">(</span><span class="identifier">u</span><span class="special">)</span> <span class="special"><=</span> <span class="identifier">eps</span>
42 <span class="special">&&</span> <span class="identifier">abs</span><span class="special">(</span><span class="identifier">u</span> <span class="special">-</span> <span class="identifier">v</span><span class="special">)/</span><span class="identifier">abs</span><span class="special">(</span><span class="identifier">v</span><span class="special">)</span> <span class="special"><=</span> <span class="identifier">eps</span><span class="special">;</span> <span class="comment">// (rel)</span>
45 For rationale for choosing these formulae, see section <a class="link" href="floating_points_comparison_theory.html" title="Theory behind floating point comparisons">Floating
46 point comparison algorithms</a>.
49 Assertion <a class="link" href="../../../utf_reference/testing_tool_ref/assertion_boost_test_universal_macro.html" title="BOOST_TEST"><code class="computeroutput"><span class="identifier">BOOST_TEST</span></code></a> (when comparing floating-point
50 numbers) uses the following algorithm:
52 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
54 When either value <code class="computeroutput"><span class="identifier">u</span></code>
55 or <code class="computeroutput"><span class="identifier">v</span></code> is zero, evaluates
56 formula (abs) on the other value.
59 When the specified tolerance is zero, performs direct (native) comparison
60 between <code class="computeroutput"><span class="identifier">u</span></code> and <code class="computeroutput"><span class="identifier">v</span></code>.
63 Otherwise, performs formula (rel) on <code class="computeroutput"><span class="identifier">u</span></code>
64 and <code class="computeroutput"><span class="identifier">v</span></code>.
67 <div class="note"><table border="0" summary="Note">
69 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../../doc/src/images/note.png"></td>
70 <th align="left">Note</th>
72 <tr><td align="left" valign="top">
74 Therefore in order to check if a number is close to zero with tolerance,
77 <pre class="programlisting"><span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">v</span> <span class="special">==</span> <span class="identifier">T</span><span class="special">(</span><span class="number">0</span><span class="special">),</span> <span class="identifier">tt</span><span class="special">::</span><span class="identifier">tolerance</span><span class="special">(</span><span class="identifier">eps</span><span class="special">));</span>
82 The compatibility assertions <a class="link" href="../../../utf_reference/testing_tool_ref/assertion_boost_level_close.html" title="BOOST_<level>_CLOSE"><code class="computeroutput"><span class="identifier">BOOST_</span><span class="special"><</span><span class="identifier">level</span><span class="special">></span><span class="identifier">_CLOSE</span></code></a> and <a class="link" href="../../../utf_reference/testing_tool_ref/assertion_boost_level_close_fraction.html" title="BOOST_<level>_CLOSE_FRACTION"><code class="computeroutput"><span class="identifier">BOOST_</span><span class="special"><</span><span class="identifier">level</span><span class="special">></span><span class="identifier">_CLOSE_FRACTION</span></code></a> perform formula
86 The compatibility assertion <a class="link" href="../../../utf_reference/testing_tool_ref/assertion_boost_level_small.html" title="BOOST_<level>_SMALL"><code class="computeroutput"><span class="identifier">BOOST_</span><span class="special"><</span><span class="identifier">level</span><span class="special">></span><span class="identifier">_SMALL</span></code></a> performs formula (abs).
89 The <span class="emphasis"><em>Unit Test Framework</em></span> also provides unary predicate
90 <code class="computeroutput"><a class="link" href="../../../../boost/math/fpc/small_with_tolerance.html" title="Class template small_with_tolerance">small_with_tolerance</a></code> and
91 binary predicate predicate <code class="computeroutput"><a class="link" href="../../../../boost/math/fpc/close_at_tolerance.html" title="Class template close_at_tolerance">close_at_tolerance</a></code> that
92 implement formula (abs) and (rel) respectively.
95 <a name="boost_test.testing_tools.extended_comparison.floating_point.floating_points_comparison_impl.h0"></a>
96 <span class="phrase"><a name="boost_test.testing_tools.extended_comparison.floating_point.floating_points_comparison_impl.tolerance_in_operator"></a></span><a class="link" href="floating_points_comparison_impl.html#boost_test.testing_tools.extended_comparison.floating_point.floating_points_comparison_impl.tolerance_in_operator">Tolerance
97 in <code class="computeroutput"><span class="keyword">operator</span><span class="special"><</span></code></a>
100 Tolerance-based computations also apply to <code class="computeroutput"><span class="keyword">operator</span><span class="special"><</span></code> and other relational operators. The
101 semantics are defined as follows:
103 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
104 <li class="listitem">
105 <span class="emphasis"><em>less-at-tolerance</em></span> <==> <span class="emphasis"><em>strictly-less</em></span>
106 and not <span class="emphasis"><em>close-at-tolerance</em></span>
108 <li class="listitem">
109 <span class="emphasis"><em>greater-at-tolerance</em></span> <==> <span class="emphasis"><em>strictly-greater</em></span>
110 and not <span class="emphasis"><em>close-at-tolerance</em></span>
112 <li class="listitem">
113 <span class="emphasis"><em>less-or-equal-at-tolerance</em></span> <==> <span class="emphasis"><em>strictly-less</em></span>
114 or <span class="emphasis"><em>close-at-tolerance</em></span>
116 <li class="listitem">
117 <span class="emphasis"><em>greater-or-equal-at-tolerance</em></span> <==> <span class="emphasis"><em>strictly-greater</em></span>
118 or <span class="emphasis"><em>close-at-tolerance</em></span>
121 <div class="note"><table border="0" summary="Note">
123 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../../doc/src/images/note.png"></td>
124 <th align="left">Note</th>
126 <tr><td align="left" valign="top"><p>
127 This implies that the exactly one of these: <code class="computeroutput"><span class="identifier">u</span>
128 <span class="special"><</span> <span class="identifier">v</span></code>,
129 <code class="computeroutput"><span class="identifier">u</span> <span class="special">==</span>
130 <span class="identifier">v</span></code>, <code class="computeroutput"><span class="identifier">u</span>
131 <span class="special">></span> <span class="identifier">v</span></code>,
132 passes with <a class="link" href="../../../utf_reference/testing_tool_ref/assertion_boost_test_universal_macro.html" title="BOOST_TEST"><code class="computeroutput"><span class="identifier">BOOST_TEST</span></code></a> at any given tolerance.
135 <div class="caution"><table border="0" summary="Caution">
137 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Caution]" src="../../../../../../../../doc/src/images/caution.png"></td>
138 <th align="left">Caution</th>
140 <tr><td align="left" valign="top"><p>
141 Relation <span class="emphasis"><em>less-at-tolerance</em></span> is not a <span class="emphasis"><em>Strict
142 Weak Ordering</em></span> as it lacks the <span class="emphasis"><em>transitivity of
143 the equivalence</em></span>; using it as predicate in <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">map</span></code> or any order-based STL algorithm
144 would result in undefined behavior.
148 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
149 <td align="left"></td>
150 <td align="right"><div class="copyright-footer">Copyright © 2001-2019 Boost.Test
152 Distributed under the Boost Software License, Version 1.0. (See accompanying
153 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
158 <div class="spirit-nav">
159 <a accesskey="p" href="customizing_for_tolerance.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../floating_point.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="floating_points_comparison_theory.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>