comparisons.htm 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-Language" content="en-us">
  6. <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
  7. <link rel="stylesheet" type="text/css" href="../../../../boost.css">
  8. <title>Comparisons</title>
  9. </head>
  10. <body>
  11. <h1>Comparisons</h1>
  12. <p>As was said before, the definition of the comparison operators induces a
  13. slight problem. There are many ways to define them, depending of the return
  14. type or the expected order. It is the reason why the meaning of the
  15. operators is not fixed once and for all.</p>
  16. <p>The way the operators are defined could have been influenced by a
  17. policy, as it is already the case for the rounding and the checking.
  18. However, comparisons are more an external property of the the class rather
  19. than an internal one. They are meant to be locally modified, independantly
  20. of the type of the intervals.</p>
  21. <p>The operators <code>&lt;</code>, <code>&lt;=</code>, <code>&gt;</code>,
  22. <code>&gt;=</code>, <code>==</code>, <code>!=</code> are defined each time;
  23. and like the arithmetic operators they can take an argument of the base
  24. type. However, due to technical limitations, this base type can only be the
  25. second argument; so the operators are unfortunately not fully symmetric.
  26. The return type is not always <code>bool</code>, since some interesting
  27. results can be achieved by using a tri-state return type. So here is the
  28. common signatures of the operators:</p>
  29. <pre>
  30. template&lt;class T, class Policies1, class Policies2&gt;
  31. return_type operator== (const interval&lt;T, Policies1&gt;&amp;, const interval&lt;T, Policies2&gt;&amp;);
  32. template&lt;class T, class Policies&gt;
  33. return_type operator== (const interval&lt;T, Policies&gt;&amp;, const T&amp;);
  34. </pre>
  35. <h2>vided comparisons</h2>
  36. <h3>Default comparison</h3>
  37. <p>If nothing is specified, the meaning of the comparison operators are an
  38. extension of the operator on the base type. More precisely, if one of the
  39. argument is invalid or empty, an exception is thrown. If the arguments are
  40. valid, the following rules are applied to determine the result of
  41. [<i>a</i>,<i>b</i>] <code>op</code> [<i>c</i>,<i>d</i>] (just consider
  42. <i>c</i> <code>==</code> <i>d</i> if the second argument is of type
  43. <code>T</code>):</p>
  44. <ul>
  45. <li>if &forall; <i>x</i> &isin; [<i>a</i>,<i>b</i>] &forall; <i>y</i>
  46. &isin; [<i>c</i>,<i>d</i>] <code>(</code><i>x</i> <code>op</code>
  47. y<code>)</code>, then <code>true</code></li>
  48. <li>if &forall; <i>x</i> &isin; [<i>a</i>,<i>b</i>] &forall; <i>y</i>
  49. &isin; [<i>c</i>,<i>d</i>] <code>!(</code><i>x</i> <code>op</code>
  50. y<code>)</code>, then <code>false</code></li>
  51. <li>otherwise throw an exception.</li>
  52. </ul>
  53. <p>This comparison allows to replace base types by interval types without
  54. changing the meaning of a program. Indeed, if no exception is thrown, the
  55. result is the same as before; and if an exception is thrown, the previous
  56. comparison was unsure and should have been rewritten.</p>
  57. <h3>Other comparisons</h3>
  58. <p>The other comparisons are selected by using a namespace. These
  59. namespaces are located under
  60. <code>boost::numeric::interval_lib::compare</code> and are invoked by:</p>
  61. <pre>
  62. using namespace boost::numeric::interval_lib::compare::the_comparison_to_select;
  63. </pre>
  64. <p>After this line, the default meaning of the operators will have been
  65. replaced by the meaning located in the namespace. Please note that because
  66. of C++ lookup rules, it is not possible to use two namespaces one after
  67. another and they must be used in different block hierarchies. Otherwise the
  68. compiler will complain about ambiguous operators. To summarize:</p>
  69. <pre>
  70. // example 1: BAD
  71. using namespace compare1;
  72. ...
  73. using namespace compare2;
  74. ...
  75. // example 2: GOOD
  76. { using namespace compare1;
  77. ... }
  78. { using namespace compare2;
  79. ... }
  80. // example 3: BAD
  81. using namespace compare1;
  82. ...
  83. { using namespace compare2;
  84. ... }
  85. </pre>
  86. <p>Now comes the list of the provided comparisons. They all are located in
  87. their respective header files under
  88. <code>&lt;boost/numeric/interval/compare/...&gt;</code>. And as for the
  89. default comparison, the operators will generally complain by throwing an
  90. exception if feed by invalid values.</p>
  91. <ul>
  92. <li><code>certain</code>: this comparison is equivalent to the default
  93. scheme with the exceptional case mapped to <code>false</code>. So these
  94. operators answer <code>true</code> only when the comparison is verified
  95. for all pairs of elements.</li>
  96. <li><code>possible</code>: this time, the exceptional case is mapped to
  97. <code>true</code>. The operators answer <code>true</code> as soon as the
  98. comparison is verified for a pair of elements.<br></li>
  99. <li><code>lexicographic</code>: the lexicographic order (the lower bounds
  100. are first compared, and if it is not enough to know the result, the upper
  101. bounds are then compared). This order does not have a meaning in interval
  102. arithmetic. However, since it is the natural total order on pair of
  103. (totally ordered) numbers, it may be handy in some cases.</li>
  104. <li><code>set</code>: the set inclusion partial order. This time, an
  105. empty interval is not considered to be invalid (but an invalid number is
  106. still invalid). <code>&lt;=</code> and <code>&lt;</code> are the subset
  107. and proper subset relations; and <code>&gt;=</code> and <code>&gt;</code>
  108. are the superset and proper superset relations.</li>
  109. <li><code>tribool</code>: this comparison relies on the Boost tristate
  110. boolean library and changes the default operators so that an explicit
  111. indeterminate value is returned in the third case instead of throwing an
  112. exception.</li>
  113. </ul>
  114. <h3>Exception</h3>
  115. <pre>
  116. namespace boost {
  117. namespace numeric {
  118. namespace interval_lib {
  119. class comparison_error: std::runtime_error; // "boost::interval: uncertain comparison"
  120. } // namespace interval_lib
  121. } // namespace numeric
  122. } // namespace boost
  123. </pre>
  124. <h2>Explicit comparison functions</h2>
  125. <p>In some situation, you may want to perform direct comparisons on the
  126. bounds and avoid the indeterminate case that appears with default
  127. operators. Some functions are provided for this purpose. They expect their
  128. arguments to be valid and return a result after only one comparison. Their
  129. names are composed by <code>cer</code> (for "certain", if the default
  130. comparison is true, the result is true) or <code>pos</code> (for
  131. "possible", if the default comparison is false, the result is false)
  132. followed by <code>lt</code>, <code>le</code>, <code>gt</code>,
  133. <code>ge</code>, <code>eq</code> or <code>ne</code>. They are located in
  134. <code>&lt;boost/numeric/interval/compare/explicit.hpp&gt;</code>. Each of
  135. these functions takes two parameters and returns a boolean; the parameters
  136. are expected to be valid, undefined behavior may result otherwise. For
  137. example, the definition of the "certainly less than" comparison is:</p>
  138. <pre>
  139. namespace boost {
  140. namespace numeric {
  141. namespace interval_lib {
  142. template&lt;class T, class Policies1, class Policies2&gt;
  143. bool cerlt(const interval&lt;T, Policies1&gt;&amp; x, const interval&lt;T, Policies2&gt;&amp; y);
  144. template&lt;class T, class Policies&gt;
  145. bool cerlt(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  146. template&lt;class T, class Policies&gt;
  147. bool cerlt(const T&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  148. } // namespace interval_lib
  149. } // namespace numeric
  150. } // namespace boost
  151. </pre>
  152. <hr>
  153. <p><a href="http://validator.w3.org/check?uri=referer"><img border="0" src=
  154. "../../../../doc/images/valid-html401.png" alt="Valid HTML 4.01 Transitional"
  155. height="31" width="88"></a></p>
  156. <p>Revised
  157. <!--webbot bot="Timestamp" s-type="EDITED" s-format="%Y-%m-%d" startspan -->2006-12-24<!--webbot bot="Timestamp" endspan i-checksum="12172" --></p>
  158. <p><i>Copyright &copy; 2002 Guillaume Melquiond, Sylvain Pion, Herv&eacute;
  159. Br&ouml;nnimann, Polytechnic University<br>
  160. Copyright &copy; 2003 Guillaume Melquiond</i></p>
  161. <p><i>Distributed under the Boost Software License, Version 1.0. (See
  162. accompanying file <a href="../../../../LICENSE_1_0.txt">LICENSE_1_0.txt</a>
  163. or copy at <a href=
  164. "http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</i></p>
  165. </body>
  166. </html>