diff --git a/projects/Math/93/org/apache/commons/math/util/MathUtils.java b/projects/Math/93/org/apache/commons/math/util/MathUtils.java index 7d8aa4d..1389d5a 100644 --- a/projects/Math/93/org/apache/commons/math/util/MathUtils.java +++ b/projects/Math/93/org/apache/commons/math/util/MathUtils.java @@ -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"); } @@ -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); } @@ -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);