mirror of
https://github.com/boostorg/interval.git
synced 2026-01-24 18:02:21 +00:00
163 lines
6.9 KiB
HTML
163 lines
6.9 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
|
|
"http://www.w3.org/TR/html4/loose.dtd">
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
<link rel="stylesheet" type="text/css" href="../../../../boost.css">
|
|
<title>Comparisons</title>
|
|
</head>
|
|
|
|
<body>
|
|
<h1>Comparisons</h1>
|
|
|
|
<p>As was said before, the definition of the comparison operators induces a
|
|
slight problem. There are many ways to define them, depending of the return
|
|
type or the expected order. It is the reason why the meaning of the operators
|
|
is not fixed once and for all.</p>
|
|
|
|
<p>The way the operators are defined could have been influenced by a policy,
|
|
as it is already the case for the rounding and the checking. However,
|
|
comparisons are more an external property of the the class rather than an
|
|
internal one. They are meant to be locally modified, independantly of the
|
|
type of the intervals.</p>
|
|
|
|
<p>The operators <code><</code>, <code><=</code>, <code>></code>,
|
|
<code>>=</code>, <code>==</code>, <code>!=</code> are defined each time;
|
|
and like the arithmetic operators they can take an argument of the base type.
|
|
However, due to technical limitations, this base type can only be the second
|
|
argument; so the operators are unfortunately not fully symmetric. The return
|
|
type is not always <code>bool</code>, since some interesting results can be
|
|
achieved by using a tri-state return type. So here is the common signatures
|
|
of the operators:</p>
|
|
<pre>template<class T, class Policies1, class Policies2>
|
|
return_type operator== (const interval<T, Policies1>&, const interval<T, Policies2>&);
|
|
|
|
template<class T, class Policies>
|
|
return_type operator== (const interval<T, Policies>&, const T&);</pre>
|
|
|
|
<h2>Provided comparisons</h2>
|
|
|
|
<h3>Default comparison</h3>
|
|
|
|
<p>If nothing is specified, the meaning of the comparison operators are an
|
|
extension of the operator on the base type. More precisely, if one of the
|
|
argument is invalid or empty, an exception is thrown. If the arguments are
|
|
valid, the following rules are applied to determine the result of
|
|
[<i>a</i>,<i>b</i>] <code>op</code> [<i>c</i>,<i>d</i>] (just consider
|
|
<i>c</i> <code>==</code> <i>d</i> if the second argument is of type
|
|
<code>T</code>):</p>
|
|
<ul>
|
|
<li>if ∀ <i>x</i> ∈ [<i>a</i>,<i>b</i>] ∀ <i>y</i>
|
|
∈ [<i>c</i>,<i>d</i>] <code>(</code><i>x</i> <code>op</code>
|
|
y<code>)</code>, then <code>true</code></li>
|
|
<li>if ∀ <i>x</i> ∈ [<i>a</i>,<i>b</i>] ∀ <i>y</i>
|
|
∈ [<i>c</i>,<i>d</i>] <code>!(</code><i>x</i> <code>op</code>
|
|
y<code>)</code>, then <code>false</code></li>
|
|
<li>otherwise throw an exception.</li>
|
|
</ul>
|
|
|
|
<p>This comparison allows to replace base types by interval types without
|
|
changing the meaning of a program. Indeed, if no exception is thrown, the
|
|
result is the same as before; and if an exception is thrown, the previous
|
|
comparison was unsure and should have been rewritten.</p>
|
|
|
|
<h3>Other comparisons</h3>
|
|
|
|
<p>The other comparisons are selected by using a namespace. These namespaces
|
|
are located under <code>boost::interval_lib::compare</code> and are invoked
|
|
by:</p>
|
|
<pre>using namespace boost::interval_lib::compare::the_comparison_to_select;</pre>
|
|
|
|
<p>After this line, the default meaning of the operators will have been
|
|
replaced by the meaning located in the namespace. Please note that because of
|
|
C++ lookup rules, it is not possible to use two namespaces one after another
|
|
and they must be used in different block hierarchies. Otherwise the compiler
|
|
will complain about ambiguous operators. To summarize:</p>
|
|
<pre>// example 1: BAD
|
|
using namespace compare1;
|
|
...
|
|
using namespace compare2;
|
|
...
|
|
|
|
// example 2: GOOD
|
|
{ using namespace compare1;
|
|
... }
|
|
{ using namespace compare2;
|
|
... }
|
|
|
|
// example 3: BAD
|
|
using namespace compare1;
|
|
...
|
|
{ using namespace compare2;
|
|
... }</pre>
|
|
|
|
<p>Now comes the list of the provided comparisons. They all are located in
|
|
their respective header files under
|
|
<code><boost/numeric/interval/compare/></code>. And as for the default
|
|
comparison, the operators will generally complain by throwing an exception if
|
|
feed by invalid values.</p>
|
|
<ul>
|
|
<li><code>lexicographic</code>: the lexicographic order (the lower bounds
|
|
are first compared, and if it is not enough to know the result, the upper
|
|
bounds are then compared). This order does not have a meaning in interval
|
|
arithmetic. However, since it is the natural total order on pair of
|
|
(totally ordered) numbers, it may be handy in some cases.</li>
|
|
<li><code>set</code>: the set inclusion partial order. This time, an empty
|
|
interval is not considered to be invalid (but an invalid number is still
|
|
invalid). <code><=</code> and <code><</code> are the subset and
|
|
proper subset relations; and <code>>=</code> and <code>></code> are
|
|
the superset and proper superset relations.</li>
|
|
<li><code>tribool</code>: this comparison relies on the Boost Tri-state
|
|
boolean Library and changes the default operators so that an
|
|
indeterminate value is returned in the third case instead of throwing an
|
|
exception.</li>
|
|
</ul>
|
|
|
|
<h3>Exception</h3>
|
|
|
|
<p></p>
|
|
<pre>namespace boost {
|
|
namespace interval_lib {
|
|
|
|
class comparison_error: std::runtime_error; // "boost::interval: uncertain comparison"
|
|
|
|
}
|
|
}</pre>
|
|
|
|
<h2>Explicit comparison functions</h2>
|
|
|
|
<p>In some situation, you may want to perform direct comparisons on the
|
|
bounds and avoid the indeterminate case that appears with default operators.
|
|
Some functions are provided for this purpose. They expect their arguments to
|
|
be valid and return a result after only one comparison. Their names are
|
|
composed by <code>cer</code> (for "certain", if the default comparison is
|
|
true, the result is true) or <code>pos</code> (for "possible", if the default
|
|
comparison is false, the result is false) followed by <code>lt</code>,
|
|
<code>le</code>, <code>gt</code>, <code>ge</code>, <code>eq</code> or
|
|
<code>ne</code>. They are located in
|
|
<code><boost/numeric/interval/compare/explicit.hpp></code>. Each of these
|
|
functions takes two parameters and returns a boolean; the parameters are
|
|
expected to be valid, undefined behavior may result otherwise. For example,
|
|
the definition of the "certainly less than" comparison is:</p>
|
|
<pre>namespace boost {
|
|
namespace interval_lib {
|
|
|
|
template<class T, class Policies1, class Policies2>
|
|
bool cerlt(const interval<T, Policies1>& x, const interval<T, Policies2>& y);
|
|
|
|
template<class T, class Policies>
|
|
bool cerlt(const interval<T, Policies>& x, const T& y);
|
|
|
|
template<class T, class Policies>
|
|
bool cerlt(const T& x, const interval<T, Policies>& y);
|
|
|
|
} // namespace interval_lib
|
|
} // namespace boost</pre>
|
|
<hr>
|
|
|
|
<p>Revised: 2002-10-13<br>
|
|
Copyright (c) Guillaume Melquiond, Sylvain Pion, Hervé Brönnimann, 2002.<br>
|
|
Polytechnic University.</p>
|
|
</body>
|
|
</html>
|