math/doc/html/math_toolkit/number_series/bernoulli_numbers.html
2019-10-31 17:55:35 +00:00

386 lines
40 KiB
HTML

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Bernoulli Numbers</title>
<link rel="stylesheet" href="../../math.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Math Toolkit 2.11.0">
<link rel="up" href="../number_series.html" title="Number Series">
<link rel="prev" href="../number_series.html" title="Number Series">
<link rel="next" href="tangent_numbers.html" title="Tangent Numbers">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../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>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="math_toolkit.number_series.bernoulli_numbers"></a><a class="link" href="bernoulli_numbers.html" title="Bernoulli Numbers">Bernoulli
Numbers</a>
</h3></div></div></div>
<p>
<a href="https://en.wikipedia.org/wiki/Bernoulli_number" target="_top">Bernoulli numbers</a>
are a sequence of rational numbers useful for the Taylor series expansion,
Euler-Maclaurin formula, and the Riemann zeta function.
</p>
<p>
Bernoulli numbers are used in evaluation of some Boost.Math functions, including
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>
and polygamma functions.
</p>
<h5>
<a name="math_toolkit.number_series.bernoulli_numbers.h0"></a>
<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
Bernoulli number</a>
</h5>
<h5>
<a name="math_toolkit.number_series.bernoulli_numbers.h1"></a>
<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>
</h5>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span>
</pre>
<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>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
<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>
<span class="keyword">template</span> <span class="special">&lt;</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">&gt;</span>
<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">&amp;</span><span class="identifier">pol</span><span class="special">);</span> <span class="comment">// User policy for errors etc.</span>
<span class="special">}}</span> <span class="comment">// namespaces</span>
</pre>
<h5>
<a name="math_toolkit.number_series.bernoulli_numbers.h2"></a>
<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>
</h5>
<p>
Both return the (2 * n)<sup>th</sup> Bernoulli number B<sub>2n</sub>.
</p>
<p>
Note that since all odd numbered Bernoulli numbers are zero (apart from B<sub>1</sub> which
is -&#189;) the interface will only return the even numbered Bernoulli numbers.
</p>
<p>
This function uses fast table lookup for low-indexed Bernoulli numbers, while
larger values are calculated as needed and then cached. The caching mechanism
requires a certain amount of thread safety code, so <code class="computeroutput"><span class="identifier">unchecked_bernoulli_b2n</span></code>
may provide a better interface for performance critical code.
</p>
<p>
The final <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a> argument is optional and can
be used to control the behaviour of the function: how it handles errors,
what level of precision to use, etc.
</p>
<p>
Refer to <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policies</a> for more details.
</p>
<h5>
<a name="math_toolkit.number_series.bernoulli_numbers.h3"></a>
<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>
</h5>
<p>
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
is <span class="emphasis"><em>2</em></span> not <span class="emphasis"><em>4</em></span> since it computes B<sub>2N</sub>.
</p>
<pre class="programlisting"><span class="keyword">try</span>
<span class="special">{</span> <span class="comment">// It is always wise to use try'n'catch blocks around Boost.Math functions</span>
<span class="comment">// so that any informative error messages can be displayed in the catch block.</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span>
<span class="special">&lt;&lt;</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">&lt;</span><span class="keyword">double</span><span class="special">&gt;::</span><span class="identifier">digits10</span><span class="special">)</span>
<span class="special">&lt;&lt;</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">&lt;</span><span class="keyword">double</span><span class="special">&gt;(</span><span class="number">2</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
</pre>
<p>
So B<sub>4</sub> == -1/30 == -0.0333333333333333
</p>
<p>
If we use Boost.Multiprecision and its 50 decimal digit floating-point type
<code class="computeroutput"><span class="identifier">cpp_dec_float_50</span></code>, we can
calculate the value of much larger numbers like B<sub>200</sub>
and also obtain much
higher precision.
</p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span>
<span class="special">&lt;&lt;</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">&lt;</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">&gt;::</span><span class="identifier">digits10</span><span class="special">)</span>
<span class="special">&lt;&lt;</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">&lt;</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">&gt;(</span><span class="number">100</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
</pre>
<pre class="programlisting"><span class="special">-</span><span class="number">3.6470772645191354362138308865549944904868234686191e+215</span>
</pre>
<h5>
<a name="math_toolkit.number_series.bernoulli_numbers.h4"></a>
<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
(unchecked) Bernoulli number</a>
</h5>
<h5>
<a name="math_toolkit.number_series.bernoulli_numbers.h5"></a>
<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>
</h5>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span>
</pre>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;&gt;</span>
<span class="keyword">struct</span> <span class="identifier">max_bernoulli_b2n</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;;</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
<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>
</pre>
<p>
<code class="computeroutput"><span class="identifier">unchecked_bernoulli_b2n</span></code> provides
access to Bernoulli numbers <span class="bold"><strong>without any checks for
overflow or invalid parameters</strong></span>. It is implemented as a direct
(and very fast) table lookup, and while not recommended for general use it
can be useful inside inner loops where the ultimate performance is required,
and error checking is moved outside the loop.
</p>
<p>
The largest value you can pass to <code class="computeroutput"><span class="identifier">unchecked_bernoulli_b2n</span><span class="special">&lt;&gt;</span></code> is <code class="computeroutput"><span class="identifier">max_bernoulli_b2n</span><span class="special">&lt;&gt;::</span><span class="identifier">value</span></code>:
passing values greater than that will result in a buffer overrun error, so
it's clearly important to place the error handling in your own code when
using this direct interface.
</p>
<p>
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">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">value</span></code> varies by the type T, for types
<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>
it's the largest value which doesn't overflow the target type: 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">&lt;</span><span class="keyword">double</span><span class="special">&gt;::</span><span class="identifier">value</span></code> is 129. However, for multiprecision
types, it's the largest value for which the result can be represented as
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">&lt;</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">&gt;::</span><span class="identifier">value</span></code>
is just 17. Of course larger indexes can be passed to <code class="computeroutput"><span class="identifier">bernoulli_b2n</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;(</span><span class="identifier">n</span><span class="special">)</span></code>, but
then you lose fast table lookup (i.e. values may need to be calculated).
</p>
<pre class="programlisting"><span class="comment">/*For example:
*/</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"boost::math::max_bernoulli_b2n&lt;float&gt;::value = "</span> <span class="special">&lt;&lt;</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">&lt;</span><span class="keyword">float</span><span class="special">&gt;::</span><span class="identifier">value</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Maximum Bernoulli number using float is "</span> <span class="special">&lt;&lt;</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">&lt;</span><span class="keyword">float</span><span class="special">&gt;(</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">&lt;</span><span class="keyword">float</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"boost::math::max_bernoulli_b2n&lt;double&gt;::value = "</span> <span class="special">&lt;&lt;</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">&lt;</span><span class="keyword">double</span><span class="special">&gt;::</span><span class="identifier">value</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Maximum Bernoulli number using double is "</span> <span class="special">&lt;&lt;</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">&lt;</span><span class="keyword">double</span><span class="special">&gt;(</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">&lt;</span><span class="keyword">double</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
</pre>
<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">&lt;</span><span class="keyword">float</span><span class="special">&gt;::</span><span class="identifier">value</span> <span class="special">=</span> <span class="number">32</span>
<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>
<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">&lt;</span><span class="keyword">double</span><span class="special">&gt;::</span><span class="identifier">value</span> <span class="special">=</span> <span class="number">129</span>
<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>
</pre>
<h5>
<a name="math_toolkit.number_series.bernoulli_numbers.h6"></a>
<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
Bernoulli Numbers</a>
</h5>
<h5>
<a name="math_toolkit.number_series.bernoulli_numbers.h7"></a>
<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>
</h5>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span>
</pre>
<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>
<span class="comment">// Multiple Bernoulli numbers (default policy).</span>
<span class="keyword">template</span> <span class="special">&lt;</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">&gt;</span>
<span class="identifier">OutputIterator</span> <span class="identifier">bernoulli_b2n</span><span class="special">(</span>
<span class="keyword">int</span> <span class="identifier">start_index</span><span class="special">,</span>
<span class="keyword">unsigned</span> <span class="identifier">number_of_bernoullis_b2n</span><span class="special">,</span>
<span class="identifier">OutputIterator</span> <span class="identifier">out_it</span><span class="special">);</span>
<span class="comment">// Multiple Bernoulli numbers (user policy).</span>
<span class="keyword">template</span> <span class="special">&lt;</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">&gt;</span>
<span class="identifier">OutputIterator</span> <span class="identifier">bernoulli_b2n</span><span class="special">(</span>
<span class="keyword">int</span> <span class="identifier">start_index</span><span class="special">,</span>
<span class="keyword">unsigned</span> <span class="identifier">number_of_bernoullis_b2n</span><span class="special">,</span>
<span class="identifier">OutputIterator</span> <span class="identifier">out_it</span><span class="special">,</span>
<span class="keyword">const</span> <span class="identifier">Policy</span><span class="special">&amp;</span> <span class="identifier">pol</span><span class="special">);</span>
<span class="special">}}</span> <span class="comment">// namespaces</span>
</pre>
<h5>
<a name="math_toolkit.number_series.bernoulli_numbers.h8"></a>
<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>
</h5>
<p>
Two versions of the Bernoulli number function are provided to compute multiple
Bernoulli numbers with one call (one with default policy and the other allowing
a user-defined policy).
</p>
<p>
These return a series of Bernoulli numbers:
</p>
<div class="blockquote"><blockquote class="blockquote"><p>
<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>
</p></blockquote></div>
<h5>
<a name="math_toolkit.number_series.bernoulli_numbers.h9"></a>
<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>
</h5>
<p>
We can compute and save all the float-precision Bernoulli numbers from one
call.
</p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">float</span><span class="special">&gt;</span> <span class="identifier">bn</span><span class="special">;</span> <span class="comment">// Space for 32-bit `float` precision Bernoulli numbers.</span>
<span class="comment">// Start with Bernoulli number 0.</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">&lt;</span><span class="keyword">float</span><span class="special">&gt;(</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>
<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">&lt;</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>
<span class="special">{</span> <span class="comment">// Show vector of even Bernoulli numbers, showing all significant decimal digits.</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</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">&lt;</span><span class="keyword">float</span><span class="special">&gt;::</span><span class="identifier">digits10</span><span class="special">)</span>
<span class="special">&lt;&lt;</span> <span class="identifier">i</span><span class="special">*</span><span class="number">2</span> <span class="special">&lt;&lt;</span> <span class="char">' '</span>
<span class="special">&lt;&lt;</span> <span class="identifier">bn</span><span class="special">[</span><span class="identifier">i</span><span class="special">]</span>
<span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<pre class="programlisting"><span class="number">0</span> <span class="number">1</span>
<span class="number">2</span> <span class="number">0.166667</span>
<span class="number">4</span> <span class="special">-</span><span class="number">0.0333333</span>
<span class="number">6</span> <span class="number">0.0238095</span>
<span class="number">8</span> <span class="special">-</span><span class="number">0.0333333</span>
<span class="number">10</span> <span class="number">0.0757576</span>
<span class="number">12</span> <span class="special">-</span><span class="number">0.253114</span>
<span class="number">14</span> <span class="number">1.16667</span>
<span class="number">16</span> <span class="special">-</span><span class="number">7.09216</span>
<span class="number">18</span> <span class="number">54.9712</span>
<span class="number">20</span> <span class="special">-</span><span class="number">529.124</span>
<span class="number">22</span> <span class="number">6192.12</span>
<span class="number">24</span> <span class="special">-</span><span class="number">86580.3</span>
<span class="number">26</span> <span class="number">1.42552e+006</span>
<span class="number">28</span> <span class="special">-</span><span class="number">2.72982e+007</span>
<span class="number">30</span> <span class="number">6.01581e+008</span>
<span class="number">32</span> <span class="special">-</span><span class="number">1.51163e+010</span>
<span class="number">34</span> <span class="number">4.29615e+011</span>
<span class="number">36</span> <span class="special">-</span><span class="number">1.37117e+013</span>
<span class="number">38</span> <span class="number">4.88332e+014</span>
<span class="number">40</span> <span class="special">-</span><span class="number">1.92966e+016</span>
<span class="number">42</span> <span class="number">8.41693e+017</span>
<span class="number">44</span> <span class="special">-</span><span class="number">4.03381e+019</span>
<span class="number">46</span> <span class="number">2.11507e+021</span>
<span class="number">48</span> <span class="special">-</span><span class="number">1.20866e+023</span>
<span class="number">50</span> <span class="number">7.50087e+024</span>
<span class="number">52</span> <span class="special">-</span><span class="number">5.03878e+026</span>
<span class="number">54</span> <span class="number">3.65288e+028</span>
<span class="number">56</span> <span class="special">-</span><span class="number">2.84988e+030</span>
<span class="number">58</span> <span class="number">2.38654e+032</span>
<span class="number">60</span> <span class="special">-</span><span class="number">2.14e+034</span>
<span class="number">62</span> <span class="number">2.0501e+036</span>
</pre>
<p>
Of course, for any floating-point type, there is a maximum Bernoulli number
that can be computed before it overflows the exponent. By default policy,
if we try to compute too high a Bernoulli number, an exception will be thrown.
</p>
<pre class="programlisting"><span class="keyword">try</span>
<span class="special">{</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span>
<span class="special">&lt;&lt;</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">&lt;</span><span class="keyword">float</span><span class="special">&gt;::</span><span class="identifier">digits10</span><span class="special">)</span>
<span class="special">&lt;&lt;</span> <span class="string">"Bernoulli number "</span> <span class="special">&lt;&lt;</span> <span class="number">33</span> <span class="special">*</span> <span class="number">2</span> <span class="special">&lt;&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</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">&lt;</span><span class="keyword">float</span><span class="special">&gt;(</span><span class="number">33</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="special">}</span>
<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>
<span class="special">{</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Thrown Exception caught: "</span> <span class="special">&lt;&lt;</span> <span class="identifier">ex</span><span class="special">.</span><span class="identifier">what</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
and we will get a helpful error message (provided try'n'catch blocks are
used).
</p>
<pre class="programlisting"><span class="identifier">Bernoulli</span> <span class="identifier">number</span> <span class="number">66</span>
<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">&lt;</span><span class="keyword">float</span><span class="special">&gt;(</span><span class="identifier">n</span><span class="special">):</span>
<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>
</pre>
<p>
The source of this example is at <a href="../../../../example/bernoulli_example.cpp" target="_top">bernoulli_example.cpp</a>
</p>
<h5>
<a name="math_toolkit.number_series.bernoulli_numbers.h10"></a>
<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>
</h5>
<p>
All the functions usually return values within one ULP (unit in the last
place) for the floating-point type.
</p>
<h5>
<a name="math_toolkit.number_series.bernoulli_numbers.h11"></a>
<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>
</h5>
<p>
The implementation details are in <a href="../../../../include/boost/math/special_functions/detail/bernoulli_details.hpp" target="_top">bernoulli_details.hpp</a>
and <a href="../../../../include/boost/math/special_functions/detail/unchecked_bernoulli.hpp" target="_top">unchecked_bernoulli.hpp</a>.
</p>
<p>
For <code class="computeroutput"><span class="identifier">i</span> <span class="special">&lt;=</span>
<span class="identifier">max_bernoulli_index</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">value</span></code> this is implemented by simple table
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
algorithm as described in the paper: Fast Computation of Bernoulli, Tangent
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>
(2011).
</p>
<p>
<a href="http://mathworld.wolfram.com/TangentNumber.html" target="_top">Tangent (or
Zag) numbers</a> (an even alternating permutation number) are defined
and their generating function is also given therein.
</p>
<p>
The relation of Tangent numbers with Bernoulli numbers <span class="emphasis"><em>B<sub>i</sub></em></span>
is given by Brent and Harvey's equation 14:
</p>
<p>
&#8192;&#8192;
</p>
<div class="blockquote"><blockquote class="blockquote"><p>
<span class="inlinemediaobject"><img src="../../../equations/tangent_numbers.svg"></span>
</p></blockquote></div>
<p>
Their relation with Bernoulli numbers <span class="emphasis"><em>B<sub>i</sub></em></span> are defined
by
</p>
<p>
if i &gt; 0 and i is even then
</p>
<div class="blockquote"><blockquote class="blockquote"><p>
<span class="inlinemediaobject"><img src="../../../equations/bernoulli_numbers.svg"></span>
</p></blockquote></div>
<p>
<br> elseif i == 0 then <span class="emphasis"><em>B<sub>i</sub></em></span> = 1 <br> elseif i ==
1 then <span class="emphasis"><em>B<sub>i</sub></em></span> = -1/2 <br> elseif i &lt; 0 or i is odd
then <span class="emphasis"><em>B<sub>i</sub></em></span> = 0
</p>
<p>
Note that computed values are stored in a fixed-size table, access is thread
safe via atomic operations (i.e. lock free programming), this imparts a much
lower overhead on access to cached values than might otherwise be expected
- typically for multiprecision types the cost of thread synchronisation is
negligible, while for built in types this code is not normally executed anyway.
For very large arguments which cannot be reasonably computed or stored in
our cache, an asymptotic expansion <a href="http://www.luschny.de/math/primes/bernincl.html" target="_top">due
to Luschny</a> is used:
</p>
<div class="blockquote"><blockquote class="blockquote"><p>
<span class="inlinemediaobject"><img src="../../../equations/bernoulli_numbers2.svg"></span>
</p></blockquote></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2006-2019 Nikhar
Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos,
Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan
R&#229;de, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg,
Daryle Walker and Xiaogang Zhang<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../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>
</div>
</body>
</html>