3 <meta http-equiv=
"Content-Type" content=
"text/html; charset=US-ASCII">
4 <title>Trading Accuracy for Performance
</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=
"../perf.html" title=
"Chapter 16. Performance">
9 <link rel=
"prev" href=
"getting_best.html" title=
"Getting the Best Performance from this Library: Compiler and Compiler Options">
10 <link rel=
"next" href=
"multiprecision.html" title=
"Cost of High-Precision Non-built-in Floating-point">
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=
"getting_best.html"><img src=
"../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../perf.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=
"multiprecision.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.tradoffs"></a><a class=
"link" href=
"tradoffs.html" title=
"Trading Accuracy for Performance">Trading Accuracy for Performance
</a>
28 </h2></div></div></div>
30 There are a number of
<a class=
"link" href=
"../policy.html" title=
"Chapter 15. Policies: Controlling Precision, Error Handling etc">Policies
</a> that can be used
31 to trade accuracy for performance:
33 <div class=
"itemizedlist"><ul class=
"itemizedlist" style=
"list-style-type: disc; ">
35 Internal promotion: by default functions with
<code class=
"computeroutput"><span class=
"keyword">float
</span></code>
36 arguments are evaluated at
<code class=
"computeroutput"><span class=
"keyword">double
</span></code>
37 precision internally to ensure full precision in the result. Similarly
38 <code class=
"computeroutput"><span class=
"keyword">double
</span></code> precision functions
39 are evaluated at
<code class=
"computeroutput"><span class=
"keyword">long
</span> <span class=
"keyword">double
</span></code>
40 precision internally by default. Changing these defaults can have a significant
41 speed advantage at the expense of accuracy, note also that evaluating using
42 <code class=
"computeroutput"><span class=
"keyword">float
</span></code> internally may result
43 in numerical instability for some of the more complex algorithms, we suggest
44 you use this option with care.
47 Target accuracy: just because you choose to evaluate at
<code class=
"computeroutput"><span class=
"keyword">double
</span></code>
48 precision doesn't mean you necessarily want to target full
16-digit accuracy,
49 if you wish you can change the default (full machine precision) to whatever
50 is
"good enough" for your particular use case.
54 For example, suppose you want to evaluate
<code class=
"computeroutput"><span class=
"keyword">double
</span></code>
55 precision functions at
<code class=
"computeroutput"><span class=
"keyword">double
</span></code>
56 precision internally, you can change the global default by passing
<code class=
"computeroutput"><span class=
"special">-
</span><span class=
"identifier">DBOOST_MATH_PROMOTE_DOUBLE_POLICY
</span><span class=
"special">=
</span><span class=
"keyword">false
</span></code> on the
57 command line, or at the point of call via something like this:
59 <pre class=
"programlisting"><span class=
"keyword">double
</span> <span class=
"identifier">val
</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">erf
</span><span class=
"special">(
</span><span class=
"identifier">my_argument
</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">policies
</span><span class=
"special">::
</span><span class=
"identifier">make_policy
</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">policies
</span><span class=
"special">::
</span><span class=
"identifier">promote_double
</span><span class=
"special"><</span><span class=
"keyword">false
</span><span class=
"special">>()));
</span>
62 However, an easier option might be:
64 <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">special_functions
</span><span class=
"special">.
</span><span class=
"identifier">hpp
</span><span class=
"special">></span> <span class=
"comment">// Or any individual special function header
</span>
66 <span class=
"keyword">namespace
</span> <span class=
"identifier">math
</span><span class=
"special">{
</span>
68 <span class=
"keyword">namespace
</span> <span class=
"identifier">precise
</span><span class=
"special">{
</span>
69 <span class=
"comment">//
</span>
70 <span class=
"comment">// Define a Policy for accurate evaluation - this is the same as the default, unless
</span>
71 <span class=
"comment">// someone has changed the global defaults.
</span>
72 <span class=
"comment">//
</span>
73 <span class=
"keyword">typedef
</span> <span class=
"identifier">boost
</span><span class=
"special">::
</span><span class=
"identifier">math
</span><span class=
"special">::
</span><span class=
"identifier">policies
</span><span class=
"special">::
</span><span class=
"identifier">policy
</span><span class=
"special"><></span> <span class=
"identifier">accurate_policy
</span><span class=
"special">;
</span>
74 <span class=
"comment">//
</span>
75 <span class=
"comment">// Invoke BOOST_MATH_DECLARE_SPECIAL_FUNCTIONS to declare
</span>
76 <span class=
"comment">// functions that use the above policy. Note no trailing
</span>
77 <span class=
"comment">//
";" required on the macro call:
</span>
78 <span class=
"comment">//
</span>
79 <span class=
"identifier">BOOST_MATH_DECLARE_SPECIAL_FUNCTIONS
</span><span class=
"special">(
</span><span class=
"identifier">accurate_policy
</span><span class=
"special">)
</span>
82 <span class=
"special">}
</span>
84 <span class=
"keyword">namespace
</span> <span class=
"identifier">fast
</span><span class=
"special">{
</span>
85 <span class=
"comment">//
</span>
86 <span class=
"comment">// Define a Policy for fast evaluation:
</span>
87 <span class=
"comment">//
</span>
88 <span class=
"keyword">using
</span> <span class=
"keyword">namespace
</span> <span class=
"identifier">boost
</span><span class=
"special">::
</span><span class=
"identifier">math
</span><span class=
"special">::
</span><span class=
"identifier">polcies
</span><span class=
"special">;
</span>
89 <span class=
"keyword">typedef
</span> <span class=
"identifier">policy
</span><span class=
"special"><</span><span class=
"identifier">promote_double
</span><span class=
"special"><</span><span class=
"keyword">false
</span><span class=
"special">></span> <span class=
"special">></span> <span class=
"identifier">fast_policy
</span><span class=
"special">;
</span>
90 <span class=
"comment">//
</span>
91 <span class=
"comment">// Invoke BOOST_MATH_DECLARE_SPECIAL_FUNCTIONS:
</span>
92 <span class=
"comment">//
</span>
93 <span class=
"identifier">BOOST_MATH_DECLARE_SPECIAL_FUNCTIONS
</span><span class=
"special">(
</span><span class=
"identifier">fast_policy
</span><span class=
"special">)
</span>
95 <span class=
"special">}
</span>
97 <span class=
"special">}
</span>
100 And now one can call:
102 <pre class=
"programlisting"><span class=
"identifier">math
</span><span class=
"special">::
</span><span class=
"identifier">accurate
</span><span class=
"special">::
</span><span class=
"identifier">tgamma
</span><span class=
"special">(
</span><span class=
"identifier">x
</span><span class=
"special">);
</span>
105 For the
"accurate" version of tgamma, and:
107 <pre class=
"programlisting"><span class=
"identifier">math
</span><span class=
"special">::
</span><span class=
"identifier">fast
</span><span class=
"special">::
</span><span class=
"identifier">tgamma
</span><span class=
"special">(
</span><span class=
"identifier">x
</span><span class=
"special">);
</span>
110 For the faster version.
113 Had we wished to change the target precision (to
9 decimal places) as well
114 as the evaluation type used, we might have done:
116 <pre class=
"programlisting"><span class=
"keyword">namespace
</span> <span class=
"identifier">math
</span><span class=
"special">{
</span>
117 <span class=
"keyword">namespace
</span> <span class=
"identifier">fast
</span><span class=
"special">{
</span>
118 <span class=
"comment">//
</span>
119 <span class=
"comment">// Define a Policy for fast evaluation:
</span>
120 <span class=
"comment">//
</span>
121 <span class=
"keyword">using
</span> <span class=
"keyword">namespace
</span> <span class=
"identifier">boost
</span><span class=
"special">::
</span><span class=
"identifier">math
</span><span class=
"special">::
</span><span class=
"identifier">polcies
</span><span class=
"special">;
</span>
122 <span class=
"keyword">typedef
</span> <span class=
"identifier">policy
</span><span class=
"special"><</span><span class=
"identifier">promote_double
</span><span class=
"special"><</span><span class=
"keyword">false
</span><span class=
"special">>,
</span> <span class=
"identifier">digits10
</span><span class=
"special"><</span><span class=
"number">9</span><span class=
"special">></span> <span class=
"special">></span> <span class=
"identifier">fast_policy
</span><span class=
"special">;
</span>
123 <span class=
"comment">//
</span>
124 <span class=
"comment">// Invoke BOOST_MATH_DECLARE_SPECIAL_FUNCTIONS:
</span>
125 <span class=
"comment">//
</span>
126 <span class=
"identifier">BOOST_MATH_DECLARE_SPECIAL_FUNCTIONS
</span><span class=
"special">(
</span><span class=
"identifier">fast_policy
</span><span class=
"special">)
</span>
128 <span class=
"special">}
</span>
129 <span class=
"special">}
</span>
132 One can do a similar thing with the distribution classes:
134 <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">distributions
</span><span class=
"special">.
</span><span class=
"identifier">hpp
</span><span class=
"special">></span> <span class=
"comment">// or any individual distribution header
</span>
136 <span class=
"keyword">namespace
</span> <span class=
"identifier">math
</span><span class=
"special">{
</span> <span class=
"keyword">namespace
</span> <span class=
"identifier">fast
</span><span class=
"special">{
</span>
137 <span class=
"comment">//
</span>
138 <span class=
"comment">// Define a policy for fastest possible evaluation:
</span>
139 <span class=
"comment">//
</span>
140 <span class=
"keyword">using
</span> <span class=
"keyword">namespace
</span> <span class=
"identifier">boost
</span><span class=
"special">::
</span><span class=
"identifier">math
</span><span class=
"special">::
</span><span class=
"identifier">polcies
</span><span class=
"special">;
</span>
141 <span class=
"keyword">typedef
</span> <span class=
"identifier">policy
</span><span class=
"special"><</span><span class=
"identifier">promote_float
</span><span class=
"special"><</span><span class=
"keyword">false
</span><span class=
"special">></span> <span class=
"special">></span> <span class=
"identifier">fast_float_policy
</span><span class=
"special">;
</span>
142 <span class=
"comment">//
</span>
143 <span class=
"comment">// Invoke BOOST_MATH_DECLARE_DISTRIBUTIONS
</span>
144 <span class=
"comment">//
</span>
145 <span class=
"identifier">BOOST_MATH_DECLARE_DISTRIBUTIONS
</span><span class=
"special">(
</span><span class=
"keyword">float
</span><span class=
"special">,
</span> <span class=
"identifier">fast_float_policy
</span><span class=
"special">)
</span>
147 <span class=
"special">}}
</span> <span class=
"comment">// namespaces
</span>
149 <span class=
"comment">//
</span>
150 <span class=
"comment">// And use:
</span>
151 <span class=
"comment">//
</span>
152 <span class=
"keyword">float
</span> <span class=
"identifier">p_val
</span> <span class=
"special">=
</span> <span class=
"identifier">cdf
</span><span class=
"special">(
</span><span class=
"identifier">math
</span><span class=
"special">::
</span><span class=
"identifier">fast
</span><span class=
"special">::
</span><span class=
"identifier">normal
</span><span class=
"special">(
</span><span class=
"number">1.0f
</span><span class=
"special">,
</span> <span class=
"number">3.0f
</span><span class=
"special">),
</span> <span class=
"number">0.25f
</span><span class=
"special">);
</span>
155 Here's how these options change the relative performance of the distributions
159 [table_Distribution_performance_comparison_for_different_performance_options_with_GNU_C_version_5_1_0_on_linux]
162 <table xmlns:
rev=
"http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width=
"100%"><tr>
163 <td align=
"left"></td>
164 <td align=
"right"><div class=
"copyright-footer">Copyright
© 2006-
2010,
2012-
2014 Nikhar Agrawal,
165 Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert
166 Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Johan R
åde, Gautam Sewani,
167 Benjamin Sobotta, Thijs van den Berg, Daryle Walker and Xiaogang Zhang
<p>
168 Distributed under the Boost Software License, Version
1.0. (See accompanying
169 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>)
174 <div class=
"spirit-nav">
175 <a accesskey=
"p" href=
"getting_best.html"><img src=
"../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../perf.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=
"multiprecision.html"><img src=
"../../../../../doc/src/images/next.png" alt=
"Next"></a>