Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/numeric/interval/doc/guide.htm @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 5.5 KB
Line 
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-Type" content="text/html; charset=iso-8859-1">
6  <link rel="stylesheet" type="text/css" href="../../../../boost.css">
7  <title>Choosing Your Own Interval Type</title>
8</head>
9
10<body lang="en">
11<h1>Choosing Your Own Interval Type</h1>
12
13<p>First of all, you need to select your base type. In order to obtain an
14useful interval type, the numbers should respect some requirements. Please
15refer to <a href="numbers.htm">this page</a> in order to see them. When your
16base type is robust enough, you can go to the next step: the choice of the
17policies.</p>
18
19<p>As you should already know if you did not come to this page by accident,
20the <code>interval</code> class expect a policies argument describing the <a
21href="rounding.htm">rounding</a> and <a href="checking.htm">checking</a>
22policies. The first thing to do is to verify if the default policies are or
23are not adapted to your case. If your base type is not <code>float</code>,
24<code>double</code>, or <code>long double</code>, the default rounding policy
25is probably not adapted. However, by specializing
26<code>interval_lib::rounded_math</code> to your base type, the default
27rounding policy will be suitable.</p>
28
29<p>The default policies define an interval type that performs precise
30computations (for <code>float</code>, <code>double</code>, <code>long
31double</code>), detects invalid numbers and throws exception each times an
32empty interval is created. This is a brief description and you should refer
33to the corresponding sections for a more precise description of the default
34policies. Unless you need some special behavior, this default type is usable
35in a lot of situations.</p>
36
37<p>After having completely defined the interval type (and its policies), the
38only thing left to do is to verify that the constants are defined and
39<code>std::numeric_limits</code> is correct (if needed). Now you can use your
40brand new interval type.</p>
41
42<h2>Some Examples</h2>
43
44<h3>Solving systems</h3>
45
46<p>If you use the interval library in order to solve equation and inequation
47systems by bisection, something like
48<code>boost::interval&lt;double&gt;</code> is probably what you need. The
49computations are precise, and they may be fast if enclosed in a protected
50rounding mode block (see the <a href="rounding.htm#perf">performance</a>
51section). The comparison are "certain"; it is probably the most used type of
52comparison, and the other comparisons are still accessible by the explicit
53comparison functions. The checking forbid empty interval; they are not needed
54since there would be an empty interval at end of the computation if an empty
55interval is created during the computation, and no root would be inside. The
56checking also forbid invalid numbers (NaN for floating-point numbers). It can
57be a minor performance hit if you only use exact floating-point constants
58(which are clearly not NaNs); however, if performance really does matter, you
59will probably use a good compiler which knows how to inline functions and all
60these annoying little tests will magically disappear (if not, it is time to
61upgrade your compiler).</p>
62
63<h3>Manipulating wide intervals</h3>
64
65<p>You may want to use the library on intervals with imprecise bounds or on
66inexact numbers. In particular, it may be an existing algorithm that you want
67to rewrite and simplify by using the library. In that case, you are not
68really interested by the inclusion property; you are only interested by the
69computation algorithms the library provides. So you do not need to use any
70rounding; the checking also may not be useful. Use an "exact computation"
71rounding (you are allowed to think the name stangely applies to the
72situation) and a checking that never tests for any invalid numbers or empty
73intervals. By doing that, you will obtain library functions reduced to their
74minimum (an addition of two intervals will only be two additions of
75numbers).</p>
76
77<h3>Computing ranges</h3>
78
79<p>The inputs of your program may be empty intervals or invalid values (for
80example, a database can allow undefined values in some field) and the core of
81your program could also do some non-arithmetic computations that do not
82always propagate empty intervals. For example, in the library, the
83<code>hull</code> function can happily receive an empty interval but not
84generate an empty interval if the other input is valid. The
85<code>intersect</code> function is also able to produce empty intervals if
86the intervals do not overlap. In that case, it is not really interesting if
87an exception is thrown each time an empty interval is produced or an invalid
88value is used; it would be better to generate and propagate empty intervals.
89So you need to change the checking policy to something like
90<code>interval_lib::checking_base&lt;T&gt;</code>.</p>
91
92<h3>Switching interval types</h3>
93
94<p>This example does not deal with a full case, but with a situation that can
95occur often. Sometimes, it can be useful to change the policies of an
96interval by converting it to another type. For example, this happens when you
97use an unprotected version of the interval type in order to speed up the
98computations; it is a change of the rounding policy. It also happens when you
99want to temporarily allow empty intervals to be created; it is a change of
100the checking policy. These changes should not be prohibited: they can greatly
101enhance a program (lisibility, interest, performance).</p>
102<hr>
103
104<p>Revised: 2003-01-15<br>
105Copyright (c) Guillaume Melquiond, Sylvain Pion, Hervé Brönnimann, 2002.<br>
106Polytechnic University.</p>
107</body>
108</html>
Note: See TracBrowser for help on using the repository browser.