public static double regularizedGammaP(final double a,final double x){
  if (Double.isInfinite(a) || Double.isInfinite(x) || !(a > 0.0)|| !(x >= 0.0)) {
    return Double.NaN;
  }
  if (x == 0.0) {
    return 0.0;
  }
  if (x >= a + 1) {
    return 1.0 - regularizedGammaQ(a,x);
  }
  double term=1.0 / a;
  double sum=term;
  for (int n=1; n < MAX_ITERATIONS; n++) {
    term=x / (a + n) * term;
    sum=sum + term;
    if (sum == Double.POSITIVE_INFINITY) {
      return 1.0;
    }
    if (Math.abs(term / sum) < NUM_PRECISION) {
      break;
    }
  }
  return Math.exp(-x + (a * Math.log(x)) - logGamma(a)) * sum;
}

public static double regularizedGammaP ( final double a , final double x ) { if ( Double . isInfinite ( a ) || Double . isInfinite ( x ) || ! ( a > 0.0 ) || ! ( x >= 0.0 ) ) { return Double . NaN ; } if ( x == 0.0 ) { return 0.0 ; } if ( x >= a + 1 ) { return 1.0 - regularizedGammaQ ( a , x ) ; } double term = 1.0 / a ; double sum = term ; for ( int n = 1 ; n < MAX_ITERATIONS ; n ++ ) { term = x / ( a + n ) * term ; sum = sum + term ; if ( sum == Double . POSITIVE_INFINITY ) { return 1.0 ; } if ( Math . abs ( term / sum ) < NUM_PRECISION ) { break ; } } return Math . exp ( - x + ( a * Math . log ( x ) ) - logGamma ( a ) ) * sum ; }
returns the regularized gamma function p ( a , x ) . includes the quadrature way of computing . todo : find " the " most accurate version of this . we seem to agree with others for the first 10 + digits , but diverge a bit later than that .
