<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Why use a high-precision library rather than built-in floating-point types?</title>
<link rel="stylesheet" href="../../math.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Math Toolkit 2.8.0">
<link rel="up" href="../high_precision.html" title="Using Boost.Math with High-Precision Floating-Point Libraries">
<link rel="prev" href="../high_precision.html" title="Using Boost.Math with High-Precision Floating-Point Libraries">
<link rel="next" href="use_multiprecision.html" title="Using Boost.Multiprecision">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<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>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<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
      a high-precision library rather than built-in floating-point types?</a>
</h3></div></div></div>
<p>
        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>
        <span class="keyword">double</span></code> if this offers higher precision
        than <code class="computeroutput"><span class="keyword">double</span></code>) offer enough precision,
        typically a dozen decimal digits.
      </p>
<p>
        Some reasons why one would want to use a higher precision:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            A much more precise result (many more digits) is just a requirement.
          </li>
<li class="listitem">
            The range of the computed value exceeds the range of the type: factorials
            are the textbook example.
          </li>
<li class="listitem">
            Using <code class="computeroutput"><span class="keyword">double</span></code> is (or may
            be) too inaccurate.
          </li>
<li class="listitem">
            Using <code class="computeroutput"><span class="keyword">long</span> <span class="keyword">double</span></code>
            (or may be) is too inaccurate.
          </li>
<li class="listitem">
            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>
            (<a href="http://en.wikipedia.org/wiki/Darwin_(operating_system)" target="_top">Darwin</a>)
            is sometimes unpredictably inaccurate.
          </li>
<li class="listitem">
            Loss of precision or inaccuracy caused by extreme arguments or cancellation
            error.
          </li>
<li class="listitem">
            An accuracy as good as possible for a chosen built-in floating-point
            type is required.
          </li>
<li class="listitem">
            As a reference value, for example, to determine the inaccuracy of a value
            computed with a built-in floating point type, (perhaps even using some
            quick'n'dirty algorithm). The accuracy of many functions and distributions
            in Boost.Math has been measured in this way from tables of very high
            precision (up to 1000 decimal digits).
          </li>
</ul></div>
<p>
        Many functions and distributions have differences from exact values that
        are only a few least significant bits - computation noise. Others, often
        those for which analytical solutions are not available, require approximations
        and iteration: these may lose several decimal digits of precision.
      </p>
<p>
        Much larger loss of precision can occur for <a href="http://en.wikipedia.org/wiki/Boundary_case" target="_top">boundary</a>
        or <a href="http://en.wikipedia.org/wiki/Corner_case" target="_top">corner cases</a>,
        often caused by <a href="http://en.wikipedia.org/wiki/Loss_of_significance" target="_top">cancellation
        errors</a>.
      </p>
<p>
        (Some of the worst and most common examples of <a href="http://en.wikipedia.org/wiki/Loss_of_significance" target="_top">cancellation
        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>:
        see <a class="link" href="../stat_tut/overview/complements.html#why_complements">why complements?</a>).
      </p>
<p>
        If you require a value which is as accurate as can be represented in the
        floating-point type, and is thus the closest representable value and has
        an error less than 1/2 a <a href="http://en.wikipedia.org/wiki/Least_significant_bit" target="_top">least
        significant bit</a> or <a href="http://en.wikipedia.org/wiki/Unit_in_the_last_place" target="_top">ulp</a>
        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.
        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>
        <span class="keyword">double</span></code>) will not cause any further
        loss of precision. A decimal digit string will also be 'read' precisely by
        the compiler into a built-in floating-point type to the nearest representable
        value.
      </p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
          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>
          into a built-in floating-point type is <span class="bold"><strong>not guaranteed
          by the C++ Standard</strong></span> to give the nearest representable value.
        </p></td></tr>
</table></div>
<p>
        William Kahan coined the term <a href="http://en.wikipedia.org/wiki/Rounding#The_table-maker.27s_dilemma" target="_top">Table-Maker's
        Dilemma</a> for the problem of correctly rounding functions. Using a
        much higher precision (50 or 100 decimal digits) is a practical way of generating
        (almost always) correctly rounded values.
      </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2006-2010, 2012-2014, 2017 Nikhar
      Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos,
      Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Johan R&#229;de, Gautam
      Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, Daryle Walker
      and Xiaogang Zhang<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        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>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<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>
</div>
</body>
</html>
