72e469da0a
[CI SKIP]
142 lines
14 KiB
HTML
142 lines
14 KiB
HTML
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
|
|
<title>Examples</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="../cstdfloat.html" title="Chapter 3. Specified-width floating-point typedefs">
|
|
<link rel="prev" href="macros.html" title="Floating-Point Constant Macros">
|
|
<link rel="next" href="float128_hints.html" title="Hints on using float128 (and __float128)">
|
|
</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="macros.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../cstdfloat.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_hints.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
|
|
<a name="math_toolkit.examples"></a><a class="link" href="examples.html" title="Examples">Examples</a>
|
|
</h2></div></div></div>
|
|
<h4>
|
|
<a name="math_toolkit.examples.h0"></a>
|
|
<span class="phrase"><a name="math_toolkit.examples.je_lambda"></a></span><a class="link" href="examples.html#math_toolkit.examples.je_lambda">Jahnke-Emden-Lambda
|
|
function</a>
|
|
</h4>
|
|
<p>
|
|
The following code uses <code class="computeroutput"><span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">cstdfloat</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span></code>
|
|
in combination with <code class="computeroutput"><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">special_functions</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span></code> to
|
|
compute a simplified version of the <a href="http://mathworld.wolfram.com/LambdaFunction.html" target="_top">Jahnke-Emden-Lambda
|
|
function</a>. Here, we specify a floating-point type with <span class="bold"><strong>exactly
|
|
64 bits</strong></span> (i.e., <code class="computeroutput"><span class="identifier">float64_t</span></code>).
|
|
If we were to use, for instance, built-in <code class="computeroutput"><span class="keyword">double</span></code>,
|
|
then there would be no guarantee that the code would behave identically on
|
|
all platforms. With <code class="computeroutput"><span class="identifier">float64_t</span></code>
|
|
from <code class="computeroutput"><span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">cstdfloat</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span></code>,
|
|
however, it is very likely to be identical.
|
|
</p>
|
|
<p>
|
|
Using <code class="computeroutput"><span class="identifier">float64_t</span></code>, we know that
|
|
this code is as portable as possible and uses a floating-point type with approximately
|
|
15 decimal digits of precision, regardless of the compiler or version or operating
|
|
system.
|
|
</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">cstdfloat</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> <span class="comment">// For float_64_t, float128_t. Must be first include!</span>
|
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">cmath</span><span class="special">></span> <span class="comment">// for pow function.</span>
|
|
<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">special_functions</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> <span class="comment">// For gamma function.</span>
|
|
</pre>
|
|
<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">float64_t</span> <span class="identifier">jahnke_emden_lambda</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">float64_t</span> <span class="identifier">v</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">float64_t</span> <span class="identifier">x</span><span class="special">)</span>
|
|
<span class="special">{</span>
|
|
<span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">float64_t</span> <span class="identifier">gamma_v_plus_one</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">tgamma</span><span class="special">(</span><span class="identifier">v</span> <span class="special">+</span> <span class="number">1</span><span class="special">);</span>
|
|
<span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">float64_t</span> <span class="identifier">x_half_pow_v</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">pow</span><span class="special">(</span><span class="identifier">x</span> <span class="special">/</span><span class="number">2</span><span class="special">,</span> <span class="identifier">v</span><span class="special">);</span>
|
|
|
|
<span class="keyword">return</span> <span class="identifier">gamma_v_plus_one</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">cyl_bessel_j</span><span class="special">(</span><span class="identifier">x</span><span class="special">,</span> <span class="identifier">v</span><span class="special">)</span> <span class="special">/</span> <span class="identifier">x_half_pow_v</span><span class="special">;</span>
|
|
<span class="special">}</span>
|
|
</pre>
|
|
<p>
|
|
Ensure that all possibly significant digits (17) including trailing zeros are
|
|
shown.
|
|
</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">boost</span><span class="special">::</span><span class="identifier">float64_t</span><span class="special">>::</span><span class="identifier">max_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">setf</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">ios</span><span class="special">::</span><span class="identifier">showpoint</span><span class="special">);</span> <span class="comment">// Show trailing zeros.</span>
|
|
|
|
<span class="keyword">try</span>
|
|
<span class="special">{</span> <span class="comment">// Always use try'n'catch blocks to ensure any error messages are displayed.</span>
|
|
|
|
<span class="comment">// Evaluate and display an evaluation of the Jahnke-Emden lambda function:</span>
|
|
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">float64_t</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">1.</span><span class="special">;</span>
|
|
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">float64_t</span> <span class="identifier">x</span> <span class="special">=</span> <span class="number">1.</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">jahnke_emden_lambda</span><span class="special">(</span><span class="identifier">v</span><span class="special">,</span> <span class="identifier">x</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="comment">// 0.88010117148986700</span>
|
|
</pre>
|
|
<p>
|
|
For details, see <a href="../../../example/cstdfloat_example.cpp" target="_top">cstdfloat_example.cpp</a>
|
|
- a extensive example program.
|
|
</p>
|
|
<h4>
|
|
<a name="math_toolkit.examples.h1"></a>
|
|
<span class="phrase"><a name="math_toolkit.examples.normal_table"></a></span><a class="link" href="examples.html#math_toolkit.examples.normal_table">Normal
|
|
distribution table</a>
|
|
</h4>
|
|
<p>
|
|
This example shows printing tables of a normal distribution's PDF and CDF,
|
|
using <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span></code> implementation of normal distribution.
|
|
</p>
|
|
<p>
|
|
A function templated on floating-point type prints a table for a range of standard
|
|
variate z values.
|
|
</p>
|
|
<p>
|
|
The example shows use of the specified-width typedefs to either use a specific
|
|
width, or to use the maximum available on the platform, perhaps a high as 128-bit.
|
|
</p>
|
|
<p>
|
|
The number of digits displayed is controlled by the precision of the type,
|
|
so there are no spurious insignificant decimal digits:
|
|
</p>
|
|
<pre class="programlisting"><span class="identifier">float_32_t</span> <span class="number">0</span> <span class="number">0.39894228</span>
|
|
<span class="identifier">float_128_t</span> <span class="number">0</span> <span class="number">0.398942280401432702863218082711682655</span>
|
|
</pre>
|
|
<p>
|
|
Some sample output for two different platforms is appended to the code at
|
|
<a href="../../../example/normal_tables.cpp" target="_top">normal_tables.cpp</a>.
|
|
</p>
|
|
<pre class="programlisting"><span class="preprocessor">#ifdef</span> <span class="identifier">BOOST_FLOAT32_C</span>
|
|
<span class="identifier">normal_table</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">float32_t</span><span class="special">>();</span> <span class="comment">// Usually type float</span>
|
|
<span class="preprocessor">#endif</span>
|
|
<span class="identifier">normal_table</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">float64_t</span><span class="special">>();</span> <span class="comment">// Usually type double. Assume that float64_t is always available.</span>
|
|
<span class="preprocessor">#ifdef</span> <span class="identifier">BOOST_FLOAT80_C</span>
|
|
<span class="identifier">normal_table</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">float80_t</span><span class="special">>();</span> <span class="comment">// Type long double on some X86 platforms.</span>
|
|
<span class="preprocessor">#endif</span>
|
|
<span class="preprocessor">#ifdef</span> <span class="identifier">BOOST_FLOAT128_C</span>
|
|
<span class="identifier">normal_table</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">float128_t</span><span class="special">>();</span> <span class="comment">// Type _Quad on some Intel and __float128 on some GCC platforms.</span>
|
|
<span class="preprocessor">#endif</span>
|
|
<span class="identifier">normal_table</span><span class="special"><</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">floatmax_t</span><span class="special">>();</span>
|
|
</pre>
|
|
</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="macros.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../cstdfloat.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_hints.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
|
|
</div>
|
|
</body>
|
|
</html>
|