numbers.htm 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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>Numbers Requirements</title>
  9. </head>
  10. <body lang="en">
  11. <h1>Numbers Requirements</h1>
  12. <p>What we call "number" is the base type of the <code>interval</code>
  13. class. The interval library expect a lot of properties from this base type
  14. in order to respect the inclusion property. All these properties are
  15. already detailed in the other sections of this documentation; but we will
  16. try to summarize them here.</p>
  17. <h3>Ordering</h3>
  18. <p>The numbers need to be supplied with an ordering. This ordering
  19. expresses itself by the operators <code>&lt; &lt;= =&gt; &gt; == !=</code>.
  20. It must be a total order (reflexivity, antisymmetry, transitivity, and each
  21. pair of numbers is ordered). So <code>complex&lt;T&gt;</code> will not be a
  22. good candidate for the base type; if you need the inclusion property of
  23. interval property, you should use <code>complex&lt; interval&lt;T&gt;
  24. &gt;</code> in place of <code>interval&lt; complex&lt;T&gt; &gt;</code>
  25. (but unfortunately, <code>complex</code> only provides specialization).</p>
  26. <p>Please note that invalid numbers are not concerned by the order; it can
  27. even be conceptually better if a comparison with these invalid numbers is
  28. always <code>false</code> (except for <code>!=</code>). If your checking
  29. policy uses <code>interval_lib::checking_base</code> and your base type
  30. contains invalid numbers, then this property is needed:
  31. <code>nan!=nan</code> (here <code>nan</code> is an invalid number). If this
  32. property is not present, then you should not use <code>checking_base</code>
  33. directly.</p>
  34. <p>Interval arithmetic involves a lot of comparison to zero. By default,
  35. they are done by comparing the numbers to
  36. <code>static_cast&lt;T&gt;(0)</code>. However, if the format of the numbers
  37. allows some faster comparisons when dealing with zero, the template
  38. functions in the <code>interval_lib::user</code> namespace can be
  39. specialized:</p>
  40. <pre>
  41. namespace user {
  42. template&lt;class T&gt; inline bool is_zero(T const &amp;v) { return v == static_cast&lt;T&gt;(0); }
  43. template&lt;class T&gt; inline bool is_neg (T const &amp;v) { return v &lt; static_cast&lt;T&gt;(0); }
  44. template&lt;class T&gt; inline bool is_pos (T const &amp;v) { return v &gt; static_cast&lt;T&gt;(0); }
  45. }
  46. </pre>
  47. <h3>Numeric limits</h3>
  48. <p>Another remark about the checking policy. It normally is powerful enough
  49. to handle the exceptional behavior that the basic type could induce; in
  50. particular infinite and invalid numbers (thanks to the four functions
  51. <code>pos_inf</code>, <code>neg_inf</code>, <code>nan</code> and
  52. <code>is_nan</code>). However, if you use
  53. <code>interval_lib::checking_base</code> (and the default checking policy
  54. uses it), your base type should have a correctly specialized
  55. <code>std::numeric_limits&lt;T&gt;</code>. In particular, the values
  56. <code>has_infinity</code> and <code>has_quiet_NaN</code>, and the functions
  57. <code>infinity</code> and <code>quiet_NaN</code> should be accordingly
  58. defined.</p>
  59. <p>So, to summarize, if you do not rely on the default policy and do not
  60. use <code>interval_lib::checking_base</code>, it is not necessary to have a
  61. specialization of the numeric limits for your base type.</p>
  62. <h3>Mathematical properties</h3>
  63. <p>Ensuring the numbers are correctly ordered is not enough. The basic
  64. operators should also respect some properties depending on the order. Here
  65. they are:</p>
  66. <ul>
  67. <li>0 &le; <i>x</i> &rArr; -<i>x</i> &le; 0</li>
  68. <li><i>x</i> &le; <i>y</i> &rArr; -<i>y</i> &le; -<i>x</i></li>
  69. <li><i>x</i> &le; <i>y</i> &rArr; <i>x</i>+<i>z</i> &le;
  70. <i>y</i>+<i>z</i></li>
  71. <li><i>x</i> &le; <i>y</i> and <i>z</i> &ge; 0 &rArr;
  72. <i>x</i>&times;<i>z</i> &le; <i>y</i>&times;<i>z</i></li>
  73. <li>0 &lt; <i>x</i> &le; <i>y</i> &rArr; 0 &lt; 1/<i>y</i> &le;
  74. 1/<i>x</i></li>
  75. </ul>
  76. <p>The previous properties are also used (and enough) for <code>abs</code>,
  77. <code>square</code> and <code>pow</code>. For all the transcendental
  78. functions (including <code>sqrt</code>), other properties are needed. These
  79. functions should have the same properties than the corresponding real
  80. functions. For example, the expected properties for <code>cos</code>
  81. are:</p>
  82. <ul>
  83. <li><code>cos</code> is defined for all the valid numbers;</li>
  84. <li>it is 2&pi;-periodic;</li>
  85. <li><code>cos</code>(2&pi;-<i>x</i>) is equal to
  86. <code>cos</code>(<i>x</i>);</li>
  87. <li><code>cos</code> is a decreasing function on [0,2&pi;].</li>
  88. </ul>
  89. <h3>Rounding</h3>
  90. <p>If you work with a base type and no inexact result is ever computed, you
  91. can skip the rest of this paragraph. You can also skip it if you are not
  92. interested in the inclusion property (if approximate results are enough).
  93. If you are still reading, it is probably because you want to know the basic
  94. properties the rounding policy should validate.</p>
  95. <p>Whichever operation or function you consider, the following property
  96. should be respected: <code>f_down(x,y) &lt;= f(x,y) &lt;= f_up(x,y)</code>.
  97. Here, <code>f</code> denotes the infinitely precise function computed and
  98. <code>f_down</code> and <code>f_up</code> are functions which return
  99. possibly inexact values but of the correct type (the base type). If
  100. possible, they should try to return the nearest representable value, but it
  101. is not always easy.</p>
  102. <h3>Constants</h3>
  103. <p>In order for the trigonometric functions to correctly work, the library
  104. need to know the value of the &pi; constant (and also &pi;/2 and 2&pi;).
  105. Since these constants may not be representable in the base type, the
  106. library does not have to know the exact value: a lower bound and an upper
  107. bound are enough. If these values are not provided by the user, the default
  108. values will be used: they are integer values (so &pi; is bounded by 3 and
  109. 4).</p>
  110. <h3>Operators and conversions</h3>
  111. <p>As explained at the beginning, the comparison operators should be
  112. defined for the base type. The rounding policy defines a lot of functions
  113. used by the interval library. So the arithmetic operators do not need to be
  114. defined for the base type (unless required by one of the predefined
  115. classes). However, there is an exception: the unary minus need to be
  116. defined. Moreover, this operator should only provide exact results; it is
  117. the reason why the rounding policy does not provide some negation
  118. functions.</p>
  119. <p>The conversion from <code>int</code> to the base type needs to be
  120. defined (only a few values need to be available: -1, 0, 1, 2). The
  121. conversion the other way around is provided by the rounding policy
  122. (<code>int_down</code> and <code>int_up</code> members); and no other
  123. conversion is strictly needed. However, it may be valuable to provide as
  124. much conversions as possible in the rounding policy (<code>conv_down</code>
  125. and <code>conv_up</code> members) in order to benefit from interval
  126. conversions.</p>
  127. <hr>
  128. <p><a href="http://validator.w3.org/check?uri=referer"><img border="0" src=
  129. "../../../../doc/images/valid-html401.png" alt="Valid HTML 4.01 Transitional"
  130. height="31" width="88"></a></p>
  131. <p>Revised
  132. <!--webbot bot="Timestamp" s-type="EDITED" s-format="%Y-%m-%d" startspan -->2006-12-24<!--webbot bot="Timestamp" endspan i-checksum="12172" --></p>
  133. <p><i>Copyright &copy; 2002 Guillaume Melquiond, Sylvain Pion, Herv&eacute;
  134. Br&ouml;nnimann, Polytechnic University<br>
  135. Copyright &copy; 2004 Guillaume Melquiond</i></p>
  136. <p><i>Distributed under the Boost Software License, Version 1.0. (See
  137. accompanying file <a href="../../../../LICENSE_1_0.txt">LICENSE_1_0.txt</a>
  138. or copy at <a href=
  139. "http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</i></p>
  140. </body>
  141. </html>