Merge pull request #113 from tesselode/fix-multi-targets
[wdl/wdl-ol.git] / WDL / cmath / factorial.h
blobf19803da7810f1a052dd652028ed14d2ea5949b2
1 /*
2 factorial.h
3 Copyright (C) 2011 and later Lubomir I. Ivanov (neolit123 [at] gmail)
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
23 methods to return low-order factorials depending on allowed data types.
25 20! = 2432902008176640000 is the maximum factorial to be held
26 in a unsigned 64bit integer.
27 13! = 479001600 is the maximum factorial to be held in a unsigned
28 32bit integer.
31 #ifndef _FACTORIAL_H_
32 #define _FACTORIAL_H_
34 #include "custom_math.h"
36 #define FACTORIAL_LOWER \
37 MK_ULL(1), \
38 MK_ULL(1), \
39 MK_ULL(2), \
40 MK_ULL(6), \
41 MK_ULL(24), \
42 MK_ULL(120), \
43 MK_ULL(720), \
44 MK_ULL(5040), \
45 MK_ULL(40320), \
46 MK_ULL(362880), \
47 MK_ULL(3628800), \
48 MK_ULL(39916800), \
49 MK_ULL(479001600)
51 #define FACTORIAL_HIGHER \
52 MK_ULL(6227020800), \
53 MK_ULL(87178291200), \
54 MK_ULL(1307674368000), \
55 MK_ULL(20922789888000), \
56 MK_ULL(355687428096000), \
57 MK_ULL(6402373705728000), \
58 MK_ULL(121645100408832000), \
59 MK_ULL(2432902008176640000)
61 static const cmath_std_uint_t _factorials[] =
63 #ifdef _CMATH_ANSI
64 FACTORIAL_LOWER
65 #else
66 FACTORIAL_LOWER,
67 FACTORIAL_HIGHER
68 #endif
71 static const cmath_t _inv_factorials[] =
73 1.00000000000000000000000000000000,
74 1.00000000000000000000000000000000,
75 0.50000000000000000000000000000000,
76 0.16666666666666666666666666666667,
77 0.04166666666666666666666666666666,
78 0.00833333333333333333333333333333,
79 0.00138888888888888888888888888888,
80 0.00019841269841269841269841269841,
81 0.00002480158730158730158730158730,
82 0.00000275573192239858906525573192,
83 0.00000027557319223985890652557319,
84 0.00000002505210838544171877505210,
85 0.00000000208767569878680989792100,
86 0.00000000016059043836821614599390,
87 0.00000000001147074559772972471385,
88 0.00000000000076471637318198164750,
89 0.00000000000004779477332387385297,
90 0.00000000000000281145725434552076,
91 0.00000000000000015619206968586225,
92 0.00000000000000000822063524662433,
93 0.00000000000000000041103176233122
96 _CMATH_INLINE
97 cmath_std_uint_t factorial(const cmath_uint32_t x)
99 if(x >= cmath_array_size(_factorials))
100 return 0;
101 else
102 return _factorials[x];
105 _CMATH_INLINE
106 cmath_t inv_factorial(const cmath_uint32_t x)
108 if(x >= cmath_array_size(_inv_factorials))
109 return 0;
110 else
111 return _inv_factorials[x];
114 #endif /* _FACTORIAL_H_ */