QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
factorial.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2003 Ferdinando Ametrano
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20#include <ql/math/factorial.hpp>
21#include <ql/math/distributions/gammadistribution.hpp>
22#include <ql/errors.hpp>
23
24namespace QuantLib {
25
26 namespace {
27
28 const double firstFactorials[] = {
29 1.0, 1.0,
30 2.0, 6.0,
31 24.0, 120.0,
32 720.0, 5040.0,
33 40320.0, 362880.0,
34 3628800.0, 39916800.0,
35 479001600.0, 6227020800.0,
36 87178291200.0, 1307674368000.0,
37 20922789888000.0, 355687428096000.0,
38 6402373705728000.0, 121645100408832000.0,
39 2432902008176640000.0, 51090942171709440000.0,
40 1124000727777607680000.0, 25852016738884976640000.0,
41 620448401733239439360000.0, 15511210043330985984000000.0,
42 403291461126605635584000000.0, 10888869450418352160768000000.0
43 };
44
45 const Size tabulated =
46 sizeof(firstFactorials)/sizeof(firstFactorials[0])-1;
47
48 }
49
51 if (i<=tabulated) {
52 return firstFactorials[i];
53 } else {
54 return std::exp(GammaFunction().logValue(i+1));
55 }
56 }
57
59 if (i<=tabulated) {
60 return std::log(firstFactorials[i]);
61 } else {
62 return GammaFunction().logValue(i+1);
63 }
64 }
65
66}
static Real get(Natural n)
Definition: factorial.cpp:50
static Real ln(Natural n)
Definition: factorial.cpp:58
Gamma function class.
Real logValue(Real x) const
QL_REAL Real
real number
Definition: types.hpp:50
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35