1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Bernoulli Numbers</title> 5<link rel="stylesheet" href="../../math.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../index.html" title="Math Toolkit 2.12.0"> 8<link rel="up" href="../number_series.html" title="Number Series"> 9<link rel="prev" href="../number_series.html" title="Number Series"> 10<link rel="next" href="tangent_numbers.html" title="Tangent Numbers"> 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="../number_series.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../number_series.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="tangent_numbers.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h3 class="title"> 27<a name="math_toolkit.number_series.bernoulli_numbers"></a><a class="link" href="bernoulli_numbers.html" title="Bernoulli Numbers">Bernoulli 28 Numbers</a> 29</h3></div></div></div> 30<p> 31 <a href="https://en.wikipedia.org/wiki/Bernoulli_number" target="_top">Bernoulli numbers</a> 32 are a sequence of rational numbers useful for the Taylor series expansion, 33 Euler-Maclaurin formula, and the Riemann zeta function. 34 </p> 35<p> 36 Bernoulli numbers are used in evaluation of some Boost.Math functions, including 37 the <a class="link" href="../sf_gamma/tgamma.html" title="Gamma">tgamma</a>, <a class="link" href="../sf_gamma/lgamma.html" title="Log Gamma">lgamma</a> 38 and polygamma functions. 39 </p> 40<h5> 41<a name="math_toolkit.number_series.bernoulli_numbers.h0"></a> 42 <span class="phrase"><a name="math_toolkit.number_series.bernoulli_numbers.single_bernoulli_number"></a></span><a class="link" href="bernoulli_numbers.html#math_toolkit.number_series.bernoulli_numbers.single_bernoulli_number">Single 43 Bernoulli number</a> 44 </h5> 45<h5> 46<a name="math_toolkit.number_series.bernoulli_numbers.h1"></a> 47 <span class="phrase"><a name="math_toolkit.number_series.bernoulli_numbers.synopsis"></a></span><a class="link" href="bernoulli_numbers.html#math_toolkit.number_series.bernoulli_numbers.synopsis">Synopsis</a> 48 </h5> 49<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">bernoulli</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 50</pre> 51<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">math</span> <span class="special">{</span> 52 53<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> 54<span class="identifier">T</span> <span class="identifier">bernoulli_b2n</span><span class="special">(</span><span class="keyword">const</span> <span class="keyword">int</span> <span class="identifier">n</span><span class="special">);</span> <span class="comment">// Single Bernoulli number (default policy).</span> 55 56<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Policy</span><span class="special">></span> 57<span class="identifier">T</span> <span class="identifier">bernoulli_b2n</span><span class="special">(</span><span class="keyword">const</span> <span class="keyword">int</span> <span class="identifier">n</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">Policy</span> <span class="special">&</span><span class="identifier">pol</span><span class="special">);</span> <span class="comment">// User policy for errors etc.</span> 58 59<span class="special">}}</span> <span class="comment">// namespaces</span> 60</pre> 61<h5> 62<a name="math_toolkit.number_series.bernoulli_numbers.h2"></a> 63 <span class="phrase"><a name="math_toolkit.number_series.bernoulli_numbers.description"></a></span><a class="link" href="bernoulli_numbers.html#math_toolkit.number_series.bernoulli_numbers.description">Description</a> 64 </h5> 65<p> 66 Both return the (2 * n)<sup>th</sup> Bernoulli number B<sub>2n</sub>. 67 </p> 68<p> 69 Note that since all odd numbered Bernoulli numbers are zero (apart from B<sub>1</sub> which 70 is -½) the interface will only return the even numbered Bernoulli numbers. 71 </p> 72<p> 73 This function uses fast table lookup for low-indexed Bernoulli numbers, while 74 larger values are calculated as needed and then cached. The caching mechanism 75 requires a certain amount of thread safety code, so <code class="computeroutput"><span class="identifier">unchecked_bernoulli_b2n</span></code> 76 may provide a better interface for performance critical code. 77 </p> 78<p> 79 The final <a class="link" href="../../policy.html" title="Chapter 21. Policies: Controlling Precision, Error Handling etc">Policy</a> argument is optional and can 80 be used to control the behaviour of the function: how it handles errors, 81 what level of precision to use, etc. 82 </p> 83<p> 84 Refer to <a class="link" href="../../policy.html" title="Chapter 21. Policies: Controlling Precision, Error Handling etc">Policies</a> for more details. 85 </p> 86<h5> 87<a name="math_toolkit.number_series.bernoulli_numbers.h3"></a> 88 <span class="phrase"><a name="math_toolkit.number_series.bernoulli_numbers.examples"></a></span><a class="link" href="bernoulli_numbers.html#math_toolkit.number_series.bernoulli_numbers.examples">Examples</a> 89 </h5> 90<p> 91 A simple example computes the value of B<sub>4</sub> where the return type is <code class="computeroutput"><span class="keyword">double</span></code>, note that the argument to bernoulli_b2n 92 is <span class="emphasis"><em>2</em></span> not <span class="emphasis"><em>4</em></span> since it computes B<sub>2N</sub>. 93 </p> 94<pre class="programlisting"><span class="keyword">try</span> 95<span class="special">{</span> <span class="comment">// It is always wise to use try'n'catch blocks around Boost.Math functions</span> 96 <span class="comment">// so that any informative error messages can be displayed in the catch block.</span> 97<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> 98 <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">setprecision</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="keyword">double</span><span class="special">>::</span><span class="identifier">digits10</span><span class="special">)</span> 99 <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">bernoulli_b2n</span><span class="special"><</span><span class="keyword">double</span><span class="special">>(</span><span class="number">2</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> 100</pre> 101<p> 102 So B<sub>4</sub> == -1/30 == -0.0333333333333333 103 </p> 104<p> 105 If we use Boost.Multiprecision and its 50 decimal digit floating-point type 106 <code class="computeroutput"><span class="identifier">cpp_dec_float_50</span></code>, we can 107 calculate the value of much larger numbers like B<sub>200</sub> 108and also obtain much 109 higher precision. 110 </p> 111<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> 112 <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">setprecision</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">multiprecision</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> 113 <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">bernoulli_b2n</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_50</span><span class="special">>(</span><span class="number">100</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> 114</pre> 115<pre class="programlisting"><span class="special">-</span><span class="number">3.6470772645191354362138308865549944904868234686191e+215</span> 116</pre> 117<h5> 118<a name="math_toolkit.number_series.bernoulli_numbers.h4"></a> 119 <span class="phrase"><a name="math_toolkit.number_series.bernoulli_numbers.single_unchecked_bernoulli_numbe"></a></span><a class="link" href="bernoulli_numbers.html#math_toolkit.number_series.bernoulli_numbers.single_unchecked_bernoulli_numbe">Single 120 (unchecked) Bernoulli number</a> 121 </h5> 122<h5> 123<a name="math_toolkit.number_series.bernoulli_numbers.h5"></a> 124 <span class="phrase"><a name="math_toolkit.number_series.bernoulli_numbers.synopsis0"></a></span><a class="link" href="bernoulli_numbers.html#math_toolkit.number_series.bernoulli_numbers.synopsis0">Synopsis</a> 125 </h5> 126<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">bernoulli</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 127</pre> 128<pre class="programlisting"><span class="keyword">template</span> <span class="special"><></span> 129<span class="keyword">struct</span> <span class="identifier">max_bernoulli_b2n</span><span class="special"><</span><span class="identifier">T</span><span class="special">>;</span> 130 131<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> 132<span class="keyword">inline</span> <span class="identifier">T</span> <span class="identifier">unchecked_bernoulli_b2n</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">n</span><span class="special">);</span> 133</pre> 134<p> 135 <code class="computeroutput"><span class="identifier">unchecked_bernoulli_b2n</span></code> provides 136 access to Bernoulli numbers <span class="bold"><strong>without any checks for 137 overflow or invalid parameters</strong></span>. It is implemented as a direct 138 (and very fast) table lookup, and while not recommended for general use it 139 can be useful inside inner loops where the ultimate performance is required, 140 and error checking is moved outside the loop. 141 </p> 142<p> 143 The largest value you can pass to <code class="computeroutput"><span class="identifier">unchecked_bernoulli_b2n</span><span class="special"><></span></code> is <code class="computeroutput"><span class="identifier">max_bernoulli_b2n</span><span class="special"><>::</span><span class="identifier">value</span></code>: 144 passing values greater than that will result in a buffer overrun error, so 145 it's clearly important to place the error handling in your own code when 146 using this direct interface. 147 </p> 148<p> 149 The value of <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">max_bernoulli_b2n</span><span class="special"><</span><span class="identifier">T</span><span class="special">>::</span><span class="identifier">value</span></code> varies by the type T, for types 150 <code class="computeroutput"><span class="keyword">float</span></code>/<code class="computeroutput"><span class="keyword">double</span></code>/<code class="computeroutput"><span class="keyword">long</span> <span class="keyword">double</span></code> 151 it's the largest value which doesn't overflow the target type: for example, 152 <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">max_bernoulli_b2n</span><span class="special"><</span><span class="keyword">double</span><span class="special">>::</span><span class="identifier">value</span></code> is 129. However, for multiprecision 153 types, it's the largest value for which the result can be represented as 154 the ratio of two 64-bit integers, for example <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">max_bernoulli_b2n</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_50</span><span class="special">>::</span><span class="identifier">value</span></code> 155 is just 17. Of course larger indexes can be passed to <code class="computeroutput"><span class="identifier">bernoulli_b2n</span><span class="special"><</span><span class="identifier">T</span><span class="special">>(</span><span class="identifier">n</span><span class="special">)</span></code>, but 156 then you lose fast table lookup (i.e. values may need to be calculated). 157 </p> 158<pre class="programlisting"><span class="comment">/*For example: 159*/</span> 160 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"boost::math::max_bernoulli_b2n<float>::value = "</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">max_bernoulli_b2n</span><span class="special"><</span><span class="keyword">float</span><span class="special">>::</span><span class="identifier">value</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 161 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Maximum Bernoulli number using float is "</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">bernoulli_b2n</span><span class="special"><</span><span class="keyword">float</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">max_bernoulli_b2n</span><span class="special"><</span><span class="keyword">float</span><span class="special">>::</span><span class="identifier">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> 162 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"boost::math::max_bernoulli_b2n<double>::value = "</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">max_bernoulli_b2n</span><span class="special"><</span><span class="keyword">double</span><span class="special">>::</span><span class="identifier">value</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 163 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Maximum Bernoulli number using double is "</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">bernoulli_b2n</span><span class="special"><</span><span class="keyword">double</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">max_bernoulli_b2n</span><span class="special"><</span><span class="keyword">double</span><span class="special">>::</span><span class="identifier">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> 164</pre> 165<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">max_bernoulli_b2n</span><span class="special"><</span><span class="keyword">float</span><span class="special">>::</span><span class="identifier">value</span> <span class="special">=</span> <span class="number">32</span> 166<span class="identifier">Maximum</span> <span class="identifier">Bernoulli</span> <span class="identifier">number</span> <span class="keyword">using</span> <span class="keyword">float</span> <span class="identifier">is</span> <span class="special">-</span><span class="number">2.0938e+038</span> 167<span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">max_bernoulli_b2n</span><span class="special"><</span><span class="keyword">double</span><span class="special">>::</span><span class="identifier">value</span> <span class="special">=</span> <span class="number">129</span> 168<span class="identifier">Maximum</span> <span class="identifier">Bernoulli</span> <span class="identifier">number</span> <span class="keyword">using</span> <span class="keyword">double</span> <span class="identifier">is</span> <span class="number">1.33528e+306</span> 169</pre> 170<h5> 171<a name="math_toolkit.number_series.bernoulli_numbers.h6"></a> 172 <span class="phrase"><a name="math_toolkit.number_series.bernoulli_numbers.multiple_bernoulli_numbers"></a></span><a class="link" href="bernoulli_numbers.html#math_toolkit.number_series.bernoulli_numbers.multiple_bernoulli_numbers">Multiple 173 Bernoulli Numbers</a> 174 </h5> 175<h5> 176<a name="math_toolkit.number_series.bernoulli_numbers.h7"></a> 177 <span class="phrase"><a name="math_toolkit.number_series.bernoulli_numbers.synopsis1"></a></span><a class="link" href="bernoulli_numbers.html#math_toolkit.number_series.bernoulli_numbers.synopsis1">Synopsis</a> 178 </h5> 179<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">bernoulli</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 180</pre> 181<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">math</span> <span class="special">{</span> 182 183<span class="comment">// Multiple Bernoulli numbers (default policy).</span> 184<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">OutputIterator</span><span class="special">></span> 185<span class="identifier">OutputIterator</span> <span class="identifier">bernoulli_b2n</span><span class="special">(</span> 186 <span class="keyword">int</span> <span class="identifier">start_index</span><span class="special">,</span> 187 <span class="keyword">unsigned</span> <span class="identifier">number_of_bernoullis_b2n</span><span class="special">,</span> 188 <span class="identifier">OutputIterator</span> <span class="identifier">out_it</span><span class="special">);</span> 189 190<span class="comment">// Multiple Bernoulli numbers (user policy).</span> 191<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">OutputIterator</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Policy</span><span class="special">></span> 192<span class="identifier">OutputIterator</span> <span class="identifier">bernoulli_b2n</span><span class="special">(</span> 193 <span class="keyword">int</span> <span class="identifier">start_index</span><span class="special">,</span> 194 <span class="keyword">unsigned</span> <span class="identifier">number_of_bernoullis_b2n</span><span class="special">,</span> 195 <span class="identifier">OutputIterator</span> <span class="identifier">out_it</span><span class="special">,</span> 196 <span class="keyword">const</span> <span class="identifier">Policy</span><span class="special">&</span> <span class="identifier">pol</span><span class="special">);</span> 197<span class="special">}}</span> <span class="comment">// namespaces</span> 198</pre> 199<h5> 200<a name="math_toolkit.number_series.bernoulli_numbers.h8"></a> 201 <span class="phrase"><a name="math_toolkit.number_series.bernoulli_numbers.description0"></a></span><a class="link" href="bernoulli_numbers.html#math_toolkit.number_series.bernoulli_numbers.description0">Description</a> 202 </h5> 203<p> 204 Two versions of the Bernoulli number function are provided to compute multiple 205 Bernoulli numbers with one call (one with default policy and the other allowing 206 a user-defined policy). 207 </p> 208<p> 209 These return a series of Bernoulli numbers: 210 </p> 211<div class="blockquote"><blockquote class="blockquote"><p> 212 <span class="serif_italic">[B<sub>2*start_index</sub>, B<sub>2*(start_index+1)</sub>, ..., B<sub>2*(start_index+number_of_bernoullis_b2n-1)</sub>]</span> 213 </p></blockquote></div> 214<h5> 215<a name="math_toolkit.number_series.bernoulli_numbers.h9"></a> 216 <span class="phrase"><a name="math_toolkit.number_series.bernoulli_numbers.examples0"></a></span><a class="link" href="bernoulli_numbers.html#math_toolkit.number_series.bernoulli_numbers.examples0">Examples</a> 217 </h5> 218<p> 219 We can compute and save all the float-precision Bernoulli numbers from one 220 call. 221 </p> 222<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="keyword">float</span><span class="special">></span> <span class="identifier">bn</span><span class="special">;</span> <span class="comment">// Space for 32-bit `float` precision Bernoulli numbers.</span> 223 224<span class="comment">// Start with Bernoulli number 0.</span> 225<span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">bernoulli_b2n</span><span class="special"><</span><span class="keyword">float</span><span class="special">>(</span><span class="number">0</span><span class="special">,</span> <span class="number">32</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">back_inserter</span><span class="special">(</span><span class="identifier">bn</span><span class="special">));</span> <span class="comment">// Fill vector with even Bernoulli numbers.</span> 226 227<span class="keyword">for</span><span class="special">(</span><span class="identifier">size_t</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special"><</span> <span class="identifier">bn</span><span class="special">.</span><span class="identifier">size</span><span class="special">();</span> <span class="identifier">i</span><span class="special">++)</span> 228<span class="special">{</span> <span class="comment">// Show vector of even Bernoulli numbers, showing all significant decimal digits.</span> 229 <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">setprecision</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="keyword">float</span><span class="special">>::</span><span class="identifier">digits10</span><span class="special">)</span> 230 <span class="special"><<</span> <span class="identifier">i</span><span class="special">*</span><span class="number">2</span> <span class="special"><<</span> <span class="char">' '</span> 231 <span class="special"><<</span> <span class="identifier">bn</span><span class="special">[</span><span class="identifier">i</span><span class="special">]</span> 232 <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="special">}</span> 234</pre> 235<pre class="programlisting"><span class="number">0</span> <span class="number">1</span> 236<span class="number">2</span> <span class="number">0.166667</span> 237<span class="number">4</span> <span class="special">-</span><span class="number">0.0333333</span> 238<span class="number">6</span> <span class="number">0.0238095</span> 239<span class="number">8</span> <span class="special">-</span><span class="number">0.0333333</span> 240<span class="number">10</span> <span class="number">0.0757576</span> 241<span class="number">12</span> <span class="special">-</span><span class="number">0.253114</span> 242<span class="number">14</span> <span class="number">1.16667</span> 243<span class="number">16</span> <span class="special">-</span><span class="number">7.09216</span> 244<span class="number">18</span> <span class="number">54.9712</span> 245<span class="number">20</span> <span class="special">-</span><span class="number">529.124</span> 246<span class="number">22</span> <span class="number">6192.12</span> 247<span class="number">24</span> <span class="special">-</span><span class="number">86580.3</span> 248<span class="number">26</span> <span class="number">1.42552e+006</span> 249<span class="number">28</span> <span class="special">-</span><span class="number">2.72982e+007</span> 250<span class="number">30</span> <span class="number">6.01581e+008</span> 251<span class="number">32</span> <span class="special">-</span><span class="number">1.51163e+010</span> 252<span class="number">34</span> <span class="number">4.29615e+011</span> 253<span class="number">36</span> <span class="special">-</span><span class="number">1.37117e+013</span> 254<span class="number">38</span> <span class="number">4.88332e+014</span> 255<span class="number">40</span> <span class="special">-</span><span class="number">1.92966e+016</span> 256<span class="number">42</span> <span class="number">8.41693e+017</span> 257<span class="number">44</span> <span class="special">-</span><span class="number">4.03381e+019</span> 258<span class="number">46</span> <span class="number">2.11507e+021</span> 259<span class="number">48</span> <span class="special">-</span><span class="number">1.20866e+023</span> 260<span class="number">50</span> <span class="number">7.50087e+024</span> 261<span class="number">52</span> <span class="special">-</span><span class="number">5.03878e+026</span> 262<span class="number">54</span> <span class="number">3.65288e+028</span> 263<span class="number">56</span> <span class="special">-</span><span class="number">2.84988e+030</span> 264<span class="number">58</span> <span class="number">2.38654e+032</span> 265<span class="number">60</span> <span class="special">-</span><span class="number">2.14e+034</span> 266<span class="number">62</span> <span class="number">2.0501e+036</span> 267</pre> 268<p> 269 Of course, for any floating-point type, there is a maximum Bernoulli number 270 that can be computed before it overflows the exponent. By default policy, 271 if we try to compute too high a Bernoulli number, an exception will be thrown. 272 </p> 273<pre class="programlisting"><span class="keyword">try</span> 274<span class="special">{</span> 275 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> 276 <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">setprecision</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="keyword">float</span><span class="special">>::</span><span class="identifier">digits10</span><span class="special">)</span> 277 <span class="special"><<</span> <span class="string">"Bernoulli number "</span> <span class="special"><<</span> <span class="number">33</span> <span class="special">*</span> <span class="number">2</span> <span class="special"><<</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 278 279 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</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">bernoulli_b2n</span><span class="special"><</span><span class="keyword">float</span><span class="special">>(</span><span class="number">33</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> 280<span class="special">}</span> 281<span class="keyword">catch</span> <span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception</span> <span class="identifier">ex</span><span class="special">)</span> 282<span class="special">{</span> 283 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Thrown Exception caught: "</span> <span class="special"><<</span> <span class="identifier">ex</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> 284<span class="special">}</span> 285</pre> 286<p> 287 and we will get a helpful error message (provided try'n'catch blocks are 288 used). 289 </p> 290<pre class="programlisting"><span class="identifier">Bernoulli</span> <span class="identifier">number</span> <span class="number">66</span> 291<span class="identifier">Thrown</span> <span class="identifier">Exception</span> <span class="identifier">caught</span><span class="special">:</span> <span class="identifier">Error</span> <span class="identifier">in</span> <span class="identifier">function</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">bernoulli_b2n</span><span class="special"><</span><span class="keyword">float</span><span class="special">>(</span><span class="identifier">n</span><span class="special">):</span> 292<span class="identifier">Overflow</span> <span class="identifier">evaluating</span> <span class="identifier">function</span> <span class="identifier">at</span> <span class="number">33</span> 293</pre> 294<p> 295 The source of this example is at <a href="../../../../example/bernoulli_example.cpp" target="_top">bernoulli_example.cpp</a> 296 </p> 297<h5> 298<a name="math_toolkit.number_series.bernoulli_numbers.h10"></a> 299 <span class="phrase"><a name="math_toolkit.number_series.bernoulli_numbers.accuracy"></a></span><a class="link" href="bernoulli_numbers.html#math_toolkit.number_series.bernoulli_numbers.accuracy">Accuracy</a> 300 </h5> 301<p> 302 All the functions usually return values within one ULP (unit in the last 303 place) for the floating-point type. 304 </p> 305<h5> 306<a name="math_toolkit.number_series.bernoulli_numbers.h11"></a> 307 <span class="phrase"><a name="math_toolkit.number_series.bernoulli_numbers.implementation"></a></span><a class="link" href="bernoulli_numbers.html#math_toolkit.number_series.bernoulli_numbers.implementation">Implementation</a> 308 </h5> 309<p> 310 The implementation details are in <a href="../../../../include/boost/math/special_functions/detail/bernoulli_details.hpp" target="_top">bernoulli_details.hpp</a> 311 and <a href="../../../../include/boost/math/special_functions/detail/unchecked_bernoulli.hpp" target="_top">unchecked_bernoulli.hpp</a>. 312 </p> 313<p> 314 For <code class="computeroutput"><span class="identifier">i</span> <span class="special"><=</span> 315 <span class="identifier">max_bernoulli_index</span><span class="special"><</span><span class="identifier">T</span><span class="special">>::</span><span class="identifier">value</span></code> this is implemented by simple table 316 lookup from a statically initialized table; for larger values of <code class="computeroutput"><span class="identifier">i</span></code>, this is implemented by the Tangent Numbers 317 algorithm as described in the paper: Fast Computation of Bernoulli, Tangent 318 and Secant Numbers, Richard P. Brent and David Harvey, <a href="http://arxiv.org/pdf/1108.0286v3.pdf" target="_top">http://arxiv.org/pdf/1108.0286v3.pdf</a> 319 (2011). 320 </p> 321<p> 322 <a href="http://mathworld.wolfram.com/TangentNumber.html" target="_top">Tangent (or 323 Zag) numbers</a> (an even alternating permutation number) are defined 324 and their generating function is also given therein. 325 </p> 326<p> 327 The relation of Tangent numbers with Bernoulli numbers <span class="emphasis"><em>B<sub>i</sub></em></span> 328 is given by Brent and Harvey's equation 14: 329 </p> 330<p> 331 332 </p> 333<div class="blockquote"><blockquote class="blockquote"><p> 334 <span class="inlinemediaobject"><img src="../../../equations/tangent_numbers.svg"></span> 335 336 </p></blockquote></div> 337<p> 338 Their relation with Bernoulli numbers <span class="emphasis"><em>B<sub>i</sub></em></span> are defined 339 by 340 </p> 341<p> 342 if i > 0 and i is even then 343 </p> 344<div class="blockquote"><blockquote class="blockquote"><p> 345 <span class="inlinemediaobject"><img src="../../../equations/bernoulli_numbers.svg"></span> 346 347 </p></blockquote></div> 348<p> 349 <br> elseif i == 0 then <span class="emphasis"><em>B<sub>i</sub></em></span> = 1 <br> elseif i == 350 1 then <span class="emphasis"><em>B<sub>i</sub></em></span> = -1/2 <br> elseif i < 0 or i is odd 351 then <span class="emphasis"><em>B<sub>i</sub></em></span> = 0 352 </p> 353<p> 354 Note that computed values are stored in a fixed-size table, access is thread 355 safe via atomic operations (i.e. lock free programming), this imparts a much 356 lower overhead on access to cached values than might otherwise be expected 357 - typically for multiprecision types the cost of thread synchronisation is 358 negligible, while for built in types this code is not normally executed anyway. 359 For very large arguments which cannot be reasonably computed or stored in 360 our cache, an asymptotic expansion <a href="http://www.luschny.de/math/primes/bernincl.html" target="_top">due 361 to Luschny</a> is used: 362 </p> 363<div class="blockquote"><blockquote class="blockquote"><p> 364 <span class="inlinemediaobject"><img src="../../../equations/bernoulli_numbers2.svg"></span> 365 366 </p></blockquote></div> 367</div> 368<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 369<td align="left"></td> 370<td align="right"><div class="copyright-footer">Copyright © 2006-2019 Nikhar 371 Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, 372 Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan 373 Råde, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, 374 Daryle Walker and Xiaogang Zhang<p> 375 Distributed under the Boost Software License, Version 1.0. (See accompanying 376 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>) 377 </p> 378</div></td> 379</tr></table> 380<hr> 381<div class="spirit-nav"> 382<a accesskey="p" href="../number_series.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../number_series.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="tangent_numbers.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 383</div> 384</body> 385</html> 386