interval.htm 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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>Boost Interval Arithmetic Library</title>
  9. </head>
  10. <body lang="en">
  11. <h1><img src="../../../../boost.png" alt="boost.png (6897 bytes)" align=
  12. "middle"> Interval Arithmetic Library</h1>
  13. <center>
  14. <table width="80%" summary="">
  15. <tbody>
  16. <tr>
  17. <td><b>Contents of this page:</b><br>
  18. <a href="#intro">Introduction</a><br>
  19. <a href="#synopsis">Synopsis</a><br>
  20. <a href="#interval">Template class <code>interval</code></a><br>
  21. <a href="#opers">Operations and functions</a><br>
  22. <a href="#interval_lib">Interval support library</a><br>
  23. <!--<a href="#compil">Compilation notes</a><br>-->
  24. <a href="#dangers">Common pitfalls and dangers</a><br>
  25. <a href="#rationale">Rationale</a><br>
  26. <a href="#acks">History and Acknowledgments</a></td>
  27. <td><b>Other pages associated with this page:</b><br>
  28. <a href="rounding.htm">Rounding policies</a><br>
  29. <a href="checking.htm">Checking policies</a><br>
  30. <a href="policies.htm">Policies manipulation</a><br>
  31. <a href="comparisons.htm">Comparisons</a><br>
  32. <a href="numbers.htm">Base number type requirements</a><br>
  33. <a href="guide.htm">Choosing your own interval type</a><br>
  34. <a href="examples.htm">Test and example programs</a><br>
  35. <a href="includes.htm">Headers inclusion</a><br>
  36. <a href="todo.htm">Some items on the todo list</a></td>
  37. </tr>
  38. </tbody>
  39. </table>
  40. </center>
  41. <h2 id="intro">Introduction and Overview</h2>
  42. <p>As implied by its name, this library is intended to help manipulating
  43. mathematical intervals. It consists of a single header &lt;<a href=
  44. "../../../../boost/numeric/interval.hpp">boost/numeric/interval.hpp</a>&gt;
  45. and principally a type which can be used as <code>interval&lt;T&gt;</code>.
  46. In fact, this interval template is declared as
  47. <code>interval&lt;T,Policies&gt;</code> where <code>Policies</code> is a
  48. policy class that controls the various behaviours of the interval class;
  49. <code>interval&lt;T&gt;</code> just happens to pick the default policies
  50. for the type <code>T</code>.</p>
  51. <p><span style="color: #FF0000; font-weight: bold">Warning!</span>
  52. Guaranteed interval arithmetic for native floating-point format is not
  53. supported on every combination of processor, operating system, and
  54. compiler. This is a list of systems known to work correctly when using
  55. <code>interval&lt;float&gt;</code> and <code>interval&lt;double&gt;</code>
  56. with basic arithmetic operators.</p>
  57. <ul>
  58. <li>x86-like hardware is supported by the library with GCC, Visual C++
  59. &ge; 7.1, Intel compiler (&ge; 8 on Windows), CodeWarrior (&ge; 9), as
  60. long as the traditional x87 floating-point unit is used for
  61. floating-point computations (no <code>-mfpmath=sse2</code> support).
  62. <ul>
  63. <li>clang (though v8) does not have proper
  64. <a href="http://lists.llvm.org/pipermail/llvm-dev/2018-May/123529.html">rounding support</a>.</li>
  65. <li>gcc requires <tt>-frounding-math</tt> to be specified.</li>
  66. <li>msvc requires <tt>/fp:strict</tt> to be specified, which means
  67. MSVC 10.0 and earlier will not work properly.</li>
  68. </ul>
  69. </li>
  70. <li>Sparc hardware is supported with GCC and Sun compiler.</li>
  71. <li>PowerPC hardware is supported with GCC and CodeWarrior, when
  72. floating-point computations are not done with the Altivec unit.</li>
  73. <li>Alpha hardware is supported with GCC, except maybe for the square
  74. root. The options <code>-mfp-rounding-mode=d -mieee</code> have to be
  75. used.</li>
  76. <li>valgrind cannot be used as it lacks necessary rounding support.</li>
  77. </ul>
  78. <p>The previous list is not exhaustive. And even if a system does not
  79. provide guaranteed computations for hardware floating-point types, the
  80. interval library is still usable with user-defined types and for doing box
  81. arithmetic.</p>
  82. <h3>Interval Arithmetic</h3>
  83. <p>An interval is a pair of numbers which represents all the numbers
  84. between these two. (Intervals are considered closed so the bounds are
  85. included.) The purpose of this library is to extend the usual arithmetic
  86. functions to intervals. These intervals will be written [<i>a</i>,<i>b</i>]
  87. to represent all the numbers between <i>a</i> and <i>b</i> (included).
  88. <i>a</i> and <i>b</i> can be infinite (but they can not be the same
  89. infinite) and <i>a</i> &le; <i>b</i>.</p>
  90. <p>The fundamental property of interval arithmetic is the
  91. <em><strong>inclusion property</strong></em>:</p>
  92. <dl>
  93. <dd>``if <i>f</i> is a function on a set of numbers, <i>f</i> can be
  94. extended to a new function defined on intervals. This new function
  95. <i>f</i> takes one interval argument and returns an interval result such
  96. as: &forall; <i>x</i> &isin; [<i>a</i>,<i>b</i>], <i>f</i>(<i>x</i>)
  97. &isin; <i>f</i>([<i>a</i>,<i>b</i>]).''</dd>
  98. </dl>
  99. <p>Such a property is not limited to functions with only one argument.
  100. Whenever possible, the interval result should be the smallest one able to
  101. satisfy the property (it is not really useful if the new functions always
  102. answer [-&infin;,+&infin;]).</p>
  103. <p>There are at least two reasons a user would like to use this library.
  104. The obvious one is when the user has to compute with intervals. One example
  105. is when input data have some builtin imprecision: instead of a number, an
  106. input variable can be passed as an interval. Another example application is
  107. to solve equations, by bisecting an interval until the interval width is
  108. small enough. A third example application is in computer graphics, where
  109. computations with boxes, segments or rays can be reduced to computations
  110. with points via intervals.</p>
  111. <p>Another common reason to use interval arithmetic is when the computer
  112. doesn't produce exact results: by using intervals, it is possible to
  113. quantify the propagation of rounding errors. This approach is used often in
  114. numerical computation. For example, let's assume the computer stores
  115. numbers with ten decimal significant digits. To the question 1 + 1E-100 -
  116. 1, the computer will answer 0 although the correct answer would be 1E-100.
  117. With the help of interval arithmetic, the computer will answer [0,1E-9].
  118. This is quite a huge interval for such a little result, but the precision
  119. is now known, without having to compute error propagation.</p>
  120. <h3>Numbers, rounding, and exceptional behavior</h3>
  121. <p>The <em><strong>base number type</strong></em> is the type that holds
  122. the bounds of the interval. In order to successfully use interval
  123. arithmetic, the base number type must present some <a href=
  124. "rounding.htm">characteristics</a>. Firstly, due to the definition of an
  125. interval, the base numbers have to be totally ordered so, for instance,
  126. <code>complex&lt;T&gt;</code> is not usable as base number type for
  127. intervals. The mathematical functions for the base number type should also
  128. be compatible with the total order (for instance if x&gt;y and z&gt;t, then
  129. it should also hold that x+z &gt; y+t), so modulo types are not usable
  130. either.</p>
  131. <p>Secondly, the computations must be exact or provide some rounding
  132. methods (for instance, toward minus or plus infinity) if we want to
  133. guarantee the inclusion property. Note that we also may explicitely specify
  134. no rounding, for instance if the base number type is exact, i.e. the result
  135. of a mathematical operation is always computed and represented without loss
  136. of precision. If the number type is not exact, we may still explicitely
  137. specify no rounding, with the obvious consequence that the inclusion
  138. property is no longer guaranteed.</p>
  139. <p>Finally, because heavy loss of precision is always possible, some
  140. numbers have to represent infinities or an exceptional behavior must be
  141. defined. The same situation also occurs for NaN (<i>Not a Number</i>).</p>
  142. <p>Given all this, one may want to limit the template argument T of the
  143. class template <code>interval</code> to the floating point types
  144. <code>float</code>, <code>double</code>, and <code>long double</code>, as
  145. defined by the IEEE-754 Standard. Indeed, if the interval arithmetic is
  146. intended to replace the arithmetic provided by the floating point unit of a
  147. processor, these types are the best choice. Unlike
  148. <code>std::complex</code>, however, we don't want to limit <code>T</code>
  149. to these types. This is why we allow the rounding and exceptional behaviors
  150. to be given by the two policies (rounding and checking). We do nevertheless
  151. provide highly optimized rounding and checking class specializations for
  152. the above-mentioned floating point types.</p>
  153. <h3>Operations and functions</h3>
  154. <p>It is straightforward to define the elementary arithmetic operations on
  155. intervals, being guided by the inclusion property. For instance, if [a,b]
  156. and [c,d] are intervals, [a,b]+[c,d] can be computed by taking the smallest
  157. interval that contains all the numbers x+y for x in [a,b] and y in [c,d];
  158. in this case, rounding a+c down and b+d up will suffice. Other operators
  159. and functions are similarly defined (see their definitions below).</p>
  160. <h3>Comparisons</h3>
  161. <p>It is also possible to define some comparison operators. Given two
  162. intervals, the result is a tri-state boolean type
  163. {<i>false</i>,<i>true,indeterminate</i>}. The answers <i>false</i> and
  164. <i>true</i> are easy to manipulate since they can directly be mapped on the
  165. boolean <i>true</i> and <i>false</i>. But it is not the case for the answer
  166. <em>indeterminate</em> since comparison operators are supposed to be
  167. boolean functions. So, what to do in order to obtain boolean answers?</p>
  168. <p>One solution consists of deciding to adopt an exceptional behavior, such
  169. as a failed assertion or raising an exception. In this case, the
  170. exceptional behavior will be triggered when the result is
  171. indeterminate.</p>
  172. <p>Another solution is to map <em>indeterminate</em> always to
  173. <i>false,</i> or always to <i>true</i>. If <i>false</i> is chosen, the
  174. comparison will be called "<i>certain</i>;" indeed, the result of
  175. [<i>a</i>,<i>b</i>] &lt; [<i>c</i>,<i>d</i>] will be <i>true</i> if and
  176. only if: &forall; <i>x</i> &isin; [<i>a</i>,<i>b</i>] &forall; <i>y</i>
  177. &isin; [<i>c</i>,<i>d</i>], <i>x</i> &lt; <i>y</i>. If <i>true</i> is
  178. chosen, the comparison will be called "<i>possible</i>;" indeed, the result
  179. of [<i>a</i>,<i>b</i>] &lt; [<i>c</i>,<i>d</i>] will be <i>true</i> if and
  180. only if: &exist; <i>x</i> &isin; [<i>a</i>,<i>b</i>] &exist; <i>y</i>
  181. &isin; [<i>c</i>,<i>d</i>], <i>x</i> &lt; <i>y</i>.</p>
  182. <p>Since any of these solution has a clearly defined semantics, it is not
  183. clear that we should enforce either of them. For this reason, the default
  184. behavior consists to mimic the real comparisons by throwing an exception in
  185. the indeterminate case. Other behaviors can be selected bu using specific
  186. comparison namespace. There is also a bunch of explicitely named comparison
  187. functions. See <a href="comparisons.htm">comparisons</a> pages for further
  188. details.</p>
  189. <h3>Overview of the library, and usage</h3>
  190. <p>This library provides two quite distinct levels of usage. One is to use
  191. the basic class template <code>interval&lt;T&gt;</code> without specifying
  192. the policy. This only requires one to know and understand the concepts
  193. developed above and the content of the namespace boost. In addition to the
  194. class <code>interval&lt;T&gt;</code>, this level of usage provides
  195. arithmetic operators (<code>+</code>, <code>-</code>, <code>*</code>,
  196. <code>/</code>), algebraic and piecewise-algebraic functions
  197. (<code>abs</code>, <code>square</code>, <code>sqrt</code>,
  198. <code>pow</code>), transcendental and trigonometric functions
  199. (<code>exp</code>, <code>log</code>, <code>sin</code>, <code>cos</code>,
  200. <code>tan</code>, <code>asin</code>, <code>acos</code>, <code>atan</code>,
  201. <code>sinh</code>, <code>cosh</code>, <code>tanh</code>,
  202. <code>asinh</code>, <code>acosh</code>, <code>atanh</code>), and the
  203. standard comparison operators (<code>&lt;</code>, <code>&lt;=</code>,
  204. <code>&gt;</code>, <code>&gt;=</code>, <code>==</code>, <code>!=</code>),
  205. as well as several interval-specific functions (<code>min</code>,
  206. <code>max</code>, which have a different meaning than <code>std::min</code>
  207. and <code>std::max</code>; <code>lower</code>, <code>upper</code>,
  208. <code>width</code>, <code>median</code>, <code>empty</code>,
  209. <code>singleton</code>, <code>equal</code>, <code>in</code>,
  210. <code>zero_in</code>, <code>subset</code>, <code>proper_subset</code>,
  211. <code>overlap</code>, <code>intersect</code>, <code>hull</code>,
  212. <code>bisect</code>).</p>
  213. <p>For some functions which take several parameters of type
  214. <code>interval&lt;T&gt;</code>, all combinations of argument types
  215. <code>T</code> and <code>interval&lt;T&gt;</code> which contain at least
  216. one <code>interval&lt;T&gt;</code>, are considered in order to avoid a
  217. conversion from the arguments of type <code>T</code> to a singleton of type
  218. <code>interval&lt;T&gt;</code>. This is done for efficiency reasons (the
  219. fact that an argument is a singleton sometimes renders some tests
  220. unnecessary).</p>
  221. <p>A somewhat more advanced usage of this library is to hand-pick the
  222. policies <code>Rounding</code> and <code>Checking</code> and pass them to
  223. <code>interval&lt;T, Policies&gt;</code> through the use of <code>Policies
  224. := boost::numeric::interval_lib::policies&lt;Rounding,Checking&gt;</code>.
  225. Appropriate policies can be fabricated by using the various classes
  226. provided in the namespace <code>boost::numeric::interval_lib</code> as
  227. detailed in section <a href="#interval_lib">Interval Support Library</a>.
  228. It is also possible to choose the comparison scheme by overloading
  229. operators through namespaces.</p>
  230. <h2><a name="synopsis" id="synopsis"></a>Synopsis</h2>
  231. <pre>
  232. namespace boost {
  233. namespace numeric {
  234. namespace interval_lib {
  235. /* this declaration is necessary for the declaration of interval */
  236. template &lt;class T&gt; struct default_policies;
  237. /* ... ; the full synopsis of namespace interval_lib can be found <a href=
  238. "#interval_lib">here</a> */
  239. } // namespace interval_lib
  240. /* template interval_policies; class definition can be found <a href=
  241. "policies.htm">here</a> */
  242. template&lt;class Rounding, class Checking&gt;
  243. struct interval_policies;
  244. /* template class interval; class definition can be found <a href=
  245. "#interval">here</a> */
  246. template&lt;class T, class Policies = typename interval_lib::default_policies&lt;T&gt;::type &gt; class interval;
  247. /* arithmetic operators involving intervals */
  248. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; operator+(const interval&lt;T, Policies&gt;&amp; x);
  249. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; operator-(const interval&lt;T, Policies&gt;&amp; x);
  250. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; operator+(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  251. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; operator+(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  252. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; operator+(const T&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  253. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; operator-(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  254. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; operator-(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  255. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; operator-(const T&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  256. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; operator*(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  257. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; operator*(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  258. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; operator*(const T&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  259. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; operator/(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  260. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; operator/(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  261. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; operator/(const T&amp; r, const interval&lt;T, Policies&gt;&amp; x);
  262. /* algebraic functions: sqrt, abs, square, pow, nth_root */
  263. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; abs(const interval&lt;T, Policies&gt;&amp; x);
  264. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; sqrt(const interval&lt;T, Policies&gt;&amp; x);
  265. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; square(const interval&lt;T, Policies&gt;&amp; x);
  266. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; pow(const interval&lt;T, Policies&gt;&amp; x, int y);
  267. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; nth_root(const interval&lt;T, Policies&gt;&amp; x, int y);
  268. /* transcendental functions: exp, log */
  269. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; exp(const interval&lt;T, Policies&gt;&amp; x);
  270. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; log(const interval&lt;T, Policies&gt;&amp; x);
  271. /* fmod, for trigonometric function argument reduction (see below) */
  272. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; fmod(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  273. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; fmod(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  274. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; fmod(const T&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  275. /* trigonometric functions */
  276. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; sin(const interval&lt;T, Policies&gt;&amp; x);
  277. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; cos(const interval&lt;T, Policies&gt;&amp; x);
  278. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; tan(const interval&lt;T, Policies&gt;&amp; x);
  279. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; asin(const interval&lt;T, Policies&gt;&amp; x);
  280. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; acos(const interval&lt;T, Policies&gt;&amp; x);
  281. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; atan(const interval&lt;T, Policies&gt;&amp; x);
  282. /* hyperbolic trigonometric functions */
  283. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; sinh(const interval&lt;T, Policies&gt;&amp; x);
  284. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; cosh(const interval&lt;T, Policies&gt;&amp; x);
  285. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; tanh(const interval&lt;T, Policies&gt;&amp; x);
  286. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; asinh(const interval&lt;T, Policies&gt;&amp; x);
  287. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; acosh(const interval&lt;T, Policies&gt;&amp; x);
  288. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; atanh(const interval&lt;T, Policies&gt;&amp; x);
  289. /* min, max external functions (NOT std::min/max, see below) */
  290. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; max(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  291. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; max(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  292. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; max(const T&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  293. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; min(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  294. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; min(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  295. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; min(const T&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  296. /* bounds-related interval functions */
  297. template &lt;class T, class Policies&gt; T lower(const interval&lt;T, Policies&gt;&amp; x);
  298. template &lt;class T, class Policies&gt; T upper(const interval&lt;T, Policies&gt;&amp; x);
  299. template &lt;class T, class Policies&gt; T width(const interval&lt;T, Policies&gt;&amp; x);
  300. template &lt;class T, class Policies&gt; T median(const interval&lt;T, Policies&gt;&amp; x);
  301. template &lt;class T, class Policies&gt; T norm(const interval&lt;T, Policies&gt;&amp; x);
  302. /* bounds-related interval functions */
  303. template &lt;class T, class Policies&gt; bool empty(const interval&lt;T, Policies&gt;&amp; b);
  304. template &lt;class T, class Policies&gt; bool singleton(const interval&lt;T, Policies&gt;&amp; x);
  305. template &lt;class T, class Policies&gt; bool equal(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  306. template &lt;class T, class Policies&gt; bool in(const T&amp; r, const interval&lt;T, Policies&gt;&amp; b);
  307. template &lt;class T, class Policies&gt; bool zero_in(const interval&lt;T, Policies&gt;&amp; b);
  308. template &lt;class T, class Policies&gt; bool subset(const interval&lt;T, Policies&gt;&amp; a, const interval&lt;T, Policies&gt;&amp; b);
  309. template &lt;class T, class Policies&gt; bool proper_subset(const interval&lt;T, Policies&gt;&amp; a, const interval&lt;T, Policies&gt;&amp; b);
  310. template &lt;class T, class Policies&gt; bool overlap(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  311. /* set manipulation interval functions */
  312. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; intersect(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  313. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; hull(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  314. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; hull(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  315. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; hull(const T&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  316. template &lt;class T, class Policies&gt; interval&lt;T, Policies&gt; hull(const T&amp; x, const T&amp; y);
  317. template &lt;class T, class Policies&gt; std::pair&lt;interval&lt;T, Policies&gt;, interval&lt;T, Policies&gt; &gt; bisect(const interval&lt;T, Policies&gt;&amp; x);
  318. /* interval comparison operators */
  319. template&lt;class T, class Policies&gt; bool operator&lt;(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  320. template&lt;class T, class Policies&gt; bool operator&lt;(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  321. template&lt;class T, class Policies&gt; bool operator&lt;(const T&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  322. template&lt;class T, class Policies&gt; bool operator&lt;=(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  323. template&lt;class T, class Policies&gt; bool operator&lt;=(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  324. template&lt;class T, class Policies&gt; bool operator&lt;=(const T&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  325. template&lt;class T, class Policies&gt; bool operator&gt;(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  326. template&lt;class T, class Policies&gt; bool operator&gt;(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  327. template&lt;class T, class Policies&gt; bool operator&gt;(const T&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  328. template&lt;class T, class Policies&gt; bool operator&gt;=(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  329. template&lt;class T, class Policies&gt; bool operator&gt;=(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  330. template&lt;class T, class Policies&gt; bool operator&gt;=(const T&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  331. </pre>
  332. <pre>
  333. template&lt;class T, class Policies&gt; bool operator==(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  334. template&lt;class T, class Policies&gt; bool operator==(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  335. template&lt;class T, class Policies&gt; bool operator==(const T&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  336. template&lt;class T, class Policies&gt; bool operator!=(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  337. template&lt;class T, class Policies&gt; bool operator!=(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  338. template&lt;class T, class Policies&gt; bool operator!=(const T&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  339. namespace interval_lib {
  340. template&lt;class T, class Policies&gt; interval&lt;T, Policies&gt; division_part1(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&amp; y, bool&amp; b);
  341. template&lt;class T, class Policies&gt; interval&lt;T, Policies&gt; division_part2(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&amp; y, bool b = true);
  342. template&lt;class T, class Policies&gt; interval&lt;T, Policies&gt; multiplicative_inverse(const interval&lt;T, Policies&gt;&amp; x);
  343. template&lt;class I&gt; I add(const typename I::base_type&amp; x, const typename I::base_type&amp; y);
  344. template&lt;class I&gt; I sub(const typename I::base_type&amp; x, const typename I::base_type&amp; y);
  345. template&lt;class I&gt; I mul(const typename I::base_type&amp; x, const typename I::base_type&amp; y);
  346. template&lt;class I&gt; I div(const typename I::base_type&amp; x, const typename I::base_type&amp; y);
  347. } // namespace interval_lib
  348. } // namespace numeric
  349. } // namespace boost
  350. </pre>
  351. <h2><a name="interval" id="interval"></a>Template class
  352. <code>interval</code></h2>The public interface of the template class
  353. interval itself is kept at a simplest minimum:
  354. <pre>
  355. template &lt;class T, class Policies = typename interval_lib::default_policies&lt;T&gt;::type&gt;
  356. class interval
  357. {
  358. public:
  359. typedef T base_type;
  360. typedef Policies traits_type;
  361. interval();
  362. interval(T const &amp;v);
  363. template&lt;class T1&gt; interval(T1 const &amp;v);
  364. interval(T const &amp;l, T const &amp;u);
  365. template&lt;class T1, class T2&gt; interval(T1 const &amp;l, T2 const &amp;u);
  366. interval(interval&lt;T, Policies&gt; const &amp;r);
  367. template&lt;class Policies1&gt; interval(interval&lt;T, Policies1&gt; const &amp;r);
  368. template&lt;class T1, class Policies1&gt; interval(interval&lt;T1, Policies1&gt; const &amp;r);
  369. interval &amp;operator=(T const &amp;v);
  370. template&lt;class T1&gt; interval &amp;operator=(T1 const &amp;v);
  371. interval &amp;operator=(interval&lt;T, Policies&gt; const &amp;r);
  372. template&lt;class Policies1&gt; interval &amp;operator=(interval&lt;T, Policies1&gt; const &amp;r);
  373. template&lt;class T1, class Policies1&gt; interval &amp;operator=(interval&lt;T1, Policies1&gt; const &amp;r);
  374. void assign(T const &amp;l, T const &amp;u);
  375. T const &amp;lower() const;
  376. T const &amp;upper() const;
  377. static interval empty();
  378. static interval whole();
  379. static interval hull(T const &amp;x, T const &amp;y);
  380. interval&amp; operator+= (T const &amp;r);
  381. interval&amp; operator-= (T const &amp;r);
  382. interval&amp; operator*= (T const &amp;r);
  383. interval&amp; operator/= (T const &amp;r);
  384. interval&amp; operator+= (interval const &amp;r);
  385. interval&amp; operator-= (interval const &amp;r);
  386. interval&amp; operator*= (interval const &amp;r);
  387. interval&amp; operator/= (interval const &amp;r);
  388. };
  389. </pre>
  390. <p>The constructors create an interval enclosing their arguments. If there
  391. are two arguments, the first one is assumed to be the left bound and the
  392. second one is the right bound. Consequently, the arguments need to be
  393. ordered. If the property !(l &lt;= u) is not respected, the checking policy
  394. will be used to create an empty interval. If no argument is given, the
  395. created interval is the singleton zero.</p>
  396. <p>If the type of the arguments is the same as the base number type, the
  397. values are directly used for the bounds. If it is not the same type, the
  398. library will use the rounding policy in order to convert the arguments
  399. (<code>conv_down</code> and <code>conv_up</code>) and create an enclosing
  400. interval. When the argument is an interval with a different policy, the
  401. input interval is checked in order to correctly propagate its emptiness (if
  402. empty).</p>
  403. <p>The assignment operators behave similarly, except they obviously take
  404. one argument only. There is also an <code>assign</code> function in order
  405. to directly change the bounds of an interval. It behaves like the
  406. two-arguments constructors if the bounds are not ordered. There is no
  407. assign function that directly takes an interval or only one number as a
  408. parameter; just use the assignment operators in such a case.</p>
  409. <p>The type of the bounds and the policies of the interval type define the
  410. set of values the intervals contain. E.g. with the default policies,
  411. intervals are subsets of the set of real numbers. The static functions
  412. <code>empty</code> and <code>whole</code> produce the intervals/subsets
  413. that are respectively the empty subset and the whole set. They are static
  414. member functions rather than global functions because they cannot guess
  415. their return types. Likewise for <code>hull</code>. <code>empty</code> and
  416. <code>whole</code> involve the checking policy in order to get the bounds
  417. of the resulting intervals.</p>
  418. <h2><a name="opers" id="opers"></a>Operations and Functions</h2>
  419. <p>Some of the following functions expect <code>min</code> and
  420. <code>max</code> to be defined for the base type. Those are the only
  421. requirements for the <code>interval</code> class (but the policies can have
  422. other requirements).</p>
  423. <h4>Operators <code>+</code> <code>-</code> <code>*</code> <code>/</code>
  424. <code>+=</code> <code>-=</code> <code>*=</code> <code>/=</code></h4>
  425. <p>The basic operations are the unary minus and the binary <code>+</code>
  426. <code>-</code> <code>*</code> <code>/</code>. The unary minus takes an
  427. interval and returns an interval. The binary operations take two intervals,
  428. or one interval and a number, and return an interval. If an argument is a
  429. number instead of an interval, you can expect the result to be the same as
  430. if the number was first converted to an interval. This property will be
  431. true for all the following functions and operators.</p>
  432. <p>There are also some assignment operators <code>+=</code> <code>-=</code>
  433. <code>*=</code> <code>/=</code>. There is not much to say: <code>x op=
  434. y</code> is equivalent to <code>x = x op y</code>. If an exception is
  435. thrown during the computations, the l-value is not modified (but it may be
  436. corrupt if an exception is thrown by the base type during an
  437. assignment).</p>
  438. <p>The operators <code>/</code> and <code>/=</code> will try to produce an
  439. empty interval if the denominator is exactly zero. If the denominator
  440. contains zero (but not only zero), the result will be the smallest interval
  441. containing the set of division results; so one of its bound will be
  442. infinite, but it may not be the whole interval.</p>
  443. <h4><code>lower</code> <code>upper</code> <code>median</code>
  444. <code>width</code> <code>norm</code></h4>
  445. <p><code>lower</code>, <code>upper</code>, <code>median</code> respectively
  446. compute the lower bound, the upper bound, and the median number of an
  447. interval (<code>(lower+upper)/2</code> rounded to nearest).
  448. <code>width</code> computes the width of an interval
  449. (<code>upper-lower</code> rounded toward plus infinity). <code>norm</code>
  450. computes an upper bound of the interval in absolute value; it is a
  451. mathematical norm (hence the name) similar to the absolute value for real
  452. numbers.</p>
  453. <h4><code>min</code> <code>max</code> <code>abs</code> <code>square</code>
  454. <code>pow</code> <code>nth_root</code> <code>division_part?</code>
  455. <code>multiplicative_inverse</code></h4>
  456. <p>The functions <code>min</code>, <code>max</code> and <code>abs</code>
  457. are also defined. Please do not mistake them for the functions defined in
  458. the standard library (aka <code>a&lt;b?a:b</code>, <code>a&gt;b?a:b</code>,
  459. <code>a&lt;0?-a:a</code>). These functions are compatible with the
  460. elementary property of interval arithmetic. For example,
  461. max([<i>a</i>,<i>b</i>], [<i>c</i>,<i>d</i>]) = {max(<i>x</i>,<i>y</i>)
  462. such that <i>x</i> in [<i>a</i>,<i>b</i>] and <i>y</i> in
  463. [<i>c</i>,<i>d</i>]}. They are not defined in the <code>std</code>
  464. namespace but in the boost namespace in order to avoid conflict with the
  465. other definitions.</p>
  466. <p>The <code>square</code> function is quite particular. As you can expect
  467. from its name, it computes the square of its argument. The reason this
  468. function is provided is: <code>square(x)</code> is not <code>x*x</code> but
  469. only a subset when <code>x</code> contains zero. For example, [-2,2]*[-2,2]
  470. = [-4,4] but [-2,2]&sup2; = [0,4]; the result is a smaller interval.
  471. Consequently, <code>square(x)</code> should be used instead of
  472. <code>x*x</code> because of its better accuracy and a small performance
  473. improvement.</p>
  474. <p>As for <code>square</code>, <code>pow</code> provides an efficient and
  475. more accurate way to compute the integer power of an interval. Please note:
  476. when the power is 0 and the interval is not empty, the result is 1, even if
  477. the input interval contains 0. <code>nth_root</code> computes the integer root
  478. of an interval (<code>nth_root(pow(x,k),k)</code> encloses <code>x</code> or
  479. <code>abs(x)</code> depending on whether <code>k</code> is odd or even).
  480. The behavior of <code>nth_root</code> is not defined if the integer argument is
  481. not positive. <code>multiplicative_inverse</code> computes
  482. <code>1/x</code>.</p>
  483. <p>The functions <code>division_part1</code> and
  484. <code>division_part2</code> are useful when the user expects the division
  485. to return disjoint intervals if necessary. For example, the narrowest
  486. closed set containing [2,3] / [-2,1] is not ]-&infin;,&infin;[ but the union
  487. of ]-&infin;,-1] and [2,&infin;[. When the result of the division is
  488. representable by only one interval, <code>division_part1</code> returns
  489. this interval and sets the boolean reference to <code>false</code>.
  490. However, if the result needs two intervals, <code>division_part1</code>
  491. returns the negative part and sets the boolean reference to
  492. <code>true</code>; a call to <code>division_part2</code> is now needed to
  493. get the positive part. This second function can take the boolean returned
  494. by the first function as last argument. If this bool is not given, its
  495. value is assumed to be true and the behavior of the function is then
  496. undetermined if the division does not produce a second interval.</p>
  497. <h4><code>intersect</code> <code>hull</code> <code>overlap</code>
  498. <code>in</code> <code>zero_in</code> <code>subset</code>
  499. <code>proper_subset</code> <code>empty</code> <code>singleton</code>
  500. <code>equal</code></h4>
  501. <p><code>intersect</code> computes the set intersection of two closed sets,
  502. <code>hull</code> computes the smallest interval which contains the two
  503. parameters; those parameters can be numbers or intervals. If one of the
  504. arguments is an invalid number or an empty interval, the function will only
  505. use the other argument to compute the resulting interval (if allowed by the
  506. checking policy).</p>
  507. <p>There is no union function since the union of two intervals is not an
  508. interval if they do not overlap. If they overlap, the <code>hull</code>
  509. function computes the union.</p>
  510. <p>The function <code>overlap</code> tests if two intervals have some
  511. common subset. <code>in</code> tests if a number is in an interval;
  512. <code>zero_in</code> is a variant which tests if zero is in the interval.
  513. <code>subset</code> tests if the first interval is a subset of the second;
  514. and <code>proper_subset</code> tests if it is a proper subset.
  515. <code>empty</code> and <code>singleton</code> test if an interval is empty
  516. or is a singleton. Finally, <code>equal</code> tests if two intervals are
  517. equal.</p>
  518. <h4><code>sqrt</code> <code>log</code> <code>exp</code> <code>sin</code>
  519. <code>cos</code> <code>tan</code> <code>asin</code> <code>acos</code>
  520. <code>atan</code> <code>sinh</code> <code>cosh</code> <code>tanh</code>
  521. <code>asinh</code> <code>acosh</code> <code>atanh</code>
  522. <code>fmod</code></h4>
  523. <p>The functions <code>sqrt</code>, <code>log</code>, <code>exp</code>,
  524. <code>sin</code>, <code>cos</code>, <code>tan</code>, <code>asin</code>,
  525. <code>acos</code>, <code>atan</code>, <code>sinh</code>, <code>cosh</code>,
  526. <code>tanh</code>, <code>asinh</code>, <code>acosh</code>,
  527. <code>atanh</code> are also defined. There is not much to say; these
  528. functions extend the traditional functions to the intervals and respect the
  529. basic property of interval arithmetic. They use the <a href=
  530. "checking.htm">checking</a> policy to produce empty intervals when the
  531. input interval is strictly outside of the domain of the function.</p>
  532. <p>The function <code>fmod(interval x, interval y)</code> expects the lower
  533. bound of <code>y</code> to be strictly positive and returns an interval
  534. <code>z</code> such as <code>0 &lt;= z.lower() &lt; y.upper()</code> and
  535. such as <code>z</code> is a superset of <code>x-n*y</code> (with
  536. <code>n</code> being an integer). So, if the two arguments are positive
  537. singletons, this function <code>fmod(interval, interval)</code> will behave
  538. like the traditional function <code>fmod(double, double)</code>.</p>
  539. <p>Please note that <code>fmod</code> does not respect the inclusion
  540. property of arithmetic interval. For example, the result of
  541. <code>fmod</code>([13,17],[7,8]) should be [0,8] (since it must contain
  542. [0,3] and [5,8]). But this answer is not really useful when the purpose is
  543. to restrict an interval in order to compute a periodic function. It is the
  544. reason why <code>fmod</code> will answer [5,10].</p>
  545. <h4><code>add</code> <code>sub</code> <code>mul</code>
  546. <code>div</code></h4>
  547. <p>These four functions take two numbers and return the enclosing interval
  548. for the operations. It avoids converting a number to an interval before an
  549. operation, it can result in a better code with poor optimizers.</p>
  550. <h3>Constants</h3>
  551. <p>Some constants are hidden in the
  552. <code>boost::numeric::interval_lib</code> namespace. They need to be
  553. explicitely templated by the interval type. The functions are
  554. <code>pi&lt;I&gt;()</code>, <code>pi_half&lt;I&gt;()</code> and
  555. <code>pi_twice&lt;I&gt;()</code>, and they return an object of interval
  556. type <code>I</code>. Their respective values are &pi;, &pi;/2 and
  557. 2&pi;.</p>
  558. <h3>Exception throwing</h3>
  559. <p>The interval class and all the functions defined around this class never
  560. throw any exceptions by themselves. However, it does not mean that an
  561. operation will never throw an exception. For example, let's consider the
  562. copy constructor. As explained before, it is the default copy constructor
  563. generated by the compiler. So it will not throw an exception if the copy
  564. constructor of the base type does not throw an exception.</p>
  565. <p>The same situation applies to all the functions: exceptions will only be
  566. thrown if the base type or one of the two policies throws an exception.</p>
  567. <h2 id="interval_lib">Interval Support Library</h2>
  568. <p>The interval support library consists of a collection of classes that
  569. can be used and combined to fabricate almost various commonly-needed
  570. interval policies. In contrast to the basic classes and functions which are
  571. used in conjunction with <code>interval&lt;T&gt;</code> (and the default
  572. policies as the implicit second template parameter in this type), which
  573. belong simply to the namespace <code>boost</code>, these components belong
  574. to the namespace <code>boost::numeric::interval_lib</code>.</p>
  575. <p>We merely give the synopsis here and defer each section to a separate
  576. web page since it is only intended for the advanced user. This allows to
  577. expand on each topic with examples, without unduly stretching the limits of
  578. this document.</p>
  579. <h4>Synopsis</h4>
  580. <pre>
  581. namespace boost {
  582. namespace numeric {
  583. namespace interval_lib {
  584. <span style=
  585. "color: #FF0000">/* built-in rounding policy and its specializations */</span>
  586. template &lt;class T&gt; struct rounded_math;
  587. template &lt;&gt; struct rounded_math&lt;float&gt;;
  588. template &lt;&gt; struct rounded_math&lt;double&gt;;
  589. template &lt;&gt; struct rounded_math&lt;long double&gt;;
  590. <span style=
  591. "color: #FF0000">/* built-in rounding construction blocks */</span>
  592. template &lt;class T&gt; struct rounding_control;
  593. template &lt;class T, class Rounding = rounding_control&lt;T&gt; &gt; struct rounded_arith_exact;
  594. template &lt;class T, class Rounding = rounding_control&lt;T&gt; &gt; struct rounded_arith_std;
  595. template &lt;class T, class Rounding = rounding_control&lt;T&gt; &gt; struct rounded_arith_opp;
  596. template &lt;class T, class Rounding&gt; struct rounded_transc_dummy;
  597. template &lt;class T, class Rounding = rounded_arith_exact&lt;T&gt; &gt; struct rounded_transc_exact;
  598. template &lt;class T, class Rounding = rounded_arith_std &lt;T&gt; &gt; struct rounded_transc_std;
  599. template &lt;class T, class Rounding = rounded_arith_opp &lt;T&gt; &gt; struct rounded_transc_opp;
  600. template &lt;class Rounding&gt; struct save_state;
  601. template &lt;class Rounding&gt; struct save_state_nothing;
  602. <span style="color: #FF0000">/* built-in checking policies */</span>
  603. template &lt;class T&gt; struct checking_base;
  604. template &lt;class T, class Checking = checking_base&lt;T&gt;, class Exception = exception_create_empty&gt; struct checking_no_empty;
  605. template &lt;class T, class Checking = checking_base&lt;T&gt; &gt; struct checking_no_nan;
  606. template &lt;class T, class Checking = checking_base&lt;T&gt;, class Exception = exception_invalid_number&gt; struct checking_catch_nan;
  607. template &lt;class T&gt; struct checking_strict;
  608. <span style=
  609. "color: #FF0000">/* some metaprogramming to manipulate interval policies */</span>
  610. template &lt;class Rounding, class Checking&gt; struct policies;
  611. template &lt;class OldInterval, class NewRounding&gt; struct change_rounding;
  612. template &lt;class OldInterval, class NewChecking&gt; struct change_checking;
  613. template &lt;class OldInterval&gt; struct unprotect;
  614. <span style=
  615. "color: #FF0000">/* constants, need to be explicitly templated */</span>
  616. template&lt;class I&gt; I pi();
  617. template&lt;class I&gt; I pi_half();
  618. template&lt;class I&gt; I pi_twice();
  619. <span style="color: #FF0000">/* interval explicit comparison functions:
  620. * the mode can be cer=certainly or pos=possibly,
  621. * the function lt=less_than, gt=greater_than, le=less_than_or_equal_to, ge=greater_than_or_equal_to
  622. * eq=equal_to, ne= not_equal_to */</span>
  623. template &lt;class T, class Policies&gt; bool cerlt(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  624. template &lt;class T, class Policies&gt; bool cerlt(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  625. template &lt;class T, class Policies&gt; bool cerlt(const T&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  626. template &lt;class T, class Policies&gt; bool cerle(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  627. template &lt;class T, class Policies&gt; bool cerle(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  628. template &lt;class T, class Policies&gt; bool cerle(const T&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  629. template &lt;class T, class Policies&gt; bool cergt(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  630. template &lt;class T, class Policies&gt; bool cergt(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  631. template &lt;class T, class Policies&gt; bool cergt(const T&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  632. template &lt;class T, class Policies&gt; bool cerge(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  633. template &lt;class T, class Policies&gt; bool cerge(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  634. template &lt;class T, class Policies&gt; bool cerge(const T&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  635. template &lt;class T, class Policies&gt; bool cereq(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  636. template &lt;class T, class Policies&gt; bool cereq(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  637. template &lt;class T, class Policies&gt; bool cereq(const T&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  638. template &lt;class T, class Policies&gt; bool cerne(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  639. template &lt;class T, class Policies&gt; bool cerne(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  640. template &lt;class T, class Policies&gt; bool cerne(const T&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  641. template &lt;class T, class Policies&gt; bool poslt(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  642. template &lt;class T, class Policies&gt; bool poslt(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  643. template &lt;class T, class Policies&gt; bool poslt(const T&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  644. template &lt;class T, class Policies&gt; bool posle(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  645. template &lt;class T, class Policies&gt; bool posle(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  646. template &lt;class T, class Policies&gt; bool posle(const T&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  647. template &lt;class T, class Policies&gt; bool posgt(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  648. template &lt;class T, class Policies&gt; bool posgt(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  649. template &lt;class T, class Policies&gt; bool posgt(const T&amp; x, const interval&lt;T, Policies&gt; &amp; y);
  650. template &lt;class T, class Policies&gt; bool posge(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  651. template &lt;class T, class Policies&gt; bool posge(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  652. template &lt;class T, class Policies&gt; bool posge(const T&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  653. template &lt;class T, class Policies&gt; bool poseq(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  654. template &lt;class T, class Policies&gt; bool poseq(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  655. template &lt;class T, class Policies&gt; bool poseq(const T&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  656. template &lt;class T, class Policies&gt; bool posne(const interval&lt;T, Policies&gt;&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  657. template &lt;class T, class Policies&gt; bool posne(const interval&lt;T, Policies&gt;&amp; x, const T&amp; y);
  658. template &lt;class T, class Policies&gt; bool posne(const T&amp; x, const interval&lt;T, Policies&gt;&amp; y);
  659. <span style="color: #FF0000">/* comparison namespaces */</span>
  660. namespace compare {
  661. namespace certain;
  662. namespace possible;
  663. namespace lexicographic;
  664. namespace set;
  665. namespace tribool;
  666. } // namespace compare
  667. } // namespace interval_lib
  668. } // namespace numeric
  669. } // namespace boost
  670. </pre>
  671. <p>Each component of the interval support library is detailed in its own
  672. page.</p>
  673. <ul>
  674. <li><a href="comparisons.htm">Comparisons</a></li>
  675. <li><a href="rounding.htm">Rounding</a></li>
  676. <li><a href="checking.htm">Checking</a></li>
  677. </ul>
  678. <h2 id="dangers">Common Pitfalls and Dangers</h2>
  679. <h4>Comparisons</h4>
  680. <p>One of the biggest problems is probably the correct use of the
  681. comparison functions and operators. First, functions and operators do not
  682. try to know if two intervals are the same mathematical object. So, if the
  683. comparison used is "certain", then <code>x != x</code> is always true
  684. unless <code>x</code> is a singleton interval; and the same problem arises
  685. with <code>cereq</code> and <code>cerne</code>.</p>
  686. <p>Another misleading interpretation of the comparison is: you cannot
  687. always expect [a,b] &lt; [c,d] to be !([a,b] &gt;= [c,d]) since the
  688. comparison is not necessarily total. Equality and less comparison should be
  689. seen as two distincts relational operators. However the default comparison
  690. operators do respect this property since they throw an exception whenever
  691. [a,b] and [c,d] overlap.</p>
  692. <h4>Interval values and references</h4>
  693. <p>This problem is a corollary of the previous problem with <code>x !=
  694. x</code>. All the functions of the library only consider the value of an
  695. interval and not the reference of an interval. In particular, you should
  696. not expect (unless <code>x</code> is a singleton) the following values to
  697. be equal: <code>x/x</code> and 1, <code>x*x</code> and
  698. <code>square(x)</code>, <code>x-x</code> and 0, etc. So the main cause of
  699. wide intervals is that interval arithmetic does not identify different
  700. occurrences of the same variable. So, whenever possible, the user has to
  701. rewrite the formulas to eliminate multiple occurences of the same variable.
  702. For example, <code>square(x)-2*x</code> is far less precise than
  703. <code>square(x-1)-1</code>.</p>
  704. <h4>Unprotected rounding</h4>
  705. <p>As explained in <a href="rounding.htm#perf">this section</a>, a good way
  706. to speed up computations when the base type is a basic floating-point type
  707. is to unprotect the intervals at the hot spots of the algorithm. This
  708. method is safe and really an improvement for interval computations. But
  709. please remember that any basic floating-point operation executed inside the
  710. unprotection blocks will probably have an undefined behavior (but only for
  711. the current thread). And do not forget to create a rounding object as
  712. explained in the <a href="rounding.htm#perfexp">example</a>.</p>
  713. <h2 id="rationale">Rationale</h2>
  714. <p>The purpose of this library is to provide an efficient and generalized
  715. way to deal with interval arithmetic through the use of a templatized class
  716. <code>boost::numeric::interval</code>. The big contention for which we provide a
  717. rationale is the format of this class template.</p>
  718. <p>It would have been easier to provide a class interval whose base type is
  719. double. Or to follow <code>std::complex</code> and allow only
  720. specializations for <code>float</code>, <code>double</code>, and <code>long
  721. double</code>. We decided not to do this to allow intervals on custom
  722. types, e.g. fixed-precision bigfloat library types (MPFR, etc), rational
  723. numbers, and so on.</p>
  724. <p><strong>Policy design.</strong> Although it was tempting to make it a
  725. class template with only one template argument, the diversity of uses for
  726. an interval arithmetic practically forced us to use policies. The behavior
  727. of this class can be fixed by two policies. These policies are packaged
  728. into a single policy class, rather than making <code>interval</code> with
  729. three template parameters. This is both for ease of use (the policy class
  730. can be picked by default) and for readability.</p>
  731. <p>The first policy provides all the mathematical functions on the base
  732. type needed to define the functions on the interval type. The second one
  733. sets the way exceptional cases encountered during computations are
  734. handled.</p>
  735. <p>We could foresee situations where any combination of these policies
  736. would be appropriate. Moreover, we wanted to enable the user of the library
  737. to reuse the <code>interval</code> class template while at the same time
  738. choosing his own behavior. See this <a href="guide.htm">page</a> for some
  739. examples.</p>
  740. <p><strong>Rounding policy.</strong> The library provides specialized
  741. implementations of the rounding policy for the primitive types float and
  742. double. In order for these implementations to be correct and fast, the
  743. library needs to work a lot with rounding modes. Some processors are
  744. directly dealt with and some mechanisms are provided in order to speed up
  745. the computations. It seems to be heavy and hazardous optimizations for a
  746. gain of only a few computer cycles; but in reality, the speed-up factor can
  747. easily go past 2 or 3 depending on the computer. Moreover, these
  748. optimizations do not impact the interface in any major way (with the design
  749. we have chosen, everything can be added by specialization or by passing
  750. different template parameters).</p>
  751. <p><strong>Pred/succ.</strong> In a previous version, two functions
  752. <code>pred</code> and <code>succ</code>, with various corollaries like
  753. <code>widen</code>, were supplied. The intent was to enlarge the interval
  754. by one ulp (as little as possible), e.g. to ensure the inclusion property.
  755. Since making interval a template of T, we could not define <i>ulp</i> for a
  756. random parameter. In turn, rounding policies let us eliminate entirely the
  757. use of ulp while making the intervals tighter (if a result is a
  758. representable singleton, there is no use to widen the interval). We decided
  759. to drop those functions.</p>
  760. <p><strong>Specialization of <code>std::less</code>.</strong> Since the
  761. operator <code>&lt;</code> depends on the comparison namespace locally
  762. chosen by the user, it is not possible to correctly specialize
  763. <code>std::less</code>. So you have to explicitely provide such a class to
  764. all the algorithms and templates that could require it (for example,
  765. <code>std::map</code>).</p>
  766. <p><strong>Input/output.</strong> The interval library does not include I/O
  767. operators. Printing an interval value allows a lot of customization: some
  768. people may want to output the bounds, others may want to display the median
  769. and the width of intervals, and so on. The example file io.cpp shows some
  770. possibilities and may serve as a foundation in order for the user to define
  771. her own operators.</p>
  772. <p><strong>Mixed operations with integers.</strong> When using and reusing
  773. template codes, it is common there are operations like <code>2*x</code>.
  774. However, the library does not provide them by default because the
  775. conversion from <code>int</code> to the base number type is not always
  776. correct (think about the conversion from a 32bit integer to a single
  777. precision floating-point number). So the functions have been put in a
  778. separate header and the user needs to include them explicitely if she wants
  779. to benefit from these mixed operators. Another point, there is no mixed
  780. comparison operators due to the technical way they are defined.</p>
  781. <p><strong>Interval-aware functions.</strong> All the functions defined by
  782. the library are obviously aware they manipulate intervals and they do it
  783. accordingly to general interval arithmetic principles. Consequently they
  784. may have a different behavior than the one commonly encountered with
  785. functions not interval-aware. For example, <code>max</code> is defined by
  786. canonical set extension and the result is not always one of the two
  787. arguments (if the intervals do not overlap, then the result is one of the
  788. two intervals).</p>
  789. <p>This behavior is different from <code>std::max</code> which returns a
  790. reference on one of its arguments. So if the user expects a reference to be
  791. returned, she should use <code>std::max</code> since it is exactly what
  792. this function does. Please note that <code>std::max</code> will throw an
  793. exception when the intervals overlap. This behavior does not predate the
  794. one described by the C++ standard since the arguments are not "equivalent"
  795. and it allows to have an equivalence between <code>a &lt;= b</code> and
  796. <code>&amp;b == &amp;std::max(a,b)</code>(some particular cases may be
  797. implementation-defined). However it is different from the one described by
  798. SGI since it does not return the first argument even if "neither is greater
  799. than the other".</p>
  800. <h2 id="acks">History and Acknowledgments</h2>
  801. <p>This library was mostly inspired by previous work from Jens Maurer. Some
  802. discussions about his work are reproduced <a href=
  803. "http://www.mscs.mu.edu/%7Egeorgec/IFAQ/maurer1.html">here</a>. Jeremy Siek
  804. and Maarten Keijzer provided some rounding control for MSVC and Sparc
  805. platforms.</p>
  806. <p>Guillaume Melquiond, Herv&eacute; Br&ouml;nnimann and Sylvain Pion
  807. started from the library left by Jens and added the policy design.
  808. Guillaume and Sylvain worked hard on the code, especially the porting and
  809. mostly tuning of the rounding modes to the different architectures.
  810. Guillaume did most of the coding, while Sylvain and Herv&eacute; have
  811. provided some useful comments in order for this library to be written.
  812. Herv&eacute; reorganized and wrote chapters of the documentation based on
  813. Guillaume's great starting point.</p>
  814. <p>This material is partly based upon work supported by the National
  815. Science Foundation under NSF CAREER Grant CCR-0133599. Any opinions,
  816. findings and conclusions or recommendations expressed in this material are
  817. those of the author(s) and do not necessarily reflect the views of the
  818. National Science Foundation (NSF).</p>
  819. <hr>
  820. <p><a href="http://validator.w3.org/check?uri=referer"><img border="0" src=
  821. "../../../../doc/images/valid-html401.png" alt="Valid HTML 4.01 Transitional"
  822. height="31" width="88"></a></p>
  823. <p>Revised
  824. <!--webbot bot="Timestamp" s-type="EDITED" s-format="%Y-%m-%d" startspan -->2006-12-25<!--webbot bot="Timestamp" endspan i-checksum="12174" --></p>
  825. <p><i>Copyright &copy; 2002 Guillaume Melquiond, Sylvain Pion, Herv&eacute;
  826. Br&ouml;nnimann, Polytechnic University<br>
  827. Copyright &copy; 2003-2006 Guillaume Melquiond, ENS Lyon</i></p>
  828. <p><i>Distributed under the Boost Software License, Version 1.0. (See
  829. accompanying file <a href="../../../../LICENSE_1_0.txt">LICENSE_1_0.txt</a>
  830. or copy at <a href=
  831. "http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</i></p>
  832. </body>
  833. </html>