• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Why use a high-precision library rather than built-in floating-point types?</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.12.0">
8<link rel="up" href="../high_precision.html" title="Using Boost.Math with High-Precision Floating-Point Libraries">
9<link rel="prev" href="../high_precision.html" title="Using Boost.Math with High-Precision Floating-Point Libraries">
10<link rel="next" href="use_multiprecision.html" title="Using Boost.Multiprecision">
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="../high_precision.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../high_precision.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="use_multiprecision.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h3 class="title">
27<a name="math_toolkit.high_precision.why_high_precision"></a><a class="link" href="why_high_precision.html" title="Why use a high-precision library rather than built-in floating-point types?">Why use
28      a high-precision library rather than built-in floating-point types?</a>
29</h3></div></div></div>
30<p>
31        For nearly all applications, the built-in floating-point types, <code class="computeroutput"><span class="keyword">double</span></code> (and <code class="computeroutput"><span class="keyword">long</span>
32        <span class="keyword">double</span></code> if this offers higher precision
33        than <code class="computeroutput"><span class="keyword">double</span></code>) offer enough precision,
34        typically a dozen decimal digits.
35      </p>
36<p>
37        Some reasons why one would want to use a higher precision:
38      </p>
39<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
40<li class="listitem">
41            A much more precise result (many more digits) is just a requirement.
42          </li>
43<li class="listitem">
44            The range of the computed value exceeds the range of the type: factorials
45            are the textbook example.
46          </li>
47<li class="listitem">
48            Using <code class="computeroutput"><span class="keyword">double</span></code> is (or may
49            be) too inaccurate.
50          </li>
51<li class="listitem">
52            Using <code class="computeroutput"><span class="keyword">long</span> <span class="keyword">double</span></code>
53            (or may be) is too inaccurate.
54          </li>
55<li class="listitem">
56            Using an extended-precision type implemented in software as <a href="http://en.wikipedia.org/wiki/Double-double_(arithmetic)#Double-double_arithmetic" target="_top">double-double</a>
57            (<a href="http://en.wikipedia.org/wiki/Darwin_(operating_system)" target="_top">Darwin</a>)
58            is sometimes unpredictably inaccurate.
59          </li>
60<li class="listitem">
61            Loss of precision or inaccuracy caused by extreme arguments or <a href="http://en.wikipedia.org/wiki/Loss_of_significance" target="_top">cancellation
62            errors</a>.
63          </li>
64<li class="listitem">
65            An accuracy as good as possible for a chosen built-in floating-point
66            type is required.
67          </li>
68<li class="listitem">
69            As a reference value, for example, to determine the inaccuracy of a value
70            computed with a built-in floating point type, (perhaps even using some
71            quick'n'dirty algorithm). The accuracy of many functions and distributions
72            in Boost.Math has been measured in this way from tables of very high
73            precision (up to 1000 decimal digits).
74          </li>
75</ul></div>
76<p>
77        Many functions and distributions have differences from exact values that
78        are only a few least significant bits - computation noise. Others, often
79        those for which analytical solutions are not available, require approximations
80        and iteration: these may lose several decimal digits of precision.
81      </p>
82<p>
83        Much larger loss of precision can occur for <a href="http://en.wikipedia.org/wiki/Boundary_case" target="_top">boundary</a>
84        or <a href="http://en.wikipedia.org/wiki/Corner_case" target="_top">corner cases</a>,
85        often caused by <a href="http://en.wikipedia.org/wiki/Loss_of_significance" target="_top">cancellation
86        errors</a>.
87      </p>
88<p>
89        (Some of the worst and most common examples of <a href="http://en.wikipedia.org/wiki/Loss_of_significance" target="_top">cancellation
90        error or loss of significance</a> can be avoided by using <a class="link" href="../stat_tut/overview/complements.html" title="Complements are supported too - and when to use them">complements</a>:
91        see <a class="link" href="../stat_tut/overview/complements.html#why_complements">why complements?</a>).
92      </p>
93<p>
94        If you require a value which is as accurate as can be represented in the
95        floating-point type, and is thus the <a href="https://en.wikipedia.org/wiki/Floating-point_arithmetic#Representable_numbers%2c_conversion_and_rounding" target="_top">closest
96        representable value</a> correctly rounded to nearest, and has an error
97        less than 1/2 a <a href="http://en.wikipedia.org/wiki/Least_significant_bit" target="_top">least
98        significant bit</a> or <a href="http://en.wikipedia.org/wiki/Unit_in_the_last_place" target="_top">ulp</a>
99        it may be useful to use a higher-precision type, for example, <code class="computeroutput"><span class="identifier">cpp_dec_float_50</span></code>, to generate this value.
100        Conversion of this value to a built-in floating-point type ('float', <code class="computeroutput"><span class="keyword">double</span></code> or <code class="computeroutput"><span class="keyword">long</span>
101        <span class="keyword">double</span></code>) will not cause any further
102        loss of precision. A decimal digit string will also be 'read' precisely by
103        the compiler into a built-in floating-point type to the nearest representable
104        value.
105      </p>
106<div class="note"><table border="0" summary="Note">
107<tr>
108<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td>
109<th align="left">Note</th>
110</tr>
111<tr><td align="left" valign="top"><p>
112          In contrast, reading a value from an <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">istream</span></code>
113          into a built-in floating-point type is <span class="bold"><strong>not guaranteed
114          by the C++ Standard</strong></span> to give the nearest representable value.
115        </p></td></tr>
116</table></div>
117<p>
118        William Kahan coined the term <a href="http://en.wikipedia.org/wiki/Rounding#The_table-maker.27s_dilemma" target="_top">Table-Maker's
119        Dilemma</a> for the problem of correctly rounding functions. Using a
120        much higher precision (50 or 100 decimal digits) is a practical way of generating
121        (almost always) correctly rounded values.
122      </p>
123</div>
124<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
125<td align="left"></td>
126<td align="right"><div class="copyright-footer">Copyright © 2006-2019 Nikhar
127      Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos,
128      Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan
129      Råde, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg,
130      Daryle Walker and Xiaogang Zhang<p>
131        Distributed under the Boost Software License, Version 1.0. (See accompanying
132        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>)
133      </p>
134</div></td>
135</tr></table>
136<hr>
137<div class="spirit-nav">
138<a accesskey="p" href="../high_precision.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../high_precision.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="use_multiprecision.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
139</div>
140</body>
141</html>
142