]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | <html> |
2 | <head> | |
3 | <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> | |
4 | <title>Factorial</title> | |
5 | <link rel="stylesheet" href="../../math.css" type="text/css"> | |
6 | <meta name="generator" content="DocBook XSL Stylesheets V1.77.1"> | |
7 | <link rel="home" href="../../index.html" title="Math Toolkit 2.5.1"> | |
8 | <link rel="up" href="../factorials.html" title="Factorials and Binomial Coefficients"> | |
9 | <link rel="prev" href="../factorials.html" title="Factorials and Binomial Coefficients"> | |
10 | <link rel="next" href="sf_double_factorial.html" title="Double 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="../factorials.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_double_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_factorial"></a><a class="link" href="sf_factorial.html" title="Factorial">Factorial</a> | |
28 | </h3></div></div></div> | |
29 | <h5> | |
30 | <a name="math_toolkit.factorials.sf_factorial.h0"></a> | |
31 | <span class="phrase"><a name="math_toolkit.factorials.sf_factorial.synopsis"></a></span><a class="link" href="sf_factorial.html#math_toolkit.factorials.sf_factorial.synopsis">Synopsis</a> | |
32 | </h5> | |
33 | <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> | |
34 | </pre> | |
35 | <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> | |
36 | ||
37 | <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> | |
38 | <span class="identifier">T</span> <span class="identifier">factorial</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">i</span><span class="special">);</span> | |
39 | ||
40 | <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 15. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">></span> | |
41 | <span class="identifier">T</span> <span class="identifier">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 15. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&);</span> | |
42 | ||
43 | <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> | |
44 | <span class="identifier">T</span> <span class="identifier">unchecked_factorial</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">i</span><span class="special">);</span> | |
45 | ||
46 | <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> | |
47 | <span class="keyword">struct</span> <span class="identifier">max_factorial</span><span class="special">;</span> | |
48 | ||
49 | <span class="special">}}</span> <span class="comment">// namespaces</span> | |
50 | </pre> | |
51 | <h5> | |
52 | <a name="math_toolkit.factorials.sf_factorial.h1"></a> | |
53 | <span class="phrase"><a name="math_toolkit.factorials.sf_factorial.description"></a></span><a class="link" href="sf_factorial.html#math_toolkit.factorials.sf_factorial.description">Description</a> | |
54 | </h5> | |
55 | <div class="important"><table border="0" summary="Important"> | |
56 | <tr> | |
57 | <td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../../../doc/src/images/important.png"></td> | |
58 | <th align="left">Important</th> | |
59 | </tr> | |
60 | <tr><td align="left" valign="top"> | |
61 | <p> | |
62 | The functions described below are templates where the template argument | |
63 | T CANNOT be deduced from the arguments passed to the function. Therefore | |
64 | if you write something like: | |
65 | </p> | |
66 | <p> | |
67 | <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">factorial</span><span class="special">(</span><span class="number">2</span><span class="special">);</span></code> | |
68 | </p> | |
69 | <p> | |
70 | You will get a (perhaps perplexing) compiler error, ususally indicating | |
71 | that there is no such function to be found. Instead you need to specify | |
72 | the return type explicity and write: | |
73 | </p> | |
74 | <p> | |
75 | <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">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> | |
76 | </p> | |
77 | <p> | |
78 | So that the return type is known. | |
79 | </p> | |
80 | <p> | |
81 | Furthermore, the template argument must be a real-valued type such as | |
82 | <code class="computeroutput"><span class="keyword">float</span></code> or <code class="computeroutput"><span class="keyword">double</span></code> | |
83 | and not an integer type - that would overflow far too easily for quite | |
84 | small values of parameter <code class="computeroutput"><span class="identifier">i</span></code>! | |
85 | </p> | |
86 | <p> | |
87 | The source code <code class="computeroutput"><span class="keyword">static_assert</span></code> | |
88 | and comment just after the will be: | |
89 | </p> | |
90 | <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> | |
91 | <span class="comment">// factorial<unsigned int>(n) is not implemented</span> | |
92 | <span class="comment">// because it would overflow integral type T for too small n</span> | |
93 | <span class="comment">// to be useful. Use instead a floating-point type,</span> | |
94 | <span class="comment">// and convert to an unsigned type if essential, for example:</span> | |
95 | <span class="comment">// unsigned int nfac = static_cast<unsigned int>(factorial<double>(n));</span> | |
96 | <span class="comment">// See factorial documentation for more detail.</span> | |
97 | </pre> | |
98 | </td></tr> | |
99 | </table></div> | |
100 | <pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> | |
101 | <span class="identifier">T</span> <span class="identifier">factorial</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">i</span><span class="special">);</span> | |
102 | ||
103 | <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 15. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">></span> | |
104 | <span class="identifier">T</span> <span class="identifier">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 15. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&);</span> | |
105 | </pre> | |
106 | <p> | |
107 | Returns <code class="literal">i!</code>. | |
108 | </p> | |
109 | <p> | |
110 | The final <a class="link" href="../../policy.html" title="Chapter 15. Policies: Controlling Precision, Error Handling etc">Policy</a> argument is optional and can | |
111 | be used to control the behaviour of the function: how it handles errors, | |
112 | what level of precision to use etc. Refer to the <a class="link" href="../../policy.html" title="Chapter 15. Policies: Controlling Precision, Error Handling etc">policy | |
113 | documentation for more details</a>. | |
114 | </p> | |
115 | <p> | |
116 | For <code class="literal">i <= max_factorial<T>::value</code> this is implemented | |
117 | by table lookup, for larger values of <code class="literal">i</code>, this function | |
118 | is implemented in terms of <a class="link" href="../sf_gamma/tgamma.html" title="Gamma">tgamma</a>. | |
119 | </p> | |
120 | <p> | |
121 | If <code class="literal">i</code> is so large that the result can not be represented | |
122 | in type T, then calls <a class="link" href="../error_handling.html#math_toolkit.error_handling.overflow_error">overflow_error</a>. | |
123 | </p> | |
124 | <pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> | |
125 | <span class="identifier">T</span> <span class="identifier">unchecked_factorial</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">i</span><span class="special">);</span> | |
126 | </pre> | |
127 | <p> | |
128 | Returns <code class="literal">i!</code>. | |
129 | </p> | |
130 | <p> | |
131 | Internally this function performs table lookup of the result. Further it | |
132 | performs no range checking on the value of i: it is up to the caller to ensure | |
133 | that <code class="literal">i <= max_factorial<T>::value</code>. This function | |
134 | is intended to be used inside inner loops that require fast table lookup | |
135 | of factorials, but requires care to ensure that argument <code class="literal">i</code> | |
136 | never grows too large. | |
137 | </p> | |
138 | <pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> | |
139 | <span class="keyword">struct</span> <span class="identifier">max_factorial</span> | |
140 | <span class="special">{</span> | |
141 | <span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">unsigned</span> <span class="identifier">value</span> <span class="special">=</span> <span class="identifier">X</span><span class="special">;</span> | |
142 | <span class="special">};</span> | |
143 | </pre> | |
144 | <p> | |
145 | This traits class defines the largest value that can be passed to <code class="literal">unchecked_factorial</code>. | |
146 | The member <code class="computeroutput"><span class="identifier">value</span></code> can be used | |
147 | where integral constant expressions are required: for example to define the | |
148 | size of further tables that depend on the factorials. | |
149 | </p> | |
150 | <h5> | |
151 | <a name="math_toolkit.factorials.sf_factorial.h2"></a> | |
152 | <span class="phrase"><a name="math_toolkit.factorials.sf_factorial.accuracy"></a></span><a class="link" href="sf_factorial.html#math_toolkit.factorials.sf_factorial.accuracy">Accuracy</a> | |
153 | </h5> | |
154 | <p> | |
155 | For arguments smaller than <code class="computeroutput"><span class="identifier">max_factorial</span><span class="special"><</span><span class="identifier">T</span><span class="special">>::</span><span class="identifier">value</span></code> the result should be correctly rounded. | |
156 | For larger arguments the accuracy will be the same as for <a class="link" href="../sf_gamma/tgamma.html" title="Gamma">tgamma</a>. | |
157 | </p> | |
158 | <h5> | |
159 | <a name="math_toolkit.factorials.sf_factorial.h3"></a> | |
160 | <span class="phrase"><a name="math_toolkit.factorials.sf_factorial.testing"></a></span><a class="link" href="sf_factorial.html#math_toolkit.factorials.sf_factorial.testing">Testing</a> | |
161 | </h5> | |
162 | <p> | |
163 | Basic sanity checks and spot values to verify the data tables: the main tests | |
164 | for the <a class="link" href="../sf_gamma/tgamma.html" title="Gamma">tgamma</a> function | |
165 | handle those cases already. | |
166 | </p> | |
167 | <h5> | |
168 | <a name="math_toolkit.factorials.sf_factorial.h4"></a> | |
169 | <span class="phrase"><a name="math_toolkit.factorials.sf_factorial.implementation"></a></span><a class="link" href="sf_factorial.html#math_toolkit.factorials.sf_factorial.implementation">Implementation</a> | |
170 | </h5> | |
171 | <p> | |
172 | The factorial function is table driven for small arguments, and is implemented | |
173 | in terms of <a class="link" href="../sf_gamma/tgamma.html" title="Gamma">tgamma</a> for | |
174 | larger arguments. | |
175 | </p> | |
176 | </div> | |
177 | <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> | |
178 | <td align="left"></td> | |
179 | <td align="right"><div class="copyright-footer">Copyright © 2006-2010, 2012-2014 Nikhar Agrawal, | |
180 | Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert | |
181 | Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Johan Råde, Gautam Sewani, | |
182 | Benjamin Sobotta, Thijs van den Berg, Daryle Walker and Xiaogang Zhang<p> | |
183 | Distributed under the Boost Software License, Version 1.0. (See accompanying | |
184 | 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>) | |
185 | </p> | |
186 | </div></td> | |
187 | </tr></table> | |
188 | <hr> | |
189 | <div class="spirit-nav"> | |
190 | <a accesskey="p" href="../factorials.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_double_factorial.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> | |
191 | </div> | |
192 | </body> | |
193 | </html> |