/** * Round the given value to the specified number of decimal places. The * value is rounded using the {@link BigDecimal#ROUND_HALF_UP} method. * * @param x the value to round. * @param scale the number of digits to the right of the decimal point. * @return the rounded value. * @since 1.1 */ public static double round(double x, int scale) { return round(x, scale, BigDecimal.ROUND_HALF_UP); }
/** * Add two long integers, checking for overflow. * * @param a an addend * @param b an addend * @return the sum <code>a+b</code> * @throws ArithmeticException if the result can not be represented as an * long * @since 1.2 */ public static long addAndCheck(long a, long b) { return addAndCheck(a, b, LocalizedFormats.OVERFLOW_IN_ADDITION); }
/** * Returns the least common multiple between two integer values. * * @param a the first integer value. * @param b the second integer value. * @return the least common multiple between a and b. * @throws ArithmeticException if the lcm is too large to store as an int * @since 1.1 */ public static int lcm(int a, int b) { return Math.abs(mulAndCheck(a / gcd(a, b), b)); }
/** * Round the given value to the specified number of decimal places. The * value is rounded using the given method which is any method defined in * {@link BigDecimal}. * * @param x the value to round. * @param scale the number of digits to the right of the decimal point. * @param roundingMethod the rounding method as defined in * {@link BigDecimal}. * @return the rounded value. * @since 1.1 */ public static float round(float x, int scale, int roundingMethod) { float sign = indicator(x); float factor = (float)Math.pow(10.0f, scale) * sign; return (float)roundUnscaled(x * factor, sign, roundingMethod) / factor; }
checkBinomial(n, k); if ((n == k) || (k == 0)) { return 1d; return binomialCoefficientDouble(n, n - k); return binomialCoefficient(n,k);
int d1 = MathUtils.gcd(denominator, fraction.denominator); if (d1==1) { int uvp = MathUtils.mulAndCheck(numerator, fraction.denominator); int upv = MathUtils.mulAndCheck(fraction.numerator, denominator); return new Fraction (isAdd ? MathUtils.addAndCheck(uvp, upv) : MathUtils.subAndCheck(uvp, upv), MathUtils.mulAndCheck(denominator, fraction.denominator)); int d2 = (tmodd1==0)?d1:MathUtils.gcd(tmodd1, d1); MathUtils.mulAndCheck(denominator/d1, fraction.denominator/d2));
checkBinomial(n, k); if ((n == k) || (k == 0)) { return 0; return Math.log(binomialCoefficient(n,k)); return Math.log(binomialCoefficientDouble(n, k)); return binomialCoefficientLog(n, n - k);
return factorial(n); return Math.floor(Math.exp(factorialLog(n)) + 0.5);
/** * For this distribution, X, this method returns P(X = x). * @param x the value at which the PMF is evaluated * @return PMF for this distribution */ public double probability(int x) { double ret; if (x < 0) { ret = 0.0; } else { ret = MathUtils.binomialCoefficientDouble(x + numberOfSuccesses - 1, numberOfSuccesses - 1) * Math.pow(probabilityOfSuccess, numberOfSuccesses) * Math.pow(1.0 - probabilityOfSuccess, x); } return ret; }
/** * For the distribution, X, defined by the given hypergeometric distribution * parameters, this method returns P(X = x). * * @param n the population size. * @param m number of successes in the population. * @param k the sample size. * @param x the value at which the PMF is evaluated. * @return PMF for the distribution. */ private double probability(int n, int m, int k, int x) { return Math.exp(MathUtils.binomialCoefficientLog(m, x) + MathUtils.binomialCoefficientLog(n - m, k - x) - MathUtils.binomialCoefficientLog(n, k)); }
/** * Returns n!. Shorthand for <code>n</code> <a * href="http://mathworld.wolfram.com/Factorial.html"> Factorial</a>, the * product of the numbers <code>1,...,n</code> as a <code>double</code>. * <p> * <Strong>Preconditions</strong>: * <ul> * <li> <code>n >= 0</code> (otherwise * <code>IllegalArgumentException</code> is thrown)</li> * <li> The result is small enough to fit into a <code>double</code>. The * largest value of <code>n</code> for which <code>n!</code> < * Double.MAX_VALUE</code> is 170. If the computed value exceeds * Double.MAX_VALUE, Double.POSITIVE_INFINITY is returned</li> * </ul> * </p> * * @param n argument * @return <code>n!</code> * @throws IllegalArgumentException if n < 0 */ public static double factorialDouble(final int n) { if (n < 0) { throw new IllegalArgumentException("must have n >= 0 for n!"); } return Math.floor(Math.exp(factorialLog(n)) + 0.5); }
int d = MathUtils.gcd(num, den); if (d > 1) { num /= d;
} else { return createComplex(Math.abs(imaginary) / (2.0 * t), MathUtils.indicator(imaginary) * t);
/** * Returns the least common multiple between two integer values. * * @param a the first integer value. * @param b the second integer value. * @return the least common multiple between a and b. * @throws ArithmeticException if the lcm is too large to store as an int * @since 1.1 */ public static int lcm(int a, int b) { return Math.abs(mulAndCheck(a / gcd(a, b), b)); }
int d1 = MathUtils.gcd(denominator, fraction.denominator); if (d1==1) { int uvp = MathUtils.mulAndCheck(numerator, fraction.denominator); int upv = MathUtils.mulAndCheck(fraction.numerator, denominator); return new Fraction (isAdd ? MathUtils.addAndCheck(uvp, upv) : MathUtils.subAndCheck(uvp, upv), MathUtils.mulAndCheck(denominator, fraction.denominator)); int d2 = (tmodd1==0)?d1:MathUtils.gcd(tmodd1, d1); MathUtils.mulAndCheck(denominator/d1, fraction.denominator/d2));
checkBinomial(n, k); if ((n == k) || (k == 0)) { return 0; return FastMath.log(binomialCoefficient(n,k)); return FastMath.log(binomialCoefficientDouble(n, k)); return binomialCoefficientLog(n, n - k);