72e469da0a
[CI SKIP]
369 lines
37 KiB
HTML
369 lines
37 KiB
HTML
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
|
|
<title>Using Boost.Multiprecision</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.11.0">
|
|
<link rel="up" href="../high_precision.html" title="Using Boost.Math with High-Precision Floating-Point Libraries">
|
|
<link rel="prev" href="why_high_precision.html" title="Why use a high-precision library rather than built-in floating-point types?">
|
|
<link rel="next" href="float128.html" title="Using with GCC's __float128 datatype">
|
|
</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="why_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="float128.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.use_multiprecision"></a><a class="link" href="use_multiprecision.html" title="Using Boost.Multiprecision">Using
|
|
Boost.Multiprecision</a>
|
|
</h3></div></div></div>
|
|
<p>
|
|
<span class="bold"><strong>All new projects are recommended to use <a href="../../../../../../libs/multiprecision/doc/html/index.html" target="_top">Boost.Multiprecision</a>.</strong></span>
|
|
</p>
|
|
<h6>
|
|
<a name="math_toolkit.high_precision.use_multiprecision.h0"></a>
|
|
<span class="phrase"><a name="math_toolkit.high_precision.use_multiprecision.using_boost_multiprecision_cpp_f"></a></span><a class="link" href="use_multiprecision.html#math_toolkit.high_precision.use_multiprecision.using_boost_multiprecision_cpp_f">Using
|
|
Boost.Multiprecision <code class="computeroutput"><span class="identifier">cpp_float</span></code>
|
|
for numerical calculations with high precision.</a>
|
|
</h6>
|
|
<p>
|
|
The Boost.Multiprecision library can be used for computations requiring precision
|
|
exceeding that of standard built-in types such as float, double and long
|
|
double. For extended-precision calculations, Boost.Multiprecision supplies
|
|
a template data type called cpp_dec_float. The number of decimal digits of
|
|
precision is fixed at compile-time via template parameter.
|
|
</p>
|
|
<p>
|
|
To use these floating-point types and constants, we need some includes:
|
|
</p>
|
|
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">constants</span><span class="special">/</span><span class="identifier">constants</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
|
|
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multiprecision</span><span class="special">/</span><span class="identifier">cpp_dec_float</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
|
<span class="comment">// using boost::multiprecision::cpp_dec_float</span>
|
|
|
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span>
|
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">limits</span><span class="special">></span>
|
|
</pre>
|
|
<p>
|
|
So now we can demonstrate with some trivial calculations:
|
|
</p>
|
|
<p>
|
|
Using <code class="computeroutput"><span class="keyword">typedef</span> <span class="identifier">cpp_dec_float_50</span></code>
|
|
hides the complexity of multiprecision, allows us to define variables with
|
|
50 decimal digit precision just like built-in <code class="computeroutput"><span class="keyword">double</span></code>.
|
|
</p>
|
|
<pre class="programlisting"><span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">cpp_dec_float_50</span><span class="special">;</span>
|
|
|
|
<span class="identifier">cpp_dec_float_50</span> <span class="identifier">seventh</span> <span class="special">=</span> <span class="identifier">cpp_dec_float_50</span><span class="special">(</span><span class="number">1</span><span class="special">)</span> <span class="special">/</span> <span class="number">7</span><span class="special">;</span> <span class="comment">// 1 / 7</span>
|
|
</pre>
|
|
<p>
|
|
By default, output would only show the standard 6 decimal digits, so set
|
|
precision to show all 50 significant digits, including any trailing zeros.
|
|
</p>
|
|
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span><span class="special">.</span><span class="identifier">precision</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">cpp_dec_float_50</span><span class="special">>::</span><span class="identifier">digits10</span><span class="special">);</span>
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">showpoint</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="comment">// Append any trailing zeros.</span>
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">seventh</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
|
|
</pre>
|
|
<p>
|
|
which outputs:
|
|
</p>
|
|
<pre class="programlisting"><span class="number">0.14285714285714285714285714285714285714285714285714</span>
|
|
</pre>
|
|
<p>
|
|
We can also use Boost.Math <a class="link" href="../constants.html" title="The Mathematical Constants">constants</a>
|
|
like π, guaranteed to be initialized with the very last bit of precision for
|
|
the floating-point type.
|
|
</p>
|
|
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"pi = "</span> <span class="special"><<</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">constants</span><span class="special">::</span><span class="identifier">pi</span><span class="special"><</span><span class="identifier">cpp_dec_float_50</span><span class="special">>()</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
|
|
<span class="identifier">cpp_dec_float_50</span> <span class="identifier">circumference</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">constants</span><span class="special">::</span><span class="identifier">pi</span><span class="special"><</span><span class="identifier">cpp_dec_float_50</span><span class="special">>()</span> <span class="special">*</span> <span class="number">2</span> <span class="special">*</span> <span class="identifier">seventh</span><span class="special">;</span>
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"c = "</span><span class="special"><<</span> <span class="identifier">circumference</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
|
|
</pre>
|
|
<p>
|
|
which outputs
|
|
</p>
|
|
<pre class="programlisting"><span class="identifier">pi</span> <span class="special">=</span> <span class="number">3.1415926535897932384626433832795028841971693993751</span>
|
|
|
|
<span class="identifier">c</span> <span class="special">=</span> <span class="number">0.89759790102565521098932668093700082405633411410717</span>
|
|
</pre>
|
|
<p>
|
|
The full source of this example is at <a href="../../../../example/big_seventh.cpp" target="_top">big_seventh.cpp</a>
|
|
</p>
|
|
<h6>
|
|
<a name="math_toolkit.high_precision.use_multiprecision.h1"></a>
|
|
<span class="phrase"><a name="math_toolkit.high_precision.use_multiprecision.using_boost_multiprecision_to_ge"></a></span><a class="link" href="use_multiprecision.html#math_toolkit.high_precision.use_multiprecision.using_boost_multiprecision_to_ge">Using
|
|
Boost.Multiprecision to generate a high-precision array of sine coefficents
|
|
for use with FFT.</a>
|
|
</h6>
|
|
<p>
|
|
The Boost.Multiprecision library can be used for computations requiring precision
|
|
exceeding that of standard built-in types such as <code class="computeroutput"><span class="keyword">float</span></code>,
|
|
<code class="computeroutput"><span class="keyword">double</span></code> and <code class="computeroutput"><span class="keyword">long</span>
|
|
<span class="keyword">double</span></code>. For extended-precision calculations,
|
|
Boost.Multiprecision supplies a template data type called <code class="computeroutput"><span class="identifier">cpp_bin_float</span></code>.
|
|
The number of decimal digits of precision is fixed at compile-time via a
|
|
template parameter.
|
|
</p>
|
|
<p>
|
|
One often needs to compute tables of numbers in mathematical software. To
|
|
avoid the <a href="https://en.wikipedia.org/wiki/Rounding#Table-maker's_dilemma" target="_top">Table-maker's
|
|
dilemma</a> it is necessary to use a higher precision type to compute
|
|
the table values so that they have the nearest representable bit-pattern
|
|
for the type, say <code class="computeroutput"><span class="keyword">double</span></code>, of
|
|
the table value.
|
|
</p>
|
|
<p>
|
|
This example is a program <code class="computeroutput"><span class="identifier">fft_since_table</span><span class="special">.</span><span class="identifier">cpp</span></code> that
|
|
writes a header file <code class="computeroutput"><span class="identifier">sines</span><span class="special">.</span><span class="identifier">hpp</span></code> containing
|
|
an array of sine coefficients for use with a Fast Fourier Transform (FFT),
|
|
that can be included by the FFT program.
|
|
</p>
|
|
<p>
|
|
To use Boost.Multiprecision's high-precision floating-point types and constants,
|
|
we need some includes:
|
|
</p>
|
|
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">constants</span><span class="special">/</span><span class="identifier">constants</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
|
<span class="comment">// using boost::math::constants::pi;</span>
|
|
|
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multiprecision</span><span class="special">/</span><span class="identifier">cpp_bin_float</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> <span class="comment">// for</span>
|
|
<span class="comment">// using boost::multiprecision::cpp_bin_float and</span>
|
|
<span class="comment">// using boost::multiprecision::cpp_bin_float_50;</span>
|
|
<span class="comment">// using boost::multiprecision::cpp_bin_float_quad;</span>
|
|
|
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">array</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> <span class="comment">// or <array> for std::array</span>
|
|
|
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span>
|
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">limits</span><span class="special">></span>
|
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">vector</span><span class="special">></span>
|
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">algorithm</span><span class="special">></span>
|
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iomanip</span><span class="special">></span>
|
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iterator</span><span class="special">></span>
|
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">fstream</span><span class="special">></span>
|
|
</pre>
|
|
<p>
|
|
First, this example defines a prolog text string which is a C++ comment with
|
|
the program licence, copyright etc. (You would of course, tailor this to
|
|
your needs, including <span class="bold"><strong>your</strong></span> copyright claim).
|
|
This will appear at the top of the written header file <code class="computeroutput"><span class="identifier">sines</span><span class="special">.</span><span class="identifier">hpp</span></code>.
|
|
</p>
|
|
<pre class="programlisting"><span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">cpp_bin_float_50</span><span class="special">;</span>
|
|
<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">constants</span><span class="special">::</span><span class="identifier">pi</span><span class="special">;</span>
|
|
</pre>
|
|
<p>
|
|
A fast Fourier transform (FFT), for example, may use a table of the values
|
|
of sin((π/2<sup>n</sup>) in its implementation details. In order to maximize the precision
|
|
in the FFT implementation, the precision of the tabulated trigonometric values
|
|
should exceed that of the built-in floating-point type used in the FFT.
|
|
</p>
|
|
<p>
|
|
The sample below computes a table of the values of sin(π/2<sup>n</sup>) in the range
|
|
1 <= n <= 31.
|
|
</p>
|
|
<p>
|
|
This program makes use of, among other program elements, the data type <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">cpp_bin_float_50</span></code> for a precision of 50
|
|
decimal digits from Boost.Multiprecision, the value of constant π retrieved
|
|
from Boost.Math, guaranteed to be initialized with the very last bit of precision
|
|
for the type, here <code class="computeroutput"><span class="identifier">cpp_bin_float_50</span></code>,
|
|
and a C++11 lambda function combined with <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">for_each</span><span class="special">()</span></code>.
|
|
</p>
|
|
<p>
|
|
define the number of values (32) in the array of sines.
|
|
</p>
|
|
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">size</span> <span class="special">=</span> <span class="number">32U</span><span class="special">;</span>
|
|
<span class="comment">//cpp_bin_float_50 p = pi<cpp_bin_float_50>();</span>
|
|
<span class="identifier">cpp_bin_float_50</span> <span class="identifier">p</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">constants</span><span class="special">::</span><span class="identifier">pi</span><span class="special"><</span><span class="identifier">cpp_bin_float_50</span><span class="special">>();</span>
|
|
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span> <span class="special"><</span><span class="identifier">cpp_bin_float_50</span><span class="special">></span> <span class="identifier">sin_values</span> <span class="special">(</span><span class="identifier">size</span><span class="special">);</span>
|
|
<span class="keyword">unsigned</span> <span class="identifier">n</span> <span class="special">=</span> <span class="number">1U</span><span class="special">;</span>
|
|
<span class="comment">// Generate the sine values.</span>
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">for_each</span>
|
|
<span class="special">(</span>
|
|
<span class="identifier">sin_values</span><span class="special">.</span><span class="identifier">begin</span> <span class="special">(),</span>
|
|
<span class="identifier">sin_values</span><span class="special">.</span><span class="identifier">end</span> <span class="special">(),</span>
|
|
<span class="special">[&</span><span class="identifier">n</span><span class="special">](</span><span class="identifier">cpp_bin_float_50</span><span class="special">&</span> <span class="identifier">y</span><span class="special">)</span>
|
|
<span class="special">{</span>
|
|
<span class="identifier">y</span> <span class="special">=</span> <span class="identifier">sin</span><span class="special">(</span> <span class="identifier">pi</span><span class="special"><</span><span class="identifier">cpp_bin_float_50</span><span class="special">>()</span> <span class="special">/</span> <span class="identifier">pow</span><span class="special">(</span><span class="identifier">cpp_bin_float_50</span> <span class="special">(</span><span class="number">2</span><span class="special">),</span> <span class="identifier">n</span><span class="special">));</span>
|
|
<span class="special">++</span><span class="identifier">n</span><span class="special">;</span>
|
|
<span class="special">}</span>
|
|
<span class="special">);</span>
|
|
</pre>
|
|
<p>
|
|
Define the floating-point type for the generated file, either built-in <code class="computeroutput"><span class="keyword">double</span><span class="special">,</span> </code>float,
|
|
or <code class="computeroutput"><span class="keyword">long</span> <span class="keyword">double</span></code>,
|
|
or a user defined type like <code class="computeroutput"><span class="identifier">cpp_bin_float_50</span></code>.
|
|
</p>
|
|
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">fp_type</span> <span class="special">=</span> <span class="string">"double"</span><span class="special">;</span>
|
|
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Generating an `std::array` or `boost::array` for floating-point type: "</span>
|
|
<span class="special"><<</span> <span class="identifier">fp_type</span> <span class="special"><<</span> <span class="string">". "</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
|
|
</pre>
|
|
<p>
|
|
By default, output would only show the standard 6 decimal digits, so set
|
|
precision to show enough significant digits for the chosen floating-point
|
|
type. For <code class="computeroutput"><span class="identifier">cpp_bin_float_50</span></code>
|
|
is 50. (50 decimal digits should be ample for most applications).
|
|
</p>
|
|
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">streamsize</span> <span class="identifier">precision</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">cpp_bin_float_50</span><span class="special">>::</span><span class="identifier">digits10</span><span class="special">;</span>
|
|
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Sines table precision is "</span> <span class="special"><<</span> <span class="identifier">precision</span> <span class="special"><<</span> <span class="string">" decimal digits. "</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
|
|
</pre>
|
|
<p>
|
|
Of course, one could also choose a lower precision for the table values,
|
|
for example,
|
|
</p>
|
|
<p>
|
|
<code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">streamsize</span> <span class="identifier">precision</span>
|
|
<span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">cpp_bin_float_quad</span><span class="special">>::</span><span class="identifier">max_digits10</span><span class="special">;</span></code>
|
|
</p>
|
|
<p>
|
|
128-bit 'quad' precision of 36 decimal digits would be sufficient for the
|
|
most precise current <code class="computeroutput"><span class="keyword">long</span> <span class="keyword">double</span></code>
|
|
implementations using 128-bit. In general, it should be a couple of decimal
|
|
digits more (guard digits) than <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">RealType</span><span class="special">>::</span><span class="identifier">max_digits10</span></code>
|
|
for the target system floating-point type. (If the implementation does not
|
|
provide <code class="computeroutput"><span class="identifier">max_digits10</span></code>, the
|
|
the Kahan formula <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">RealType</span><span class="special">>::</span><span class="identifier">digits</span> <span class="special">*</span> <span class="number">3010</span><span class="special">/</span><span class="number">10000</span>
|
|
<span class="special">+</span> <span class="number">2</span></code>
|
|
can be used instead).
|
|
</p>
|
|
<p>
|
|
The compiler will read these values as decimal digits strings and use the
|
|
nearest representation for the floating-point type.
|
|
</p>
|
|
<p>
|
|
Now output all the sine table, to a file of your chosen name.
|
|
</p>
|
|
<pre class="programlisting"><span class="keyword">const</span> <span class="keyword">char</span> <span class="identifier">sines_name</span><span class="special">[]</span> <span class="special">=</span> <span class="string">"sines.hpp"</span><span class="special">;</span> <span class="comment">// Assuming in same directory as .exe</span>
|
|
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">ofstream</span> <span class="identifier">fout</span><span class="special">(</span><span class="identifier">sines_name</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ios_base</span><span class="special">::</span><span class="identifier">out</span><span class="special">);</span> <span class="comment">// Creates if no file exists,</span>
|
|
<span class="comment">// & uses default overwrite/ ios::replace.</span>
|
|
<span class="keyword">if</span> <span class="special">(</span><span class="identifier">fout</span><span class="special">.</span><span class="identifier">is_open</span><span class="special">()</span> <span class="special">==</span> <span class="keyword">false</span><span class="special">)</span>
|
|
<span class="special">{</span> <span class="comment">// failed to open OK!</span>
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Open file "</span> <span class="special"><<</span> <span class="identifier">sines_name</span> <span class="special"><<</span> <span class="string">" failed!"</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
|
|
<span class="keyword">return</span> <span class="identifier">EXIT_FAILURE</span><span class="special">;</span>
|
|
<span class="special">}</span>
|
|
<span class="keyword">else</span>
|
|
<span class="special">{</span> <span class="comment">// Write prolog etc as a C++ comment.</span>
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Open file "</span> <span class="special"><<</span> <span class="identifier">sines_name</span> <span class="special"><<</span> <span class="string">" for output OK."</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
|
|
<span class="identifier">fout</span> <span class="special"><<</span> <span class="identifier">prolog</span>
|
|
<span class="special"><<</span> <span class="string">"// Table of "</span> <span class="special"><<</span> <span class="identifier">sin_values</span><span class="special">.</span><span class="identifier">size</span><span class="special">()</span> <span class="special"><<</span> <span class="string">" values with "</span>
|
|
<span class="special"><<</span> <span class="identifier">precision</span> <span class="special"><<</span> <span class="string">" decimal digits precision,\n"</span>
|
|
<span class="string">"// generated by program fft_sines_table.cpp.\n"</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
|
|
|
|
<span class="identifier">fout</span> <span class="special"><<</span> <span class="string">"#include <array> // std::array"</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
|
|
|
|
<span class="comment">// Write the table of sines as a C++ array.</span>
|
|
<span class="identifier">fout</span> <span class="special"><<</span> <span class="string">"\nstatic const std::array<double, "</span> <span class="special"><<</span> <span class="identifier">size</span> <span class="special"><<</span> <span class="string">"> sines =\n"</span>
|
|
<span class="string">"{{\n"</span><span class="special">;</span> <span class="comment">// 2nd { needed for some old GCC compiler versions.</span>
|
|
<span class="identifier">fout</span><span class="special">.</span><span class="identifier">precision</span><span class="special">(</span><span class="identifier">precision</span><span class="special">);</span>
|
|
|
|
<span class="keyword">for</span> <span class="special">(</span><span class="keyword">unsigned</span> <span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0U</span><span class="special">;</span> <span class="special">;)</span>
|
|
<span class="special">{</span>
|
|
<span class="identifier">fout</span> <span class="special"><<</span> <span class="string">" "</span> <span class="special"><<</span> <span class="identifier">sin_values</span><span class="special">[</span><span class="identifier">i</span><span class="special">];</span>
|
|
<span class="keyword">if</span> <span class="special">(</span><span class="identifier">i</span> <span class="special">==</span> <span class="identifier">sin_values</span><span class="special">.</span><span class="identifier">size</span><span class="special">()-</span><span class="number">1</span><span class="special">)</span>
|
|
<span class="special">{</span> <span class="comment">// next is last value.</span>
|
|
<span class="identifier">fout</span> <span class="special"><<</span> <span class="string">"\n}}; // array sines\n"</span><span class="special">;</span> <span class="comment">// 2nd } needed for some old GCC compiler versions.</span>
|
|
<span class="keyword">break</span><span class="special">;</span>
|
|
<span class="special">}</span>
|
|
<span class="keyword">else</span>
|
|
<span class="special">{</span>
|
|
<span class="identifier">fout</span> <span class="special"><<</span> <span class="string">",\n"</span><span class="special">;</span>
|
|
<span class="identifier">i</span><span class="special">++;</span>
|
|
<span class="special">}</span>
|
|
<span class="special">}</span> <span class="comment">// for</span>
|
|
|
|
<span class="identifier">fout</span><span class="special">.</span><span class="identifier">close</span><span class="special">();</span>
|
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Closed file "</span> <span class="special"><<</span> <span class="identifier">sines_name</span> <span class="special"><<</span> <span class="string">" for output."</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
|
|
<span class="special">}</span>
|
|
</pre>
|
|
<p>
|
|
The output file generated can be seen at <a href="../../../../example/sines.hpp" target="_top">../../example/sines.hpp</a>
|
|
</p>
|
|
<p>
|
|
The table output is:
|
|
</p>
|
|
<pre class="programlisting"><span class="identifier">The</span> <span class="identifier">printed</span> <span class="identifier">table</span> <span class="identifier">is</span><span class="special">:</span>
|
|
|
|
<span class="number">1</span>
|
|
<span class="number">0.70710678118654752440084436210484903928483593768847</span>
|
|
<span class="number">0.38268343236508977172845998403039886676134456248563</span>
|
|
<span class="number">0.19509032201612826784828486847702224092769161775195</span>
|
|
<span class="number">0.098017140329560601994195563888641845861136673167501</span>
|
|
<span class="number">0.049067674327418014254954976942682658314745363025753</span>
|
|
<span class="number">0.024541228522912288031734529459282925065466119239451</span>
|
|
<span class="number">0.012271538285719926079408261951003212140372319591769</span>
|
|
<span class="number">0.0061358846491544753596402345903725809170578863173913</span>
|
|
<span class="number">0.003067956762965976270145365490919842518944610213452</span>
|
|
<span class="number">0.0015339801862847656123036971502640790799548645752374</span>
|
|
<span class="number">0.00076699031874270452693856835794857664314091945206328</span>
|
|
<span class="number">0.00038349518757139558907246168118138126339502603496474</span>
|
|
<span class="number">0.00019174759731070330743990956198900093346887403385916</span>
|
|
<span class="number">9.5873799095977345870517210976476351187065612851145e-05</span>
|
|
<span class="number">4.7936899603066884549003990494658872746866687685767e-05</span>
|
|
<span class="number">2.3968449808418218729186577165021820094761474895673e-05</span>
|
|
<span class="number">1.1984224905069706421521561596988984804731977538387e-05</span>
|
|
<span class="number">5.9921124526424278428797118088908617299871778780951e-06</span>
|
|
<span class="number">2.9960562263346607504548128083570598118251878683408e-06</span>
|
|
<span class="number">1.4980281131690112288542788461553611206917585861527e-06</span>
|
|
<span class="number">7.4901405658471572113049856673065563715595930217207e-07</span>
|
|
<span class="number">3.7450702829238412390316917908463317739740476297248e-07</span>
|
|
<span class="number">1.8725351414619534486882457659356361712045272098287e-07</span>
|
|
<span class="number">9.3626757073098082799067286680885620193236507169473e-08</span>
|
|
<span class="number">4.681337853654909269511551813854009695950362701667e-08</span>
|
|
<span class="number">2.3406689268274552759505493419034844037886207223779e-08</span>
|
|
<span class="number">1.1703344634137277181246213503238103798093456639976e-08</span>
|
|
<span class="number">5.8516723170686386908097901008341396943900085051757e-09</span>
|
|
<span class="number">2.9258361585343193579282304690689559020175857150074e-09</span>
|
|
<span class="number">1.4629180792671596805295321618659637103742615227834e-09</span>
|
|
<span class="special">*/</span>
|
|
</pre>
|
|
<p>
|
|
The output can be copied as text and readily integrated into a given source
|
|
code. Alternatively, the output can be written to a text or even be used
|
|
within a self-written automatic code generator as this example.
|
|
</p>
|
|
<p>
|
|
A computer algebra system can be used to verify the results obtained from
|
|
Boost.Math and Boost.Multiprecision. For example, the <a href="http://www.wolfram.com/products/mathematica/index.html" target="_top">Wolfram
|
|
Mathematica</a> computer algebra system can obtain a similar table with
|
|
the command:
|
|
</p>
|
|
<pre class="programlisting"><span class="identifier">Table</span><span class="special">[</span><span class="identifier">N</span><span class="special">[</span><span class="identifier">Sin</span><span class="special">[</span><span class="identifier">Pi</span> <span class="special">/</span> <span class="special">(</span><span class="number">2</span><span class="special">^</span><span class="identifier">n</span><span class="special">)],</span> <span class="number">50</span><span class="special">],</span> <span class="special">{</span><span class="identifier">n</span><span class="special">,</span> <span class="number">1</span><span class="special">,</span> <span class="number">31</span><span class="special">,</span> <span class="number">1</span><span class="special">}]</span>
|
|
</pre>
|
|
<p>
|
|
The <a href="http://www.wolframalpha.com/" target="_top">Wolfram Alpha</a> computational
|
|
knowledge engine can also be used to generate this table. The same command
|
|
can be pasted into the compute box.
|
|
</p>
|
|
<p>
|
|
The full source of this example is at <a href="../../../../example/fft_sines_table.cpp" target="_top">fft_sines_table.cpp</a>
|
|
</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 © 2006-2019 Nikhar
|
|
Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos,
|
|
Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan
|
|
Rå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="why_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="float128.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
|
|
</div>
|
|
</body>
|
|
</html>
|