WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
factorial.hpp
Go to the documentation of this file.
1/*################################################################################
2 ##
3 ## Copyright (C) 2016-2024 Keith O'Hara
4 ##
5 ## This file is part of the GCE-Math C++ library.
6 ##
7 ## Licensed under the Apache License, Version 2.0 (the "License");
8 ## you may not use this file except in compliance with the License.
9 ## You may obtain a copy of the License at
10 ##
11 ## http://www.apache.org/licenses/LICENSE-2.0
12 ##
13 ## Unless required by applicable law or agreed to in writing, software
14 ## distributed under the License is distributed on an "AS IS" BASIS,
15 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 ## See the License for the specific language governing permissions and
17 ## limitations under the License.
18 ##
19 ################################################################################*/
20
21/*
22 * compile-time factorial function
23 */
24
25#ifndef _gcem_factorial_HPP
26#define _gcem_factorial_HPP
27
28namespace gcem
29{
30
31namespace internal
32{
33
34// T should be int, long int, unsigned int, etc.
35
36template<typename T>
37constexpr
38T
40noexcept
41{ // table for x! when x = {0, ..., 20}
42 return( x == T(0) ? T(1) : x == T(1) ? T(1) :
43 x == T(2) ? T(2) : x == T(3) ? T(6) :
44 x == T(4) ? T(24) : x == T(5) ? T(120) :
45 x == T(6) ? T(720) : x == T(7) ? T(5040) :
46 x == T(8) ? T(40320) : x == T(9) ? T(362880) :
47 //
48 x == T(10) ? T(3628800) :
49 x == T(11) ? T(39916800) :
50 x == T(12) ? T(479001600) :
51 x == T(13) ? T(6227020800) :
52 x == T(14) ? T(87178291200) :
53 x == T(15) ? T(1307674368000) :
54 x == T(16) ? T(20922789888000) :
55 x == T(17) ? T(355687428096000) :
56 x == T(18) ? T(6402373705728000) :
57 x == T(19) ? T(121645100408832000) :
58 T(2432902008176640000) );
59}
60
61template<typename T, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
62constexpr
63T
65noexcept
66{
67 return( x < T(21) ? \
68 // if
70 // else (but overflow is almost guaranteed here)
71 x * factorial_recur(x - 1) );
72}
73
74template<typename T, typename std::enable_if<!std::is_integral<T>::value>::type* = nullptr>
75constexpr
76T
77factorial_recur(const T x)
78noexcept
79{
80 return tgamma(x + 1);
81}
82
83}
84
85/**
86 * Compile-time factorial function
87 *
88 * @param x a real-valued input.
89 * @return Computes the factorial value \f$ x! \f$.
90 * When \c x is an integral type (\c int, <tt>long int</tt>, etc.), a simple recursion method is used, along with table values.
91 * When \c x is real-valued, <tt>factorial(x) = tgamma(x+1)</tt>.
92 */
93
94template<typename T>
95constexpr
96T
97factorial(const T x)
98noexcept
99{
101}
102
103}
104
105#endif
constexpr T factorial_recur(const T x) noexcept
Definition factorial.hpp:64
constexpr T factorial_table(const T x) noexcept
Definition factorial.hpp:39
Definition is_odd.hpp:29
constexpr T factorial(const T x) noexcept
Compile-time factorial function.
Definition factorial.hpp:97
constexpr return_t< T > tgamma(const T x) noexcept
Compile-time gamma function.
Definition tgamma.hpp:80