3 <meta http-equiv=
"Content-Type" content=
"text/html; charset=US-ASCII">
4 <title>Examples
</title>
5 <link rel=
"stylesheet" href=
"../math.css" type=
"text/css">
6 <meta name=
"generator" content=
"DocBook XSL Stylesheets V1.77.1">
7 <link rel=
"home" href=
"../index.html" title=
"Math Toolkit 2.5.1">
8 <link rel=
"up" href=
"../cstdfloat.html" title=
"Chapter 3. Specified-width floating-point typedefs">
9 <link rel=
"prev" href=
"macros.html" title=
"Floating-Point Constant Macros">
10 <link rel=
"next" href=
"float128_hints.html" title=
"Hints on using float128 (and __float128)">
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=
"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>
26 <div class=
"titlepage"><div><div><h2 class=
"title" style=
"clear: both">
27 <a name=
"math_toolkit.examples"></a><a class=
"link" href=
"examples.html" title=
"Examples">Examples
</a>
28 </h2></div></div></div>
30 <a name=
"math_toolkit.examples.h0"></a>
31 <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
35 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>
36 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
37 compute a simplified version of the
<a href=
"http://mathworld.wolfram.com/LambdaFunction.html" target=
"_top">Jahnke-Emden-Lambda
38 function
</a>. Here, we specify a floating-point type with
<span class=
"bold"><strong>exactly
39 64 bits
</strong></span> (i.e.,
<code class=
"computeroutput"><span class=
"identifier">float64_t
</span></code>).
40 If we were to use, for instance, built-in
<code class=
"computeroutput"><span class=
"keyword">double
</span></code>,
41 then there would be no guarantee that the code would behave identically on
42 all platforms. With
<code class=
"computeroutput"><span class=
"identifier">float64_t
</span></code>
43 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>,
44 however, it is very likely to be identical.
47 Using
<code class=
"computeroutput"><span class=
"identifier">float64_t
</span></code>, we know that
48 this code is as portable as possible and uses a floating-point type with approximately
49 15 decimal digits of precision, regardless of the compiler or version or operating
52 <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>
53 <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>
54 <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>
56 <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>
57 <span class=
"special">{
</span>
58 <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>
59 <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>
61 <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>
62 <span class=
"special">}
</span>
65 Ensure that all possibly significant digits (
17) including trailing zeros are
68 <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>
69 <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>
71 <span class=
"keyword">try
</span>
72 <span class=
"special">{
</span> <span class=
"comment">// Always use try'n'catch blocks to ensure any error messages are displayed.
</span>
74 <span class=
"comment">// Evaluate and display an evaluation of the Jahnke-Emden lambda function:
</span>
75 <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>
76 <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>
77 <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>
80 For details, see
<a href=
"../../../example/cstdfloat_example.cpp" target=
"_top">cstdfloat_example.cpp
</a>
81 - a extensive example program.
84 <a name=
"math_toolkit.examples.h1"></a>
85 <span class=
"phrase"><a name=
"math_toolkit.examples.normal_table"></a></span><a class=
"link" href=
"examples.html#math_toolkit.examples.normal_table">Normal
86 distribution table
</a>
89 This example shows printing tables of a normal distribution's PDF and CDF,
90 using
<code class=
"computeroutput"><span class=
"identifier">boost
</span><span class=
"special">::
</span><span class=
"identifier">math
</span></code> implementation of normal distribution.
93 A function templated on floating-point type prints a table for a range of standard
97 The example shows use of the specified-width typedefs to either use a specific
98 width, or to use the maximum available on the platform, perhaps a high as
128-bit.
101 The number of digits displayed is controlled by the precision of the type,
102 so there are no spurious insignificant decimal digits:
104 <pre class=
"programlisting"><span class=
"identifier">float_32_t
</span> <span class=
"number">0</span> <span class=
"number">0.39894228</span>
105 <span class=
"identifier">float_128_t
</span> <span class=
"number">0</span> <span class=
"number">0.398942280401432702863218082711682655</span>
108 Some sample output for two different platforms is appended to the code at
109 <a href=
"../../../example/normal_tables.cpp" target=
"_top">normal_tables.cpp
</a>.
111 <pre class=
"programlisting"><span class=
"preprocessor">#ifdef
</span> <span class=
"identifier">BOOST_FLOAT32_C
</span>
112 <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>
113 <span class=
"preprocessor">#endif
</span>
114 <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">// Uusually type double. Assume that float64_t is always available.
</span>
115 <span class=
"preprocessor">#ifdef
</span> <span class=
"identifier">BOOST_FLOAT80_C
</span>
116 <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>
117 <span class=
"preprocessor">#endif
</span>
118 <span class=
"preprocessor">#ifdef
</span> <span class=
"identifier">BOOST_FLOAT128_C
</span>
119 <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>
120 <span class=
"preprocessor">#endif
</span>
121 <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>
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-
2010,
2012-
2014 Nikhar Agrawal,
127 Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert
128 Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Johan R
åde, Gautam Sewani,
129 Benjamin Sobotta, Thijs van den Berg, Daryle Walker and Xiaogang Zhang
<p>
130 Distributed under the Boost Software License, Version
1.0. (See accompanying
131 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>)
136 <div class=
"spirit-nav">
137 <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>