aGrUM  0.16.0
gammaLog2_inl.h
Go to the documentation of this file.
1 
30 namespace gum {
31 
32  ALWAYS_INLINE double GammaLog2::gammaLog2(double x) const {
33  if (x <= 0)
35  "log2(gamma()) should be called with a positive argument");
36 
37  // if x is small, use precomputed values
38  if (x < 50) {
40  Idx index = int(x * 100);
41  return __small_values[index]
42  + (__small_values[index + 1] - __small_values[index])
43  * double(x * 100 - index);
44  } else {
45  Idx index = int(x * 100 + 0.5);
46  return __small_values[index];
47  }
48  }
49 
50  // returns the approximation by the stirling formula
51  return (__log_sqrt_2pi + (x - 0.5f) * log(x) - x + log(1.0 + 1.0 / (12 * x)))
52  * __1log2;
53  }
54 
55  ALWAYS_INLINE double GammaLog2::operator()(double x) const {
56  return gammaLog2(x);
57  }
58 
59  INLINE void GammaLog2::setPrecision(bool prec) { __requires_precision = prec; }
60 
61 } /* namespace gum */
double gammaLog2(double x) const
Returns log2 ( gamma (x) ) for x >= 0.
Definition: gammaLog2_inl.h:32
void setPrecision(bool p)
Sets whether we need more precision for small values.
Definition: gammaLog2_inl.h:59
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
static constexpr double __log_sqrt_2pi
The value of std::log ( std::sqrt(2pi) ).
Definition: gammaLog2.h:120
bool __requires_precision
Indicates whether we need more precision for small values.
Definition: gammaLog2.h:114
double operator()(double x) const
Returns log2 ( gamma (x) ) for x > 0.
Definition: gammaLog2_inl.h:55
Size Idx
Type for indexes.
Definition: types.h:53
static const std::vector< double > __small_values
The 5000 values from 0 to 50 by step of 1/100.
Definition: gammaLog2.h:123
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
static constexpr double __1log2
The value of 1 / std::log(2).
Definition: gammaLog2.h:117