List of usage examples for org.apache.commons.math3.exception.util LocalizedFormats FACTORIAL_NEGATIVE_PARAMETER
LocalizedFormats FACTORIAL_NEGATIVE_PARAMETER
To view the source code for org.apache.commons.math3.exception.util LocalizedFormats FACTORIAL_NEGATIVE_PARAMETER.
Click Source Link
From source file:org.gitools.analysis.groupcomparison.format.math33Preview.CombinatoricsUtils.java
/** * Returns n!. Shorthand for {@code n} <a * href="http://mathworld.wolfram.com/Factorial.html"> Factorial</a>, the * product of the numbers {@code 1, ..., n}. * <p>/* w w w .j a v a2 s. c om*/ * <Strong>Preconditions</strong>: * <ul> * <li> {@code n >= 0} (otherwise * {@code IllegalArgumentException} is thrown)</li> * <li> The result is small enough to fit into a {@code long}. The * largest value of {@code n} for which {@code n!} < * Long.MAX_VALUE} is 20. If the computed value exceeds {@code Long.MAX_VALUE} * an {@code ArithMeticException } is thrown.</li> * </ul> * </p> * * @param n argument * @return {@code n!} * @throws MathArithmeticException if the result is too large to be represented * by a {@code long}. * @throws NotPositiveException if {@code n < 0}. * @throws MathArithmeticException if {@code n > 20}: The factorial value is too * large to fit in a {@code long}. */ public static long factorial(final int n) throws NotPositiveException, MathArithmeticException { if (n < 0) { throw new NotPositiveException(LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER, n); } if (n > 20) { throw new MathArithmeticException(); } return FACTORIALS[n]; }
From source file:org.gitools.analysis.groupcomparison.format.math33Preview.CombinatoricsUtils.java
/** * Compute n!, the<a href="http://mathworld.wolfram.com/Factorial.html"> * factorial</a> of {@code n} (the product of the numbers 1 to n), as a * {@code double}.// w w w. j a va 2s. com * The result should be small enough to fit into a {@code double}: The * largest {@code n} for which {@code n! < Double.MAX_VALUE} is 170. * If the computed value exceeds {@code Double.MAX_VALUE}, * {@code Double.POSITIVE_INFINITY} is returned. * * @param n Argument. * @return {@code n!} * @throws NotPositiveException if {@code n < 0}. */ public static double factorialDouble(final int n) throws NotPositiveException { if (n < 0) { throw new NotPositiveException(LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER, n); } if (n < 21) { return FACTORIALS[n]; } return FastMath.floor(FastMath.exp(CombinatoricsUtils.factorialLog(n)) + 0.5); }
From source file:org.gitools.analysis.groupcomparison.format.math33Preview.CombinatoricsUtils.java
/** * Compute the natural logarithm of the factorial of {@code n}. * * @param n Argument./* www . j a v a 2s .c om*/ * @return {@code n!} * @throws NotPositiveException if {@code n < 0}. */ public static double factorialLog(final int n) throws NotPositiveException { if (n < 0) { throw new NotPositiveException(LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER, n); } if (n < 21) { return FastMath.log(FACTORIALS[n]); } double logSum = 0; for (int i = 2; i <= n; i++) { logSum += FastMath.log(i); } return logSum; }