3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>Double Factorial</title>
5 <link rel="stylesheet" href="../../math.css" type="text/css">
6 <meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
7 <link rel="home" href="../../index.html" title="Math Toolkit 2.1.0">
8 <link rel="up" href="../factorials.html" title="Factorials and Binomial Coefficients">
9 <link rel="prev" href="sf_factorial.html" title="Factorial">
10 <link rel="next" href="sf_rising_factorial.html" title="Rising Factorial">
12 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13 <table cellpadding="2" width="100%"><tr>
14 <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
15 <td align="center"><a href="../../../../../../index.html">Home</a></td>
16 <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
17 <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18 <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19 <td align="center"><a href="../../../../../../more/index.htm">More</a></td>
22 <div class="spirit-nav">
23 <a accesskey="p" href="sf_factorial.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../factorials.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="sf_rising_factorial.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
26 <div class="titlepage"><div><div><h3 class="title">
27 <a name="math_toolkit.factorials.sf_double_factorial"></a><a class="link" href="sf_double_factorial.html" title="Double Factorial">Double Factorial</a>
28 </h3></div></div></div>
29 <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">factorials</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
31 <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>
33 <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span>
34 <span class="identifier">T</span> <span class="identifier">double_factorial</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">i</span><span class="special">);</span>
36 <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> <a class="link" href="../../policy.html" title="Chapter 14. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">></span>
37 <span class="identifier">T</span> <span class="identifier">double_factorial</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">i</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../../policy.html" title="Chapter 14. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&);</span>
39 <span class="special">}}</span> <span class="comment">// namespaces</span>
42 Returns <code class="literal">i!!</code>.
45 The final <a class="link" href="../../policy.html" title="Chapter 14. Policies: Controlling Precision, Error Handling etc">Policy</a> argument is optional and can
46 be used to control the behaviour of the function: how it handles errors,
47 what level of precision to use etc. Refer to the <a class="link" href="../../policy.html" title="Chapter 14. Policies: Controlling Precision, Error Handling etc">policy
48 documentation for more details</a>.
51 May return the result of <a class="link" href="../error_handling.html#math_toolkit.error_handling.overflow_error">overflow_error</a>
52 if the result is too large to represent in type T. The implementation is
53 designed to be optimised for small <span class="emphasis"><em>i</em></span> where table lookup
56 <div class="important"><table border="0" summary="Important">
58 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../../../doc/src/images/important.png"></td>
59 <th align="left">Important</th>
61 <tr><td align="left" valign="top">
63 The functions described above are templates where the template argument
64 T can not be deduced from the arguments passed to the function. Therefore
65 if you write something like:
68 <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">double_factorial</span><span class="special">(</span><span class="number">2</span><span class="special">);</span></code>
71 You will get a (possibly perplexing) compiler error, ususally indicating
72 that there is no such function to be found. Instead you need to specifiy
73 the return type explicity and write:
76 <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">double_factorial</span><span class="special"><</span><span class="keyword">double</span><span class="special">>(</span><span class="number">2</span><span class="special">);</span></code>
79 So that the return type is known. Further, the template argument must be
80 a real-valued type such as <code class="computeroutput"><span class="keyword">float</span></code>
81 or <code class="computeroutput"><span class="keyword">double</span></code> and not an integer
82 type - that would overflow far too easily!
85 The source code <code class="computeroutput"><span class="keyword">static_assert</span></code>
86 and comment just after the will be:
88 <pre class="programlisting"><span class="identifier">BOOST_STATIC_ASSERT</span><span class="special">(!</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">is_integral</span><span class="special"><</span><span class="identifier">T</span><span class="special">>::</span><span class="identifier">value</span><span class="special">);</span>
89 <span class="comment">// factorial<unsigned int>(n) is not implemented</span>
90 <span class="comment">// because it would overflow integral type T for too small n</span>
91 <span class="comment">// to be useful. Use instead a floating-point type,</span>
92 <span class="comment">// and convert to an unsigned type if essential, for example:</span>
93 <span class="comment">// unsigned int nfac = static_cast<unsigned int>(factorial<double>(n));</span>
94 <span class="comment">// See factorial documentation for more detail.</span>
98 <div class="note"><table border="0" summary="Note">
100 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td>
101 <th align="left">Note</th>
103 <tr><td align="left" valign="top"><p>
104 The argument to <code class="computeroutput"><span class="identifier">double_factorial</span></code>
105 is type <code class="computeroutput"><span class="keyword">unsigned</span></code> even though
106 technically -1!! is defined.
110 <a name="math_toolkit.factorials.sf_double_factorial.h0"></a>
111 <span class="phrase"><a name="math_toolkit.factorials.sf_double_factorial.accuracy"></a></span><a class="link" href="sf_double_factorial.html#math_toolkit.factorials.sf_double_factorial.accuracy">Accuracy</a>
114 The implementation uses a trivial adaptation of the factorial function, so
115 error rates should be no more than a couple of epsilon higher.
118 <a name="math_toolkit.factorials.sf_double_factorial.h1"></a>
119 <span class="phrase"><a name="math_toolkit.factorials.sf_double_factorial.testing"></a></span><a class="link" href="sf_double_factorial.html#math_toolkit.factorials.sf_double_factorial.testing">Testing</a>
122 The spot tests for the double factorial use data generated by functions.wolfram.com.
125 <a name="math_toolkit.factorials.sf_double_factorial.h2"></a>
126 <span class="phrase"><a name="math_toolkit.factorials.sf_double_factorial.implementation"></a></span><a class="link" href="sf_double_factorial.html#math_toolkit.factorials.sf_double_factorial.implementation">Implementation</a>
129 The double factorial is implemented in terms of the factorial and gamma functions
133 (2n)!! = 2<sup>n </sup> * n!
136 (2n+1)!! = (2n+1)! / (2<sup>n </sup> n!)
142 (2n-1)!! = Γ((2n+1)/2) * 2<sup>n </sup> / sqrt(pi)
145 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
146 <td align="left"></td>
147 <td align="right"><div class="copyright-footer">Copyright © 2006-2010, 2012-2014 Nikhar Agrawal,
148 Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert
149 Holin, Bruno Lalande, John Maddock, Johan Råde, Gautam Sewani, Benjamin Sobotta,
150 Thijs van den Berg, Daryle Walker and Xiaogang Zhang<p>
151 Distributed under the Boost Software License, Version 1.0. (See accompanying
152 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>)
157 <div class="spirit-nav">
158 <a accesskey="p" href="sf_factorial.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../factorials.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="sf_rising_factorial.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>