Program: 57088
Project: 57088
Buggy source:
/** 
 * Computes the digamma function, defined as the logarithmic derivative of the \( \Gamma \) function: \( \frac{d}{dx}(\ln \Gamma(x)) = \frac{\Gamma^\prime(x)}{\Gamma(x)} \). <p>This is an independently written implementation of the algorithm described in Jose Bernardo, Algorithm AS 103: Psi (Digamma) Function, Applied Statistics, 1976. A <a href="https://en.wikipedia.org/wiki/Digamma_function#Reflection_formula"> reflection formula</a> is incorporated to improve performance on negative values.</p> <p>Some of the constants have been changed to increase accuracy at the moderate expense of run-time.  The result should be accurate to within \( 10^{-8} \) relative tolerance for \( 0 < x < 10^{-5} \) and within \( 10^{-8} \) absolute tolerance otherwise.</p>
 * @param x Argument.
 * @return digamma(x) to within \( 10^{-8} \) relative or absolute error whichever is larger.
 * @see <a href="http://en.wikipedia.org/wiki/Digamma_function">Digamma</a>
 * @see <a href="http://www.uv.es/~bernardo/1976AppStatist.pdf">Bernardo&apos;s original article</a>
 * @since 2.0
 */
public static double digamma(double x){
  if (Double.isNaN(x) || Double.isInfinite(x)) {
    return x;
  }
  double digamma=0.0;
  if (x < 0) {
    digamma-=FastMath.PI / FastMath.tan(FastMath.PI * x);
    x=1 - x;
  }
  if (x > 0 && x <= S_LIMIT) {
    return digamma - GAMMA - 1 / x;
  }
  while (x < C_LIMIT) {
    digamma-=1.0 / x;
    x+=1.0;
  }
  double inv=1 / (x * x);
  digamma+=FastMath.log(x) - 0.5 / x - inv * ((1.0 / 12) + inv * (1.0 / 120 - inv / 252));
  return digamma;
}

Fixed version (ground truth):
/** 
 * Computes the digamma function, defined as the logarithmic derivative of the \( \Gamma \) function: \( \frac{d}{dx}(\ln \Gamma(x)) = \frac{\Gamma^\prime(x)}{\Gamma(x)} \). <p>This is an independently written implementation of the algorithm described in Jose Bernardo, Algorithm AS 103: Psi (Digamma) Function, Applied Statistics, 1976. A <a href="https://en.wikipedia.org/wiki/Digamma_function#Reflection_formula"> reflection formula</a> is incorporated to improve performance on negative values.</p> <p>Some of the constants have been changed to increase accuracy at the moderate expense of run-time.  The result should be accurate to within \( 10^{-8} \) relative tolerance for \( 0 \le x \le 10^{-5} \) and within \( 10^{-8} \) absolute tolerance otherwise.</p>
 * @param x Argument.
 * @return digamma(x) to within \( 10^{-8} \) relative or absolute error whichever is larger.
 * @see <a href="http://en.wikipedia.org/wiki/Digamma_function">Digamma</a>
 * @see <a href="http://www.uv.es/~bernardo/1976AppStatist.pdf">Bernardo's original article</a>
 * @since 2.0
 */
public static double digamma(double x){
  if (Double.isNaN(x) || Double.isInfinite(x)) {
    return x;
  }
  double digamma=0.0;
  if (x < 0) {
    digamma-=FastMath.PI / FastMath.tan(FastMath.PI * x);
    x=1 - x;
  }
  if (x > 0 && x <= S_LIMIT) {
    return digamma - GAMMA - 1 / x;
  }
  while (x < C_LIMIT) {
    digamma-=1.0 / x;
    x+=1.0;
  }
  double inv=1 / (x * x);
  digamma+=FastMath.log(x) - 0.5 / x - inv * ((1.0 / 12) + inv * (1.0 / 120 - inv / 252));
  return digamma;
}