• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
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">
11</head>
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>
20</tr></table>
21<hr>
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>
24</div>
25<div class="section">
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
28          comparisons</a>
29</h5></div></div></div>
30<p>
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>:
34          </p>
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">&lt;=</span> <span class="identifier">eps</span><span class="special">;</span> <span class="comment">// (abs)</span>
36</pre>
37<p>
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>:
40          </p>
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">&lt;=</span> <span class="identifier">eps</span>
42<span class="special">&amp;&amp;</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">&lt;=</span> <span class="identifier">eps</span><span class="special">;</span> <span class="comment">// (rel)</span>
43</pre>
44<p>
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>.
47          </p>
48<p>
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:
51          </p>
52<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
53<li class="listitem">
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.
57              </li>
58<li class="listitem">
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>.
61              </li>
62<li class="listitem">
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>.
65              </li>
66</ul></div>
67<div class="note"><table border="0" summary="Note">
68<tr>
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>
71</tr>
72<tr><td align="left" valign="top">
73<p>
74              Therefore in order to check if a number is close to zero with tolerance,
75              you need to type:
76            </p>
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>
78</pre>
79</td></tr>
80</table></div>
81<p>
82            The compatibility assertions <a class="link" href="../../../utf_reference/testing_tool_ref/assertion_boost_level_close.html" title="BOOST_&lt;level&gt;_CLOSE"><code class="computeroutput"><span class="identifier">BOOST_</span><span class="special">&lt;</span><span class="identifier">level</span><span class="special">&gt;</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_&lt;level&gt;_CLOSE_FRACTION"><code class="computeroutput"><span class="identifier">BOOST_</span><span class="special">&lt;</span><span class="identifier">level</span><span class="special">&gt;</span><span class="identifier">_CLOSE_FRACTION</span></code></a> perform formula
83            (rel).
84          </p>
85<p>
86            The compatibility assertion <a class="link" href="../../../utf_reference/testing_tool_ref/assertion_boost_level_small.html" title="BOOST_&lt;level&gt;_SMALL"><code class="computeroutput"><span class="identifier">BOOST_</span><span class="special">&lt;</span><span class="identifier">level</span><span class="special">&gt;</span><span class="identifier">_SMALL</span></code></a> performs formula (abs).
87          </p>
88<p>
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.
93          </p>
94<h4>
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">&lt;</span></code></a>
98          </h4>
99<p>
100            Tolerance-based computations also apply to <code class="computeroutput"><span class="keyword">operator</span><span class="special">&lt;</span></code> and other relational operators. The
101            semantics are defined as follows:
102          </p>
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> &lt;==&gt; <span class="emphasis"><em>strictly-less</em></span>
106                and not <span class="emphasis"><em>close-at-tolerance</em></span>
107              </li>
108<li class="listitem">
109                <span class="emphasis"><em>greater-at-tolerance</em></span> &lt;==&gt; <span class="emphasis"><em>strictly-greater</em></span>
110                and not <span class="emphasis"><em>close-at-tolerance</em></span>
111              </li>
112<li class="listitem">
113                <span class="emphasis"><em>less-or-equal-at-tolerance</em></span> &lt;==&gt; <span class="emphasis"><em>strictly-less</em></span>
114                or <span class="emphasis"><em>close-at-tolerance</em></span>
115              </li>
116<li class="listitem">
117                <span class="emphasis"><em>greater-or-equal-at-tolerance</em></span> &lt;==&gt; <span class="emphasis"><em>strictly-greater</em></span>
118                or <span class="emphasis"><em>close-at-tolerance</em></span>
119              </li>
120</ul></div>
121<div class="note"><table border="0" summary="Note">
122<tr>
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>
125</tr>
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">&lt;</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">&gt;</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.
133            </p></td></tr>
134</table></div>
135<div class="caution"><table border="0" summary="Caution">
136<tr>
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>
139</tr>
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.
145            </p></td></tr>
146</table></div>
147</div>
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-2020 Boost.Test contributors<p>
151        Distributed under the Boost Software License, Version 1.0. (See accompanying
152        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>)
153      </p>
154</div></td>
155</tr></table>
156<hr>
157<div class="spirit-nav">
158<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>
159</div>
160</body>
161</html>
162