1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Double Factorial</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="../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"> 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="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> 24</div> 25<div class="section"> 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> 30</pre> 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> 32 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> 35 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 21. 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 21. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&);</span> 38 39<span class="special">}}</span> <span class="comment">// namespaces</span> 40</pre> 41<p> 42 Returns <code class="literal">i!!</code>. 43 </p> 44<p> 45 The final <a class="link" href="../../policy.html" title="Chapter 21. 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 21. Policies: Controlling Precision, Error Handling etc">policy 48 documentation for more details</a>. 49 </p> 50<p> 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 54 of i! is possible. 55 </p> 56<div class="important"><table border="0" summary="Important"> 57<tr> 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> 60</tr> 61<tr><td align="left" valign="top"> 62<p> 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: 66 </p> 67<p> 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> 69 </p> 70<p> 71 You will get a (possibly perplexing) compiler error, usually indicating 72 that there is no such function to be found. Instead you need to specify 73 the return type explicitly and write: 74 </p> 75<p> 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> 77 </p> 78<p> 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! 83 </p> 84<p> 85 The source code <code class="computeroutput"><span class="keyword">static_assert</span></code> 86 and comment just after the will be: 87 </p> 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> 95</pre> 96</td></tr> 97</table></div> 98<div class="note"><table border="0" summary="Note"> 99<tr> 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> 102</tr> 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. 107 </p></td></tr> 108</table></div> 109<h5> 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> 112 </h5> 113<p> 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. 116 </p> 117<h5> 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> 120 </h5> 121<p> 122 The spot tests for the double factorial use data generated by <a href="http://www.wolframalpha.com/" target="_top">Wolfram 123 Alpha</a>. 124 </p> 125<h5> 126<a name="math_toolkit.factorials.sf_double_factorial.h2"></a> 127 <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> 128 </h5> 129<p> 130 The double factorial is implemented in terms of the factorial and gamma functions 131 using the relations: 132 </p> 133<div class="blockquote"><blockquote class="blockquote"><p> 134 <span class="serif_italic"><span class="emphasis"><em>(2n)!! = 2<sup>n </sup> * n!</em></span></span> 135 </p></blockquote></div> 136<div class="blockquote"><blockquote class="blockquote"><p> 137 <span class="serif_italic"><span class="emphasis"><em>(2n+1)!! = (2n+1)! / (2<sup>n </sup> n!)</em></span></span> 138 </p></blockquote></div> 139<p> 140 and 141 </p> 142<div class="blockquote"><blockquote class="blockquote"><p> 143 <span class="serif_italic"><span class="emphasis"><em>(2n-1)!! = Γ((2n+1)/2) * 2<sup>n </sup> / sqrt(pi)</em></span></span> 144 </p></blockquote></div> 145</div> 146<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 147<td align="left"></td> 148<td align="right"><div class="copyright-footer">Copyright © 2006-2019 Nikhar 149 Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, 150 Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan 151 Råde, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, 152 Daryle Walker and Xiaogang Zhang<p> 153 Distributed under the Boost Software License, Version 1.0. (See accompanying 154 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>) 155 </p> 156</div></td> 157</tr></table> 158<hr> 159<div class="spirit-nav"> 160<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> 161</div> 162</body> 163</html> 164