3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
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.78.1">
7 <link rel="home" href="../../index.html" title="Math Toolkit 2.1.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">
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>
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>
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>
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.
37 Some reasons why one would want to use a higher precision:
39 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
41 A much more precise result (many more digits) is just a requirement.
44 The range of the computed value exceeds the range of the type: factorials
45 are the textbook example.
48 Using double is (or may be) too inaccurate.
51 Using long double (or may be) is too inaccurate.
54 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>
55 (<a href="http://en.wikipedia.org/wiki/Darwin_(operating_system)" target="_top">Darwin</a>)
56 is sometimes unpredictably inaccurate.
59 Loss of precision or inaccuracy caused by extreme arguments or cancellation
63 An accuracy as good as possible for a chosen built-in floating-point
67 As a reference value, for example, to determine the inaccuracy of a value
68 computed with a built-in floating point type, (perhaps even using some
69 quick'n'dirty algorithm). The accuracy of many functions and distributions
70 in Boost.Math has been measured in this way from tables of very high
71 precision (up to 1000 decimal digits).
75 Many functions and distributions have differences from exact values that
76 are only a few least significant bits - computation noise. Others, often
77 those for which analytical solutions are not available, require approximations
78 and iteration: these may lose several decimal digits of precision.
81 Much larger loss of precision can occur for <a href="http://en.wikipedia.org/wiki/Boundary_case" target="_top">boundary</a>
82 or <a href="http://en.wikipedia.org/wiki/Corner_case" target="_top">corner cases</a>,
83 often caused by <a href="http://en.wikipedia.org/wiki/Loss_of_significance" target="_top">cancellation
87 (Some of the worst and most common examples of <a href="http://en.wikipedia.org/wiki/Loss_of_significance" target="_top">cancellation
88 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>:
89 see <a class="link" href="../stat_tut/overview/complements.html#why_complements">why complements?</a>).
92 If you require a value which is as accurate as can be represented in the
93 floating-point type, and is thus the closest representable value and has
94 an error less than 1/2 a <a href="http://en.wikipedia.org/wiki/Least_significant_bit" target="_top">least
95 significant bit</a> or <a href="http://en.wikipedia.org/wiki/Unit_in_the_last_place" target="_top">ulp</a>
96 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.
97 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>
98 <span class="keyword">double</span></code>) will not cause any further
99 loss of precision. A decimal digit string will also be 'read' precisely by
100 the compiler into a built-in floating-point type to the nearest representable
103 <div class="note"><table border="0" summary="Note">
105 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td>
106 <th align="left">Note</th>
108 <tr><td align="left" valign="top"><p>
109 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>
110 into a built-in floating-point type is <span class="bold"><strong>not guaranteed</strong></span>
111 by the C++ Standard to give the nearest representable value.
115 William Kahan coined the term <a href="http://en.wikipedia.org/wiki/Rounding#The_table-maker.27s_dilemma" target="_top">Table-Maker's
116 Dilemma</a> for the problem of correctly rounding functions. Using a
117 much higher precision (50 or 100 decimal digits) is a practical way of generating
118 (almost always) correctly rounded values.
121 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
122 <td align="left"></td>
123 <td align="right"><div class="copyright-footer">Copyright © 2006-2010, 2012-2014 Nikhar Agrawal,
124 Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert
125 Holin, Bruno Lalande, John Maddock, Johan Råde, Gautam Sewani, Benjamin Sobotta,
126 Thijs van den Berg, Daryle Walker and Xiaogang Zhang<p>
127 Distributed under the Boost Software License, Version 1.0. (See accompanying
128 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 <div class="spirit-nav">
134 <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>