relative_error.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Relative Error</title>
  5. <link rel="stylesheet" href="../math.css" type="text/css">
  6. <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
  7. <link rel="home" href="../index.html" title="Math Toolkit 2.11.0">
  8. <link rel="up" href="../backgrounders.html" title="Chapter&#160;22.&#160;Backgrounders">
  9. <link rel="prev" href="special_tut/special_tut_test.html" title="Testing">
  10. <link rel="next" href="lanczos.html" title="The Lanczos Approximation">
  11. </head>
  12. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  13. <table cellpadding="2" width="100%"><tr>
  14. <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
  15. <td align="center"><a href="../../../../../index.html">Home</a></td>
  16. <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
  17. <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
  18. <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
  19. <td align="center"><a href="../../../../../more/index.htm">More</a></td>
  20. </tr></table>
  21. <hr>
  22. <div class="spirit-nav">
  23. <a accesskey="p" href="special_tut/special_tut_test.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../backgrounders.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="lanczos.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
  24. </div>
  25. <div class="section">
  26. <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  27. <a name="math_toolkit.relative_error"></a><a class="link" href="relative_error.html" title="Relative Error">Relative Error</a>
  28. </h2></div></div></div>
  29. <p>
  30. Given an actual value <span class="emphasis"><em>a</em></span> and a found value <span class="emphasis"><em>v</em></span>
  31. the relative error can be calculated from:
  32. </p>
  33. <div class="blockquote"><blockquote class="blockquote"><p>
  34. <span class="inlinemediaobject"><img src="../../equations/error2.svg"></span>
  35. </p></blockquote></div>
  36. <p>
  37. However the test programs in the library use the symmetrical form:
  38. </p>
  39. <div class="blockquote"><blockquote class="blockquote"><p>
  40. <span class="inlinemediaobject"><img src="../../equations/error1.svg"></span>
  41. </p></blockquote></div>
  42. <p>
  43. which measures <span class="emphasis"><em>relative difference</em></span> and happens to be less
  44. error prone in use since we don't have to worry which value is the "true"
  45. result, and which is the experimental one. It guarantees to return a value
  46. at least as large as the relative error.
  47. </p>
  48. <p>
  49. Special care needs to be taken when one value is zero: we could either take
  50. the absolute error in this case (but that's cheating as the absolute error
  51. is likely to be very small), or we could assign a value of either 1 or infinity
  52. to the relative error in this special case. In the test cases for the special
  53. functions in this library, everything below a threshold is regarded as "effectively
  54. zero", otherwise the relative error is assigned the value of 1 if only
  55. one of the terms is zero. The threshold is currently set at <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;&gt;::</span><span class="identifier">min</span><span class="special">()</span></code>: in other words all denormalised numbers
  56. are regarded as a zero.
  57. </p>
  58. <p>
  59. All the test programs calculate <span class="emphasis"><em>quantized relative error</em></span>,
  60. whereas the graphs in this manual are produced with the <span class="emphasis"><em>actual error</em></span>.
  61. The difference is as follows: in the test programs, the test data is rounded
  62. to the target real type under test when the program is compiled, so the error
  63. observed will then be a whole number of <span class="emphasis"><em>units in the last place</em></span>
  64. either rounded up from the actual error, or rounded down (possibly to zero).
  65. In contrast the <span class="emphasis"><em>true error</em></span> is obtained by extending the
  66. precision of the calculated value, and then comparing to the actual value:
  67. in this case the calculated error may be some fraction of <span class="emphasis"><em>units in
  68. the last place</em></span>.
  69. </p>
  70. <p>
  71. Note that throughout this manual and the test programs the relative error is
  72. usually quoted in units of epsilon. However, remember that <span class="emphasis"><em>units
  73. in the last place</em></span> more accurately reflect the number of contaminated
  74. digits, and that relative error can <span class="emphasis"><em>"wobble"</em></span>
  75. by a factor of 2 compared to <span class="emphasis"><em>units in the last place</em></span>.
  76. In other words: two implementations of the same function, whose maximum relative
  77. errors differ by a factor of 2, can actually be accurate to the same number
  78. of binary digits. You have been warned!
  79. </p>
  80. <h5>
  81. <a name="math_toolkit.relative_error.h0"></a>
  82. <span class="phrase"><a name="math_toolkit.relative_error.zero_error"></a></span><a class="link" href="relative_error.html#math_toolkit.relative_error.zero_error">The
  83. Impossibility of Zero Error</a>
  84. </h5>
  85. <p>
  86. For many of the functions in this library, it is assumed that the error is
  87. "effectively zero" if the computation can be done with a number of
  88. guard digits. However it should be remembered that if the result is a <span class="emphasis"><em>transcendental
  89. number</em></span> then as a point of principle we can never be sure that the
  90. result is accurate to more than 1 ulp. This is an example of what <a href="http://en.wikipedia.org/wiki/William_Kahan" target="_top">http://en.wikipedia.org/wiki/William_Kahan</a>
  91. called <a href="http://en.wikipedia.org/wiki/Rounding#The_table-maker.27s_dilemma" target="_top">http://en.wikipedia.org/wiki/Rounding#The_table-maker.27s_dilemma</a>:
  92. consider what happens if the first guard digit is a one, and the remaining
  93. guard digits are all zero. Do we have a tie or not? Since the only thing we
  94. can tell about a transcendental number is that its digits have no particular
  95. pattern, we can never tell if we have a tie, no matter how many guard digits
  96. we have. Therefore, we can never be completely sure that the result has been
  97. rounded in the right direction. Of course, transcendental numbers that just
  98. happen to be a tie - for however many guard digits we have - are extremely
  99. rare, and get rarer the more guard digits we have, but even so....
  100. </p>
  101. <p>
  102. Refer to the classic text <a href="http://docs.sun.com/source/806-3568/ncg_goldberg.html" target="_top">What
  103. Every Computer Scientist Should Know About Floating-Point Arithmetic</a>
  104. for more information.
  105. </p>
  106. </div>
  107. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  108. <td align="left"></td>
  109. <td align="right"><div class="copyright-footer">Copyright &#169; 2006-2019 Nikhar
  110. Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos,
  111. Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan
  112. R&#229;de, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg,
  113. Daryle Walker and Xiaogang Zhang<p>
  114. Distributed under the Boost Software License, Version 1.0. (See accompanying
  115. file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
  116. </p>
  117. </div></td>
  118. </tr></table>
  119. <hr>
  120. <div class="spirit-nav">
  121. <a accesskey="p" href="special_tut/special_tut_test.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../backgrounders.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="lanczos.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
  122. </div>
  123. </body>
  124. </html>