Skip to content

Commit

Permalink
fixed files form Math #93
Browse files Browse the repository at this point in the history
  • Loading branch information
tdurieux committed Mar 7, 2017
1 parent 88ebb8f commit 7d1505a
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions projects/Math/93/org/apache/commons/math/util/MathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,10 @@ public static boolean equals(double[] x, double[] y) {
* @throws IllegalArgumentException if n < 0
*/
public static long factorial(final int n) {
long result = Math.round(factorialDouble(n));
if (result == Long.MAX_VALUE) {
if (n < 0) {
throw new IllegalArgumentException("must have n >= 0 for n!");
}
if (n > 20) {
throw new ArithmeticException(
"factorial value is too large to fit in a long");
}
Expand Down Expand Up @@ -374,6 +376,9 @@ public static double factorialDouble(final int n) {
if (n < 0) {
throw new IllegalArgumentException("must have n >= 0 for n!");
}
if (n < 21) {
return factorial(n);
}
return Math.floor(Math.exp(factorialLog(n)) + 0.5);
}

Expand All @@ -394,6 +399,9 @@ public static double factorialLog(final int n) {
if (n < 0) {
throw new IllegalArgumentException("must have n > 0 for n!");
}
if (n < 21) {
return Math.log(factorial(n));
}
double logSum = 0;
for (int i = 2; i <= n; i++) {
logSum += Math.log((double)i);
Expand Down

0 comments on commit 7d1505a

Please sign in to comment.