guide.htm 6.3 KB

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