Skip to content

Commit dccb000

Browse files
committed
adjusted floating point constants precision
#131
1 parent 8bda92b commit dccb000

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

src/org/cicirello/math/MathFunctions.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* MathFunctions is a class of utility methods that implement various mathematical functions.
2727
*
2828
* @author <a href=https://www.cicirello.org/ target=_top>Vincent A. Cicirello</a>, <a href=https://www.cicirello.org/ target=_top>https://www.cicirello.org/</a>
29-
* @version 2.12.2021
29+
* @version 5.13.2021
3030
*
3131
*/
3232
public final class MathFunctions {
@@ -92,7 +92,7 @@ public static double logGamma(double n) {
9292
// This check doesn't seem to be necessary given addition of first check in block.
9393
// if (z == 0.0) return Double.POSITIVE_INFINITY;
9494
// ln(PI)
95-
final double LOG_PI = 1.14472988584940017414;
95+
final double LOG_PI = 1.1447298858494002;
9696
return LOG_PI - Math.log(z) - logGamma(n);
9797
} else if (n < 13.0) {
9898
double z = 1.0;
@@ -117,13 +117,13 @@ public static double logGamma(double n) {
117117
n * evaluatePolynomial(n, POLY_APPROX_2) / evaluatePolynomial(n, POLY_APPROX_3);
118118
} else {
119119
// ln(sqrt(2pi))
120-
final double LOG_SQRT_PI2 = 0.91893853320467274178;
120+
final double LOG_SQRT_PI2 = 0.9189385332046727;
121121
double q = (n - 0.5) * Math.log(n) - n + LOG_SQRT_PI2;
122122
if (n > 1.0e8) return q;
123123
double p = 1.0 / (n * n);
124124
if (n >= 1000.0) {
125-
q = q + ((7.9365079365079365079365e-4 * p - 2.7777777777777777777778e-3)
126-
* p + 0.0833333333333333333333) / n;
125+
q = q + ((7.936507936507937E-4 * p - 0.002777777777777778)
126+
* p + 0.08333333333333333) / n;
127127
} else {
128128
q += evaluatePolynomial(p, STIRLING_EXPANSION_LN_GAMMA) / n;
129129
}
@@ -142,21 +142,22 @@ private static double evaluatePolynomial(double x, double polynomial[]) {
142142
}
143143

144144
private static final double[] STIRLING_EXPANSION_LN_GAMMA = {
145-
8.11614167470508450300E-4, -5.95061904284301438324E-4,
146-
7.93650340457716943945E-4, -2.77777777730099687205E-3,
147-
8.33333333333331927722E-2
145+
8.116141674705085E-4, -5.950619042843014E-4,
146+
7.936503404577169E-4, -0.002777777777300997,
147+
0.08333333333333319
148148
};
149149

150150
private static final double[] POLY_APPROX_2 = {
151-
-1.37825152569120859100E3, -3.88016315134637840924E4,
152-
-3.31612992738871184744E5, -1.16237097492762307383E6,
153-
-1.72173700820839662146E6, -8.53555664245765465627E5
151+
-1378.2515256912086, -38801.631513463784,
152+
-331612.9927388712, -1162370.974927623,
153+
-1721737.0082083966, -853555.6642457654
154154
};
155155

156156
private static final double[] POLY_APPROX_3 = {
157-
1.0, -3.51815701436523470549E2, -1.70642106651881159223E4,
158-
-2.20528590553854454839E5, -1.13933444367982507207E6,
159-
-2.53252307177582951285E6, -2.01889141433532773231E6
157+
1.0,
158+
-351.81570143652345, -17064.210665188115,
159+
-220528.59055385445, -1139334.4436798252,
160+
-2532523.0717758294, -2018891.4143353277,
160161
};
161162

162163
// HELPERS FOR logGamma END HERE

0 commit comments

Comments
 (0)