mathutil.cc
1 // Copyright 2008 Google Inc. All Rights Reserved.
2 
3 #include "util/math/mathutil.h"
4 #include <vector>
5 using std::vector;
6 
7 #include "base/integral_types.h"
8 #include "base/logging.h"
9 
10 double MathUtil::Harmonic(int64 const n, double *const e) {
11  CHECK_GT(n, 0);
12 
13  // Hn ~ ln(n) + 0.5772156649 +
14  // + 1/(2n) - 1/(12n^2) + 1/(120n^4) - error,
15  // with 0 < error < 1/(256*n^4).
16 
17  double const
18  d = static_cast<double>(n),
19  d2 = d * d,
20  d4 = d2 * d2;
21 
22  return (log(d) + 0.5772156649) // ln + Gamma constant
23  + 1 / (2 * d) - 1 / (12 * d2) + 1 / (120 * d4)
24  - (*e = 1 / (256 * d4));
25 }