public static double regularizedGammaQ(final double a,final double x){
  if (Double.isNaN(a) || Double.isNaN(x) || (a <= 0.0)|| (x < 0.0)) {
    return Double.NaN;
  }
  if (x == 0.0) {
    return 1.0;
  }
  if (x < a + 1.0) {
    return 1.0 - regularizedGammaP(a,x);
  }
  final double FPMIN=Double.MIN_VALUE / NUM_PRECISION;
  double b=x + 1 - a;
  double c=1.0 / FPMIN;
  double d=1.0 / b;
  double fac=d;
  for (int i=1; i < MAX_ITERATIONS; i++) {
    double an=i * (a - i);
    b+=2;
    d=an * d + b;
    if (Math.abs(d) < FPMIN) {
      d=FPMIN;
    }
    c=b + an / c;
    if (Math.abs(c) < FPMIN) {
      c=FPMIN;
    }
    d=1 / d;
    double del=d * c;
    fac*=del;
    if (Math.abs(del - 1.0) <= NUM_PRECISION) {
      break;
    }
  }
  return fac * Math.exp(-x + a * Math.log(x) - logGamma(a));
}

public static double regularizedGammaQ ( final double a , final double x ) { if ( Double . isNaN ( a ) || Double . isNaN ( x ) || ( a <= 0.0 ) || ( x < 0.0 ) ) { return Double . NaN ; } if ( x == 0.0 ) { return 1.0 ; } if ( x < a + 1.0 ) { return 1.0 - regularizedGammaP ( a , x ) ; } final double FPMIN = Double . MIN_VALUE / NUM_PRECISION ; double b = x + 1 - a ; double c = 1.0 / FPMIN ; double d = 1.0 / b ; double fac = d ; for ( int i = 1 ; i < MAX_ITERATIONS ; i ++ ) { double an = i * ( a - i ) ; b += 2 ; d = an * d + b ; if ( Math . abs ( d ) < FPMIN ) { d = FPMIN ; } c = b + an / c ; if ( Math . abs ( c ) < FPMIN ) { c = FPMIN ; } d = 1 / d ; double del = d * c ; fac *= del ; if ( Math . abs ( del - 1.0 ) <= NUM_PRECISION ) { break ; } } return fac * Math . exp ( - x + a * Math . log ( x ) - logGamma ( a ) ) ; }
returns the regularized gamma function q ( a , x ) = 1 - p ( a , x ) . includes the continued fraction way of computing , based loosely on the book " numerical recipes " ; but probably not with the exactly same precision , since we reimplemented this in our coding style , not literally . 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 .
