public static double logGamma(double x) {
		double xcopy = x;
		double fg = 0.0D;
		double first = x + lgfGamma + 0.5;
		double second = lgfCoeff[0];

		if (x >= 0.0) {
			first -= (x + 0.5) * Math.log(first);
			for (int i = 1; i <= lgfN; i++)
				second += lgfCoeff[i] / ++xcopy;
			fg = Math.log(Math.sqrt(2.0 * Math.PI) * second / x) - first;
		} else {
			fg = Math.PI / (Stat.gamma(1.0D - x) * Math.sin(Math.PI * x));

			if (fg != 1.0 / 0.0 && fg != -1.0 / 0.0) {
				if (fg < 0) {
					throw new IllegalArgumentException("\nThe gamma function is negative");
				} else {
					fg = Math.log(fg);
				}
			}
		}
		return fg;
	}