aGrUM  0.14.1
gammaLog2_inl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 namespace gum {
28
29  ALWAYS_INLINE double GammaLog2::gammaLog2(double x) const {
30  if (x <= 0)
32  "log2(gamma()) should be called with a positive argument");
33
34  // if x is small, use precomputed values
35  if (x < 50) {
37  Idx index = int(x * 100);
38  return __small_values[index]
39  + (__small_values[index + 1] - __small_values[index])
40  * double(x * 100 - index);
41  } else {
42  Idx index = int(x * 100 + 0.5);
43  return __small_values[index];
44  }
45  }
46
47  // returns the approximation by the stirling formula
48  return (__log_sqrt_2pi + (x - 0.5f) * log(x) - x + log(1.0 + 1.0 / (12 * x)))
49  * __1log2;
50  }
51
52  ALWAYS_INLINE double GammaLog2::operator()(double x) const {
53  return gammaLog2(x);
54  }
55
56  INLINE void GammaLog2::setPrecision(bool prec) { __requires_precision = prec; }
57
58 } /* namespace gum */
double gammaLog2(double x) const
Returns log2 ( gamma (x) ) for x >= 0.
Definition: gammaLog2_inl.h:29
void setPrecision(bool p)
Sets whether we need more precision for small values.
Definition: gammaLog2_inl.h:56
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
static constexpr double __log_sqrt_2pi
The value of std::log ( std::sqrt(2pi) ).
Definition: gammaLog2.h:117
bool __requires_precision
Indicates whether we need more precision for small values.
Definition: gammaLog2.h:111
double operator()(double x) const
Returns log2 ( gamma (x) ) for x > 0.
Definition: gammaLog2_inl.h:52
Size Idx
Type for indexes.
Definition: types.h:50
static const std::vector< double > __small_values
The 5000 values from 0 to 50 by step of 1/100.
Definition: gammaLog2.h:120
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
static constexpr double __1log2
The value of 1 / std::log(2).
Definition: gammaLog2.h:114