1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Variable-Precision Newton Evaluation</title> 5<link rel="stylesheet" href="../../../../multiprecision.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../../../index.html" title="Chapter 1. Boost.Multiprecision"> 8<link rel="up" href="../fp_eg.html" title="Examples"> 9<link rel="prev" href="poly_eg.html" title="Polynomial Evaluation"> 10<link rel="next" href="gauss_lagerre_quadrature.html" title="Gauss-Laguerre quadrature"> 11</head> 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> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="poly_eg.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../fp_eg.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="gauss_lagerre_quadrature.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h5 class="title"> 27<a name="boost_multiprecision.tut.floats.fp_eg.variable_precision"></a><a class="link" href="variable_precision.html" title="Variable-Precision Newton Evaluation">Variable-Precision 28 Newton Evaluation</a> 29</h5></div></div></div> 30<p> 31 This example illustrates the use of variable-precision arithmetic with 32 the <code class="computeroutput"><span class="identifier">mpfr_float</span></code> number 33 type. We'll calculate the median of the beta distribution to an absurdly 34 high precision and compare the accuracy and times taken for various methods. 35 That is, we want to calculate the value of <code class="computeroutput"><span class="identifier">x</span></code> 36 for which <span class="emphasis"><em>I<sub>x</sub>(a, b) = 0.5</em></span>. 37 </p> 38<p> 39 Ultimately we'll use Newtons method and set the precision of mpfr_float 40 to have just enough digits at each iteration. 41 </p> 42<p> 43 The full source of the this program is in <a href="http://www.boost.org/doc/libs/release/libs/multiprecision/doc/html/../../example/mpfr_precision.cpp" target="_top">../../example/mpfr_precision.cpp</a> 44 </p> 45<p> 46 We'll skip over the #includes and using declations, and go straight to 47 some support code, first off a simple stopwatch for performance measurement: 48 </p> 49<pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">clock_type</span><span class="special">></span> 50<span class="keyword">struct</span> <span class="identifier">stopwatch</span> <span class="special">{</span> <span class="comment">/*details \*/</span> <span class="special">};</span> 51</pre> 52<p> 53 We'll use <code class="computeroutput"><span class="identifier">stopwatch</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">chono</span><span class="special">::</span><span class="identifier">high_resolution_clock</span><span class="special">></span></code> 54 as our performance measuring device. 55 </p> 56<p> 57 We also have a small utility class for controlling the current precision 58 of mpfr_float: 59 </p> 60<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">scoped_precision</span> 61<span class="special">{</span> 62 <span class="keyword">unsigned</span> <span class="identifier">p</span><span class="special">;</span> 63 <span class="identifier">scoped_precision</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">new_p</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">p</span><span class="special">(</span><span class="identifier">mpfr_float</span><span class="special">::</span><span class="identifier">default_precision</span><span class="special">())</span> 64 <span class="special">{</span> 65 <span class="identifier">mpfr_float</span><span class="special">::</span><span class="identifier">default_precision</span><span class="special">(</span><span class="identifier">new_p</span><span class="special">);</span> 66 <span class="special">}</span> 67 <span class="special">~</span><span class="identifier">scoped_precision</span><span class="special">()</span> 68 <span class="special">{</span> 69 <span class="identifier">mpfr_float</span><span class="special">::</span><span class="identifier">default_precision</span><span class="special">(</span><span class="identifier">p</span><span class="special">);</span> 70 <span class="special">}</span> 71<span class="special">};</span> 72</pre> 73<p> 74 We'll begin with a reference method that simply calls the Boost.Math 75 function <code class="computeroutput"><span class="identifier">ibeta_inv</span></code> and 76 uses the full working precision of the arguments throughout. Our reference 77 function takes 3 arguments: 78 </p> 79<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 80<li class="listitem"> 81 The 2 parameters <code class="computeroutput"><span class="identifier">a</span></code> 82 and <code class="computeroutput"><span class="identifier">b</span></code> of the beta 83 distribution, and 84 </li> 85<li class="listitem"> 86 The number of decimal digits precision to achieve in the result. 87 </li> 88</ul></div> 89<p> 90 We begin by setting the default working precision to that requested, 91 and then, since we don't know where our arguments <code class="computeroutput"><span class="identifier">a</span></code> 92 and <code class="computeroutput"><span class="identifier">b</span></code> have been or what 93 precision they have, we make a copy of them - note that since copying 94 also copies the precision as well as the value, we have to set the precision 95 expicitly with a second argument to the copy. Then we can simply return 96 the result of <code class="computeroutput"><span class="identifier">ibeta_inv</span></code>: 97 </p> 98<pre class="programlisting"><span class="identifier">mpfr_float</span> <span class="identifier">beta_distribution_median_method_1</span><span class="special">(</span><span class="identifier">mpfr_float</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">a_</span><span class="special">,</span> <span class="identifier">mpfr_float</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">b_</span><span class="special">,</span> <span class="keyword">unsigned</span> <span class="identifier">digits10</span><span class="special">)</span> 99<span class="special">{</span> 100 <span class="identifier">scoped_precision</span> <span class="identifier">sp</span><span class="special">(</span><span class="identifier">digits10</span><span class="special">);</span> 101 <span class="identifier">mpfr_float</span> <span class="identifier">half</span><span class="special">(</span><span class="number">0.5</span><span class="special">),</span> <span class="identifier">a</span><span class="special">(</span><span class="identifier">a_</span><span class="special">,</span> <span class="identifier">digits10</span><span class="special">),</span> <span class="identifier">b</span><span class="special">(</span><span class="identifier">b_</span><span class="special">,</span> <span class="identifier">digits10</span><span class="special">);</span> 102 <span class="keyword">return</span> <span class="identifier">ibeta_inv</span><span class="special">(</span><span class="identifier">a</span><span class="special">,</span> <span class="identifier">b</span><span class="special">,</span> <span class="identifier">half</span><span class="special">);</span> 103<span class="special">}</span> 104</pre> 105<p> 106 You be wondering why we needed to change the precision of our variables 107 <code class="computeroutput"><span class="identifier">a</span></code> and <code class="computeroutput"><span class="identifier">b</span></code> 108 as well as setting the default - there are in fact two ways in which 109 this can go wrong if we don't do that: 110 </p> 111<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 112<li class="listitem"> 113 The variables have too much precision - this will cause all arithmetic 114 operations involving those types to be promoted to the higher precision 115 wasting precious calculation time. 116 </li> 117<li class="listitem"> 118 The variables have too little precision - this will cause expressions 119 involving only those variables to be calculated at the lower precision 120 - for example if we calculate <code class="computeroutput"><span class="identifier">exp</span><span class="special">(</span><span class="identifier">a</span><span class="special">)</span></code> internally, this will be evaluated 121 at the precision of <code class="computeroutput"><span class="identifier">a</span></code>, 122 and not the current default. 123 </li> 124</ul></div> 125<p> 126 Since our reference method carries out all calculations at the full precision 127 requested, an obvious refinement would be to calculate a first approximation 128 to <code class="computeroutput"><span class="keyword">double</span></code> precision and 129 then to use Newton steps to refine it further. 130 </p> 131<p> 132 Our function begins the same as before: set the new default precision 133 and then make copies of our arguments at the correct precision. We then 134 call <code class="computeroutput"><span class="identifier">ibeta_inv</span></code> with all 135 double precision arguments, promote the result to an <code class="computeroutput"><span class="identifier">mpfr_float</span></code> 136 and perform Newton steps to obtain the result. Note that our termination 137 condition is somewhat crude: we simply assume that we have approximately 138 14 digits correct from the double-precision approximation and that the 139 precision doubles with each step. We also cheat, and use an internal 140 Boost.Math function that calculates <span class="emphasis"><em>I<sub>x</sub>(a, b)</em></span> and 141 its derivative in one go: 142 </p> 143<pre class="programlisting"><span class="identifier">mpfr_float</span> <span class="identifier">beta_distribution_median_method_2</span><span class="special">(</span><span class="identifier">mpfr_float</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">a_</span><span class="special">,</span> <span class="identifier">mpfr_float</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">b_</span><span class="special">,</span> <span class="keyword">unsigned</span> <span class="identifier">digits10</span><span class="special">)</span> 144<span class="special">{</span> 145 <span class="identifier">scoped_precision</span> <span class="identifier">sp</span><span class="special">(</span><span class="identifier">digits10</span><span class="special">);</span> 146 <span class="identifier">mpfr_float</span> <span class="identifier">half</span><span class="special">(</span><span class="number">0.5</span><span class="special">),</span> <span class="identifier">a</span><span class="special">(</span><span class="identifier">a_</span><span class="special">,</span> <span class="identifier">digits10</span><span class="special">),</span> <span class="identifier">b</span><span class="special">(</span><span class="identifier">b_</span><span class="special">,</span> <span class="identifier">digits10</span><span class="special">);</span> 147 <span class="identifier">mpfr_float</span> <span class="identifier">guess</span> <span class="special">=</span> <span class="identifier">ibeta_inv</span><span class="special">((</span><span class="keyword">double</span><span class="special">)</span><span class="identifier">a</span><span class="special">,</span> <span class="special">(</span><span class="keyword">double</span><span class="special">)</span><span class="identifier">b</span><span class="special">,</span> <span class="number">0.5</span><span class="special">);</span> 148 <span class="keyword">unsigned</span> <span class="identifier">current_digits</span> <span class="special">=</span> <span class="number">14</span><span class="special">;</span> 149 <span class="identifier">mpfr_float</span> <span class="identifier">f</span><span class="special">,</span> <span class="identifier">f1</span><span class="special">;</span> 150 <span class="keyword">while</span> <span class="special">(</span><span class="identifier">current_digits</span> <span class="special"><</span> <span class="identifier">digits10</span><span class="special">)</span> 151 <span class="special">{</span> 152 <span class="identifier">f</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">detail</span><span class="special">::</span><span class="identifier">ibeta_imp</span><span class="special">(</span><span class="identifier">a</span><span class="special">,</span> <span class="identifier">b</span><span class="special">,</span> <span class="identifier">guess</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">policy</span><span class="special"><>(),</span> <span class="keyword">false</span><span class="special">,</span> <span class="keyword">true</span><span class="special">,</span> <span class="special">&</span><span class="identifier">f1</span><span class="special">)</span> <span class="special">-</span> <span class="identifier">half</span><span class="special">;</span> 153 <span class="identifier">guess</span> <span class="special">-=</span> <span class="identifier">f</span> <span class="special">/</span> <span class="identifier">f1</span><span class="special">;</span> 154 <span class="identifier">current_digits</span> <span class="special">*=</span> <span class="number">2</span><span class="special">;</span> 155 <span class="special">}</span> 156 <span class="keyword">return</span> <span class="identifier">guess</span><span class="special">;</span> 157<span class="special">}</span> 158</pre> 159<p> 160 Before we refine the method further, it might be wise to take stock and 161 see how methods 1 and 2 compare. We'll ask them both for 1500 digit precision, 162 and compare against the value produced by <code class="computeroutput"><span class="identifier">ibeta_inv</span></code> 163 at 1700 digits. Here's what the results look like: 164 </p> 165<pre class="programlisting">Method 1 time = 0.611647 166Relative error: 2.99991e-1501 167Method 2 time = 0.646746 168Relative error: 7.55843e-1501 169</pre> 170<p> 171 Clearly they are both equally accurate, but Method 1 is actually faster 172 and our plan for improved performance hasn't actually worked. It turns 173 out that we're not actually comparing like with like, because <code class="computeroutput"><span class="identifier">ibeta_inv</span></code> uses Halley iteration internally 174 which churns out more digits of precision rather more rapidly than Newton 175 iteration. So the time we save by refining an initial <code class="computeroutput"><span class="keyword">double</span></code> 176 approximation, then loose it again by taking more iterations to get to 177 the result. 178 </p> 179<p> 180 Time for a more refined approach. It follows the same form as Method 181 2, but now we set the working precision within the Newton iteration loop, 182 to just enough digits to cover the expected precision at each step. That 183 means we also create new copies of our arguments at the correct precision 184 within the loop, and likewise change the precision of the current <code class="computeroutput"><span class="identifier">guess</span></code> each time through: 185 </p> 186<pre class="programlisting"><span class="identifier">mpfr_float</span> <span class="identifier">beta_distribution_median_method_3</span><span class="special">(</span><span class="identifier">mpfr_float</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">a_</span><span class="special">,</span> <span class="identifier">mpfr_float</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">b_</span><span class="special">,</span> <span class="keyword">unsigned</span> <span class="identifier">digits10</span><span class="special">)</span> 187<span class="special">{</span> 188 <span class="identifier">mpfr_float</span> <span class="identifier">guess</span> <span class="special">=</span> <span class="identifier">ibeta_inv</span><span class="special">((</span><span class="keyword">double</span><span class="special">)</span><span class="identifier">a_</span><span class="special">,</span> <span class="special">(</span><span class="keyword">double</span><span class="special">)</span><span class="identifier">b_</span><span class="special">,</span> <span class="number">0.5</span><span class="special">);</span> 189 <span class="keyword">unsigned</span> <span class="identifier">current_digits</span> <span class="special">=</span> <span class="number">14</span><span class="special">;</span> 190 <span class="identifier">mpfr_float</span> <span class="identifier">f</span><span class="special">(</span><span class="number">0</span><span class="special">,</span> <span class="identifier">current_digits</span><span class="special">),</span> <span class="identifier">f1</span><span class="special">(</span><span class="number">0</span><span class="special">,</span> <span class="identifier">current_digits</span><span class="special">),</span> <span class="identifier">delta</span><span class="special">(</span><span class="number">1</span><span class="special">);</span> 191 <span class="keyword">while</span> <span class="special">(</span><span class="identifier">current_digits</span> <span class="special"><</span> <span class="identifier">digits10</span><span class="special">)</span> 192 <span class="special">{</span> 193 <span class="identifier">current_digits</span> <span class="special">*=</span> <span class="number">2</span><span class="special">;</span> 194 <span class="identifier">scoped_precision</span> <span class="identifier">sp</span><span class="special">((</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">min</span><span class="special">)(</span><span class="identifier">current_digits</span><span class="special">,</span> <span class="identifier">digits10</span><span class="special">));</span> 195 <span class="identifier">mpfr_float</span> <span class="identifier">a</span><span class="special">(</span><span class="identifier">a_</span><span class="special">,</span> <span class="identifier">mpfr_float</span><span class="special">::</span><span class="identifier">default_precision</span><span class="special">()),</span> <span class="identifier">b</span><span class="special">(</span><span class="identifier">b_</span><span class="special">,</span> <span class="identifier">mpfr_float</span><span class="special">::</span><span class="identifier">default_precision</span><span class="special">());</span> 196 <span class="identifier">guess</span><span class="special">.</span><span class="identifier">precision</span><span class="special">(</span><span class="identifier">mpfr_float</span><span class="special">::</span><span class="identifier">default_precision</span><span class="special">());</span> 197 <span class="identifier">f</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">detail</span><span class="special">::</span><span class="identifier">ibeta_imp</span><span class="special">(</span><span class="identifier">a</span><span class="special">,</span> <span class="identifier">b</span><span class="special">,</span> <span class="identifier">guess</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">policy</span><span class="special"><>(),</span> <span class="keyword">false</span><span class="special">,</span> <span class="keyword">true</span><span class="special">,</span> <span class="special">&</span><span class="identifier">f1</span><span class="special">)</span> <span class="special">-</span> <span class="number">0.5f</span><span class="special">;</span> 198 <span class="identifier">guess</span> <span class="special">-=</span> <span class="identifier">f</span> <span class="special">/</span> <span class="identifier">f1</span><span class="special">;</span> 199 <span class="special">}</span> 200 <span class="keyword">return</span> <span class="identifier">guess</span><span class="special">;</span> 201<span class="special">}</span> 202</pre> 203<p> 204 The new performance results look much more promising: 205 </p> 206<pre class="programlisting">Method 1 time = 0.591244 207Relative error: 2.99991e-1501 208Method 2 time = 0.622679 209Relative error: 7.55843e-1501 210Method 3 time = 0.143393 211Relative error: 4.03898e-1501 212</pre> 213<p> 214 This time we're 4x faster than <code class="computeroutput"><span class="identifier">ibeta_inv</span></code>, 215 and no doubt that could be improved a little more by carefully optimising 216 the number of iterations and the method (Halley vs Newton) taken. 217 </p> 218<p> 219 Finally, here's the driver code for the above methods: 220 </p> 221<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 222<span class="special">{</span> 223 <span class="keyword">try</span> <span class="special">{</span> 224 <span class="identifier">mpfr_float</span> <span class="identifier">a</span><span class="special">(</span><span class="number">10</span><span class="special">),</span> <span class="identifier">b</span><span class="special">(</span><span class="number">20</span><span class="special">);</span> 225 226 <span class="identifier">mpfr_float</span> <span class="identifier">true_value</span> <span class="special">=</span> <span class="identifier">beta_distribution_median_method_1</span><span class="special">(</span><span class="identifier">a</span><span class="special">,</span> <span class="identifier">b</span><span class="special">,</span> <span class="number">1700</span><span class="special">);</span> 227 228 <span class="identifier">stopwatch</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">high_resolution_clock</span><span class="special">></span> <span class="identifier">my_stopwatch</span><span class="special">;</span> 229 230 <span class="identifier">mpfr_float</span> <span class="identifier">v1</span> <span class="special">=</span> <span class="identifier">beta_distribution_median_method_1</span><span class="special">(</span><span class="identifier">a</span><span class="special">,</span> <span class="identifier">b</span><span class="special">,</span> <span class="number">1500</span><span class="special">);</span> 231 <span class="keyword">float</span> <span class="identifier">hp_time</span> <span class="special">=</span> <span class="identifier">my_stopwatch</span><span class="special">.</span><span class="identifier">elapsed</span><span class="special">();</span> 232 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Method 1 time = "</span> <span class="special"><<</span> <span class="identifier">hp_time</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 233 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Relative error: "</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">relative_difference</span><span class="special">(</span><span class="identifier">v1</span><span class="special">,</span> <span class="identifier">true_value</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> 234 235 <span class="identifier">my_stopwatch</span><span class="special">.</span><span class="identifier">reset</span><span class="special">();</span> 236 <span class="identifier">mpfr_float</span> <span class="identifier">v2</span> <span class="special">=</span> <span class="identifier">beta_distribution_median_method_2</span><span class="special">(</span><span class="identifier">a</span><span class="special">,</span> <span class="identifier">b</span><span class="special">,</span> <span class="number">1500</span><span class="special">);</span> 237 <span class="identifier">hp_time</span> <span class="special">=</span> <span class="identifier">my_stopwatch</span><span class="special">.</span><span class="identifier">elapsed</span><span class="special">();</span> 238 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Method 2 time = "</span> <span class="special"><<</span> <span class="identifier">hp_time</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 239 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Relative error: "</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">relative_difference</span><span class="special">(</span><span class="identifier">v2</span><span class="special">,</span> <span class="identifier">true_value</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> 240 241 <span class="identifier">my_stopwatch</span><span class="special">.</span><span class="identifier">reset</span><span class="special">();</span> 242 <span class="identifier">mpfr_float</span> <span class="identifier">v3</span> <span class="special">=</span> <span class="identifier">beta_distribution_median_method_3</span><span class="special">(</span><span class="identifier">a</span><span class="special">,</span> <span class="identifier">b</span><span class="special">,</span> <span class="number">1500</span><span class="special">);</span> 243 <span class="identifier">hp_time</span> <span class="special">=</span> <span class="identifier">my_stopwatch</span><span class="special">.</span><span class="identifier">elapsed</span><span class="special">();</span> 244 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Method 3 time = "</span> <span class="special"><<</span> <span class="identifier">hp_time</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 245 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Relative error: "</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">relative_difference</span><span class="special">(</span><span class="identifier">v3</span><span class="special">,</span> <span class="identifier">true_value</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> 246 <span class="special">}</span> 247 <span class="keyword">catch</span> <span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">exception</span><span class="special">&</span> <span class="identifier">e</span><span class="special">)</span> 248 <span class="special">{</span> 249 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Found exception with message: "</span> <span class="special"><<</span> <span class="identifier">e</span><span class="special">.</span><span class="identifier">what</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> 250 <span class="special">}</span> 251 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 252<span class="special">}</span> 253</pre> 254</div> 255<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 256<td align="left"></td> 257<td align="right"><div class="copyright-footer">Copyright © 2002-2020 John 258 Maddock and Christopher Kormanyos<p> 259 Distributed under the Boost Software License, Version 1.0. (See accompanying 260 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>) 261 </p> 262</div></td> 263</tr></table> 264<hr> 265<div class="spirit-nav"> 266<a accesskey="p" href="poly_eg.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../fp_eg.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="gauss_lagerre_quadrature.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a> 267</div> 268</body> 269</html> 270