Repository: commons-math Updated Branches: refs/heads/master e082e0c48 -> 50d293912
MATH-1416: Depend on "Commons Numbers". Replaced class "ArithmeticUtils" by its equivalent in module "commons-numbers-core". Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/50d29391 Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/50d29391 Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/50d29391 Branch: refs/heads/master Commit: 50d2939125522b7fecb16546fe7a5c253566df15 Parents: e082e0c Author: Gilles <[email protected]> Authored: Thu May 4 01:55:53 2017 +0200 Committer: Gilles <[email protected]> Committed: Thu May 4 01:55:53 2017 +0200 ---------------------------------------------------------------------- .../commons/math4/fraction/BigFraction.java | 2 +- .../apache/commons/math4/fraction/Fraction.java | 2 +- .../math4/transform/FastCosineTransformer.java | 2 +- .../math4/transform/FastFourierTransformer.java | 2 +- .../transform/FastHadamardTransformer.java | 2 +- .../math4/transform/FastSineTransformer.java | 2 +- .../commons/math4/util/ArithmeticUtils.java | 799 ------------------- .../apache/commons/math4/util/Combinations.java | 1 + .../commons/math4/util/CombinatoricsUtils.java | 1 + .../DerivativeStructureTest.java | 2 +- .../commons/math4/fraction/FractionTest.java | 68 +- .../math4/linear/InverseHilbertMatrix.java | 2 +- .../commons/math4/util/ArithmeticUtilsTest.java | 786 ------------------ .../math4/util/CombinatoricsUtilsTest.java | 28 +- .../apache/commons/math4/util/FastMathTest.java | 1 + 15 files changed, 59 insertions(+), 1641 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-math/blob/50d29391/src/main/java/org/apache/commons/math4/fraction/BigFraction.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/fraction/BigFraction.java b/src/main/java/org/apache/commons/math4/fraction/BigFraction.java index 69e8dc8..a3da8c0 100644 --- a/src/main/java/org/apache/commons/math4/fraction/BigFraction.java +++ b/src/main/java/org/apache/commons/math4/fraction/BigFraction.java @@ -26,7 +26,7 @@ import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.exception.ZeroException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.util.ArithmeticUtils; +import org.apache.commons.numbers.core.ArithmeticUtils; import org.apache.commons.math4.util.FastMath; import org.apache.commons.math4.util.MathUtils; http://git-wip-us.apache.org/repos/asf/commons-math/blob/50d29391/src/main/java/org/apache/commons/math4/fraction/Fraction.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/fraction/Fraction.java b/src/main/java/org/apache/commons/math4/fraction/Fraction.java index 39eba8d..834573c 100644 --- a/src/main/java/org/apache/commons/math4/fraction/Fraction.java +++ b/src/main/java/org/apache/commons/math4/fraction/Fraction.java @@ -23,7 +23,7 @@ import org.apache.commons.math4.FieldElement; import org.apache.commons.math4.exception.MathArithmeticException; import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.util.ArithmeticUtils; +import org.apache.commons.numbers.core.ArithmeticUtils; import org.apache.commons.math4.util.FastMath; /** http://git-wip-us.apache.org/repos/asf/commons-math/blob/50d29391/src/main/java/org/apache/commons/math4/transform/FastCosineTransformer.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/transform/FastCosineTransformer.java b/src/main/java/org/apache/commons/math4/transform/FastCosineTransformer.java index 969675e..35d724b 100644 --- a/src/main/java/org/apache/commons/math4/transform/FastCosineTransformer.java +++ b/src/main/java/org/apache/commons/math4/transform/FastCosineTransformer.java @@ -23,7 +23,7 @@ import org.apache.commons.math4.analysis.UnivariateFunction; import org.apache.commons.math4.complex.Complex; import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.util.ArithmeticUtils; +import org.apache.commons.numbers.core.ArithmeticUtils; import org.apache.commons.math4.util.FastMath; /** http://git-wip-us.apache.org/repos/asf/commons-math/blob/50d29391/src/main/java/org/apache/commons/math4/transform/FastFourierTransformer.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/transform/FastFourierTransformer.java b/src/main/java/org/apache/commons/math4/transform/FastFourierTransformer.java index d599f7c..522ea64 100644 --- a/src/main/java/org/apache/commons/math4/transform/FastFourierTransformer.java +++ b/src/main/java/org/apache/commons/math4/transform/FastFourierTransformer.java @@ -24,7 +24,7 @@ import org.apache.commons.math4.exception.DimensionMismatchException; import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.MathIllegalStateException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.util.ArithmeticUtils; +import org.apache.commons.numbers.core.ArithmeticUtils; import org.apache.commons.math4.util.FastMath; import org.apache.commons.math4.util.MathArrays; http://git-wip-us.apache.org/repos/asf/commons-math/blob/50d29391/src/main/java/org/apache/commons/math4/transform/FastHadamardTransformer.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/transform/FastHadamardTransformer.java b/src/main/java/org/apache/commons/math4/transform/FastHadamardTransformer.java index cf8e45d..1aafada 100644 --- a/src/main/java/org/apache/commons/math4/transform/FastHadamardTransformer.java +++ b/src/main/java/org/apache/commons/math4/transform/FastHadamardTransformer.java @@ -22,7 +22,7 @@ import org.apache.commons.math4.analysis.FunctionUtils; import org.apache.commons.math4.analysis.UnivariateFunction; import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.util.ArithmeticUtils; +import org.apache.commons.numbers.core.ArithmeticUtils; /** * Implements the <a href="http://www.archive.chipcenter.com/dsp/DSP000517F1.html">Fast Hadamard Transform</a> (FHT). http://git-wip-us.apache.org/repos/asf/commons-math/blob/50d29391/src/main/java/org/apache/commons/math4/transform/FastSineTransformer.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/transform/FastSineTransformer.java b/src/main/java/org/apache/commons/math4/transform/FastSineTransformer.java index 440ec7d..71e2cfb 100644 --- a/src/main/java/org/apache/commons/math4/transform/FastSineTransformer.java +++ b/src/main/java/org/apache/commons/math4/transform/FastSineTransformer.java @@ -23,7 +23,7 @@ import org.apache.commons.math4.analysis.UnivariateFunction; import org.apache.commons.math4.complex.Complex; import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.util.ArithmeticUtils; +import org.apache.commons.numbers.core.ArithmeticUtils; import org.apache.commons.math4.util.FastMath; /** http://git-wip-us.apache.org/repos/asf/commons-math/blob/50d29391/src/main/java/org/apache/commons/math4/util/ArithmeticUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/util/ArithmeticUtils.java b/src/main/java/org/apache/commons/math4/util/ArithmeticUtils.java deleted file mode 100644 index bab02d3..0000000 --- a/src/main/java/org/apache/commons/math4/util/ArithmeticUtils.java +++ /dev/null @@ -1,799 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.math4.util; - -import java.math.BigInteger; - -import org.apache.commons.math4.exception.MathArithmeticException; -import org.apache.commons.math4.exception.NotPositiveException; -import org.apache.commons.math4.exception.util.Localizable; -import org.apache.commons.math4.exception.util.LocalizedFormats; - -/** - * Some useful, arithmetics related, additions to the built-in functions in - * {@link Math}. - * - */ -public final class ArithmeticUtils { - - /** Private constructor. */ - private ArithmeticUtils() { - super(); - } - - /** - * Add two integers, checking for overflow. - * - * @param x an addend - * @param y an addend - * @return the sum {@code x+y} - * @throws MathArithmeticException if the result can not be represented - * as an {@code int}. - * @since 1.1 - */ - public static int addAndCheck(int x, int y) - throws MathArithmeticException { - long s = (long)x + (long)y; - if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) { - throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_ADDITION, x, y); - } - return (int)s; - } - - /** - * Add two long integers, checking for overflow. - * - * @param a an addend - * @param b an addend - * @return the sum {@code a+b} - * @throws MathArithmeticException if the result can not be represented as an long - * @since 1.2 - */ - public static long addAndCheck(long a, long b) throws MathArithmeticException { - return addAndCheck(a, b, LocalizedFormats.OVERFLOW_IN_ADDITION); - } - - /** - * Computes the greatest common divisor of the absolute value of two - * numbers, using a modified version of the "binary gcd" method. - * See Knuth 4.5.2 algorithm B. - * The algorithm is due to Josef Stein (1961). - * <br/> - * Special cases: - * <ul> - * <li>The invocations - * {@code gcd(Integer.MIN_VALUE, Integer.MIN_VALUE)}, - * {@code gcd(Integer.MIN_VALUE, 0)} and - * {@code gcd(0, Integer.MIN_VALUE)} throw an - * {@code ArithmeticException}, because the result would be 2^31, which - * is too large for an int value.</li> - * <li>The result of {@code gcd(x, x)}, {@code gcd(0, x)} and - * {@code gcd(x, 0)} is the absolute value of {@code x}, except - * for the special cases above.</li> - * <li>The invocation {@code gcd(0, 0)} is the only one which returns - * {@code 0}.</li> - * </ul> - * - * @param p Number. - * @param q Number. - * @return the greatest common divisor (never negative). - * @throws MathArithmeticException if the result cannot be represented as - * a non-negative {@code int} value. - * @since 1.1 - */ - public static int gcd(int p, int q) throws MathArithmeticException { - int a = p; - int b = q; - if (a == 0 || - b == 0) { - if (a == Integer.MIN_VALUE || - b == Integer.MIN_VALUE) { - throw new MathArithmeticException(LocalizedFormats.GCD_OVERFLOW_32_BITS, - p, q); - } - return FastMath.abs(a + b); - } - - long al = a; - long bl = b; - boolean useLong = false; - if (a < 0) { - if(Integer.MIN_VALUE == a) { - useLong = true; - } else { - a = -a; - } - al = -al; - } - if (b < 0) { - if (Integer.MIN_VALUE == b) { - useLong = true; - } else { - b = -b; - } - bl = -bl; - } - if (useLong) { - if(al == bl) { - throw new MathArithmeticException(LocalizedFormats.GCD_OVERFLOW_32_BITS, - p, q); - } - long blbu = bl; - bl = al; - al = blbu % al; - if (al == 0) { - if (bl > Integer.MAX_VALUE) { - throw new MathArithmeticException(LocalizedFormats.GCD_OVERFLOW_32_BITS, - p, q); - } - return (int) bl; - } - blbu = bl; - - // Now "al" and "bl" fit in an "int". - b = (int) al; - a = (int) (blbu % al); - } - - return gcdPositive(a, b); - } - - /** - * Computes the greatest common divisor of two <em>positive</em> numbers - * (this precondition is <em>not</em> checked and the result is undefined - * if not fulfilled) using the "binary gcd" method which avoids division - * and modulo operations. - * See Knuth 4.5.2 algorithm B. - * The algorithm is due to Josef Stein (1961). - * <br/> - * Special cases: - * <ul> - * <li>The result of {@code gcd(x, x)}, {@code gcd(0, x)} and - * {@code gcd(x, 0)} is the value of {@code x}.</li> - * <li>The invocation {@code gcd(0, 0)} is the only one which returns - * {@code 0}.</li> - * </ul> - * - * @param a Positive number. - * @param b Positive number. - * @return the greatest common divisor. - */ - private static int gcdPositive(int a, int b) { - if (a == 0) { - return b; - } - else if (b == 0) { - return a; - } - - // Make "a" and "b" odd, keeping track of common power of 2. - final int aTwos = Integer.numberOfTrailingZeros(a); - a >>= aTwos; - final int bTwos = Integer.numberOfTrailingZeros(b); - b >>= bTwos; - final int shift = FastMath.min(aTwos, bTwos); - - // "a" and "b" are positive. - // If a > b then "gdc(a, b)" is equal to "gcd(a - b, b)". - // If a < b then "gcd(a, b)" is equal to "gcd(b - a, a)". - // Hence, in the successive iterations: - // "a" becomes the absolute difference of the current values, - // "b" becomes the minimum of the current values. - while (a != b) { - final int delta = a - b; - b = Math.min(a, b); - a = Math.abs(delta); - - // Remove any power of 2 in "a" ("b" is guaranteed to be odd). - a >>= Integer.numberOfTrailingZeros(a); - } - - // Recover the common power of 2. - return a << shift; - } - - /** - * <p> - * Gets the greatest common divisor of the absolute value of two numbers, - * using the "binary gcd" method which avoids division and modulo - * operations. See Knuth 4.5.2 algorithm B. This algorithm is due to Josef - * Stein (1961). - * </p> - * Special cases: - * <ul> - * <li>The invocations - * {@code gcd(Long.MIN_VALUE, Long.MIN_VALUE)}, - * {@code gcd(Long.MIN_VALUE, 0L)} and - * {@code gcd(0L, Long.MIN_VALUE)} throw an - * {@code ArithmeticException}, because the result would be 2^63, which - * is too large for a long value.</li> - * <li>The result of {@code gcd(x, x)}, {@code gcd(0L, x)} and - * {@code gcd(x, 0L)} is the absolute value of {@code x}, except - * for the special cases above. - * <li>The invocation {@code gcd(0L, 0L)} is the only one which returns - * {@code 0L}.</li> - * </ul> - * - * @param p Number. - * @param q Number. - * @return the greatest common divisor, never negative. - * @throws MathArithmeticException if the result cannot be represented as - * a non-negative {@code long} value. - * @since 2.1 - */ - public static long gcd(final long p, final long q) throws MathArithmeticException { - long u = p; - long v = q; - if ((u == 0) || (v == 0)) { - if ((u == Long.MIN_VALUE) || (v == Long.MIN_VALUE)){ - throw new MathArithmeticException(LocalizedFormats.GCD_OVERFLOW_64_BITS, - p, q); - } - return FastMath.abs(u) + FastMath.abs(v); - } - // keep u and v negative, as negative integers range down to - // -2^63, while positive numbers can only be as large as 2^63-1 - // (i.e. we can't necessarily negate a negative number without - // overflow) - /* assert u!=0 && v!=0; */ - if (u > 0) { - u = -u; - } // make u negative - if (v > 0) { - v = -v; - } // make v negative - // B1. [Find power of 2] - int k = 0; - while ((u & 1) == 0 && (v & 1) == 0 && k < 63) { // while u and v are - // both even... - u /= 2; - v /= 2; - k++; // cast out twos. - } - if (k == 63) { - throw new MathArithmeticException(LocalizedFormats.GCD_OVERFLOW_64_BITS, - p, q); - } - // B2. Initialize: u and v have been divided by 2^k and at least - // one is odd. - long t = ((u & 1) == 1) ? v : -(u / 2)/* B3 */; - // t negative: u was odd, v may be even (t replaces v) - // t positive: u was even, v is odd (t replaces u) - do { - /* assert u<0 && v<0; */ - // B4/B3: cast out twos from t. - while ((t & 1) == 0) { // while t is even.. - t /= 2; // cast out twos - } - // B5 [reset max(u,v)] - if (t > 0) { - u = -t; - } else { - v = t; - } - // B6/B3. at this point both u and v should be odd. - t = (v - u) / 2; - // |u| larger: t positive (replace u) - // |v| larger: t negative (replace v) - } while (t != 0); - return -u * (1L << k); // gcd is u*2^k - } - - /** - * <p> - * Returns the least common multiple of the absolute value of two numbers, - * using the formula {@code lcm(a,b) = (a / gcd(a,b)) * b}. - * </p> - * Special cases: - * <ul> - * <li>The invocations {@code lcm(Integer.MIN_VALUE, n)} and - * {@code lcm(n, Integer.MIN_VALUE)}, where {@code abs(n)} is a - * power of 2, throw an {@code ArithmeticException}, because the result - * would be 2^31, which is too large for an int value.</li> - * <li>The result of {@code lcm(0, x)} and {@code lcm(x, 0)} is - * {@code 0} for any {@code x}. - * </ul> - * - * @param a Number. - * @param b Number. - * @return the least common multiple, never negative. - * @throws MathArithmeticException if the result cannot be represented as - * a non-negative {@code int} value. - * @since 1.1 - */ - public static int lcm(int a, int b) throws MathArithmeticException { - if (a == 0 || b == 0){ - return 0; - } - int lcm = FastMath.abs(ArithmeticUtils.mulAndCheck(a / gcd(a, b), b)); - if (lcm == Integer.MIN_VALUE) { - throw new MathArithmeticException(LocalizedFormats.LCM_OVERFLOW_32_BITS, - a, b); - } - return lcm; - } - - /** - * <p> - * Returns the least common multiple of the absolute value of two numbers, - * using the formula {@code lcm(a,b) = (a / gcd(a,b)) * b}. - * </p> - * Special cases: - * <ul> - * <li>The invocations {@code lcm(Long.MIN_VALUE, n)} and - * {@code lcm(n, Long.MIN_VALUE)}, where {@code abs(n)} is a - * power of 2, throw an {@code ArithmeticException}, because the result - * would be 2^63, which is too large for an int value.</li> - * <li>The result of {@code lcm(0L, x)} and {@code lcm(x, 0L)} is - * {@code 0L} for any {@code x}. - * </ul> - * - * @param a Number. - * @param b Number. - * @return the least common multiple, never negative. - * @throws MathArithmeticException if the result cannot be represented - * as a non-negative {@code long} value. - * @since 2.1 - */ - public static long lcm(long a, long b) throws MathArithmeticException { - if (a == 0 || b == 0){ - return 0; - } - long lcm = FastMath.abs(ArithmeticUtils.mulAndCheck(a / gcd(a, b), b)); - if (lcm == Long.MIN_VALUE){ - throw new MathArithmeticException(LocalizedFormats.LCM_OVERFLOW_64_BITS, - a, b); - } - return lcm; - } - - /** - * Multiply two integers, checking for overflow. - * - * @param x Factor. - * @param y Factor. - * @return the product {@code x * y}. - * @throws MathArithmeticException if the result can not be - * represented as an {@code int}. - * @since 1.1 - */ - public static int mulAndCheck(int x, int y) throws MathArithmeticException { - long m = ((long)x) * ((long)y); - if (m < Integer.MIN_VALUE || m > Integer.MAX_VALUE) { - throw new MathArithmeticException(); - } - return (int)m; - } - - /** - * Multiply two long integers, checking for overflow. - * - * @param a Factor. - * @param b Factor. - * @return the product {@code a * b}. - * @throws MathArithmeticException if the result can not be represented - * as a {@code long}. - * @since 1.2 - */ - public static long mulAndCheck(long a, long b) throws MathArithmeticException { - long ret; - if (a > b) { - // use symmetry to reduce boundary cases - ret = mulAndCheck(b, a); - } else { - if (a < 0) { - if (b < 0) { - // check for positive overflow with negative a, negative b - if (a >= Long.MAX_VALUE / b) { - ret = a * b; - } else { - throw new MathArithmeticException(); - } - } else if (b > 0) { - // check for negative overflow with negative a, positive b - if (Long.MIN_VALUE / b <= a) { - ret = a * b; - } else { - throw new MathArithmeticException(); - - } - } else { - // assert b == 0 - ret = 0; - } - } else if (a > 0) { - // assert a > 0 - // assert b > 0 - - // check for positive overflow with positive a, positive b - if (a <= Long.MAX_VALUE / b) { - ret = a * b; - } else { - throw new MathArithmeticException(); - } - } else { - // assert a == 0 - ret = 0; - } - } - return ret; - } - - /** - * Subtract two integers, checking for overflow. - * - * @param x Minuend. - * @param y Subtrahend. - * @return the difference {@code x - y}. - * @throws MathArithmeticException if the result can not be represented - * as an {@code int}. - * @since 1.1 - */ - public static int subAndCheck(int x, int y) throws MathArithmeticException { - long s = (long)x - (long)y; - if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) { - throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_SUBTRACTION, x, y); - } - return (int)s; - } - - /** - * Subtract two long integers, checking for overflow. - * - * @param a Value. - * @param b Value. - * @return the difference {@code a - b}. - * @throws MathArithmeticException if the result can not be represented as a - * {@code long}. - * @since 1.2 - */ - public static long subAndCheck(long a, long b) throws MathArithmeticException { - long ret; - if (b == Long.MIN_VALUE) { - if (a < 0) { - ret = a - b; - } else { - throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_ADDITION, a, -b); - } - } else { - // use additive inverse - ret = addAndCheck(a, -b, LocalizedFormats.OVERFLOW_IN_ADDITION); - } - return ret; - } - - /** - * Raise an int to an int power. - * - * @param k Number to raise. - * @param e Exponent (must be positive or zero). - * @return \( k^e \) - * @throws NotPositiveException if {@code e < 0}. - * @throws MathArithmeticException if the result would overflow. - */ - public static int pow(final int k, - final int e) - throws NotPositiveException, - MathArithmeticException { - if (e < 0) { - throw new NotPositiveException(LocalizedFormats.EXPONENT, e); - } - - try { - int exp = e; - int result = 1; - int k2p = k; - while (true) { - if ((exp & 0x1) != 0) { - result = mulAndCheck(result, k2p); - } - - exp >>= 1; - if (exp == 0) { - break; - } - - k2p = mulAndCheck(k2p, k2p); - } - - return result; - } catch (MathArithmeticException mae) { - // Add context information. - mae.getContext().addMessage(LocalizedFormats.OVERFLOW); - mae.getContext().addMessage(LocalizedFormats.BASE, k); - mae.getContext().addMessage(LocalizedFormats.EXPONENT, e); - - // Rethrow. - throw mae; - } - } - - /** - * Raise a long to an int power. - * - * @param k Number to raise. - * @param e Exponent (must be positive or zero). - * @return \( k^e \) - * @throws NotPositiveException if {@code e < 0}. - * @throws MathArithmeticException if the result would overflow. - */ - public static long pow(final long k, - final int e) - throws NotPositiveException, - MathArithmeticException { - if (e < 0) { - throw new NotPositiveException(LocalizedFormats.EXPONENT, e); - } - - try { - int exp = e; - long result = 1; - long k2p = k; - while (true) { - if ((exp & 0x1) != 0) { - result = mulAndCheck(result, k2p); - } - - exp >>= 1; - if (exp == 0) { - break; - } - - k2p = mulAndCheck(k2p, k2p); - } - - return result; - } catch (MathArithmeticException mae) { - // Add context information. - mae.getContext().addMessage(LocalizedFormats.OVERFLOW); - mae.getContext().addMessage(LocalizedFormats.BASE, k); - mae.getContext().addMessage(LocalizedFormats.EXPONENT, e); - - // Rethrow. - throw mae; - } - } - - /** - * Raise a BigInteger to an int power. - * - * @param k Number to raise. - * @param e Exponent (must be positive or zero). - * @return k<sup>e</sup> - * @throws NotPositiveException if {@code e < 0}. - */ - public static BigInteger pow(final BigInteger k, int e) throws NotPositiveException { - if (e < 0) { - throw new NotPositiveException(LocalizedFormats.EXPONENT, e); - } - - return k.pow(e); - } - - /** - * Raise a BigInteger to a long power. - * - * @param k Number to raise. - * @param e Exponent (must be positive or zero). - * @return k<sup>e</sup> - * @throws NotPositiveException if {@code e < 0}. - */ - public static BigInteger pow(final BigInteger k, long e) throws NotPositiveException { - if (e < 0) { - throw new NotPositiveException(LocalizedFormats.EXPONENT, e); - } - - BigInteger result = BigInteger.ONE; - BigInteger k2p = k; - while (e != 0) { - if ((e & 0x1) != 0) { - result = result.multiply(k2p); - } - k2p = k2p.multiply(k2p); - e >>= 1; - } - - return result; - - } - - /** - * Raise a BigInteger to a BigInteger power. - * - * @param k Number to raise. - * @param e Exponent (must be positive or zero). - * @return k<sup>e</sup> - * @throws NotPositiveException if {@code e < 0}. - */ - public static BigInteger pow(final BigInteger k, BigInteger e) throws NotPositiveException { - if (e.compareTo(BigInteger.ZERO) < 0) { - throw new NotPositiveException(LocalizedFormats.EXPONENT, e); - } - - BigInteger result = BigInteger.ONE; - BigInteger k2p = k; - while (!BigInteger.ZERO.equals(e)) { - if (e.testBit(0)) { - result = result.multiply(k2p); - } - k2p = k2p.multiply(k2p); - e = e.shiftRight(1); - } - - return result; - } - - /** - * Add two long integers, checking for overflow. - * - * @param a Addend. - * @param b Addend. - * @param pattern Pattern to use for any thrown exception. - * @return the sum {@code a + b}. - * @throws MathArithmeticException if the result cannot be represented - * as a {@code long}. - * @since 1.2 - */ - private static long addAndCheck(long a, long b, Localizable pattern) throws MathArithmeticException { - final long result = a + b; - if (!((a ^ b) < 0 || (a ^ result) >= 0)) { - throw new MathArithmeticException(pattern, a, b); - } - return result; - } - - /** - * Returns true if the argument is a power of two. - * - * @param n the number to test - * @return true if the argument is a power of two - */ - public static boolean isPowerOfTwo(long n) { - return (n > 0) && ((n & (n - 1)) == 0); - } - - /** - * Returns the unsigned remainder from dividing the first argument - * by the second where each argument and the result is interpreted - * as an unsigned value. - * <p>This method does not use the {@code long} datatype.</p> - * - * @param dividend the value to be divided - * @param divisor the value doing the dividing - * @return the unsigned remainder of the first argument divided by - * the second argument. - * - * @since 4.0 - */ - public static int remainderUnsigned(int dividend, int divisor) { - if (divisor >= 0) { - if (dividend >= 0) { - return dividend % divisor; - } - // The implementation is a Java port of algorithm described in the book - // "Hacker's Delight" (section "Unsigned short division from signed division"). - int q = ((dividend >>> 1) / divisor) << 1; - dividend -= q * divisor; - if (dividend < 0 || dividend >= divisor) { - dividend -= divisor; - } - return dividend; - } - return dividend >= 0 || dividend < divisor ? dividend : dividend - divisor; - } - - /** - * Returns the unsigned remainder from dividing the first argument - * by the second where each argument and the result is interpreted - * as an unsigned value. - * <p>This method does not use the {@code BigInteger} datatype.</p> - * - * @param dividend the value to be divided - * @param divisor the value doing the dividing - * @return the unsigned remainder of the first argument divided by - * the second argument. - * - * @since 4.0 - */ - public static long remainderUnsigned(long dividend, long divisor) { - if (divisor >= 0L) { - if (dividend >= 0L) { - return dividend % divisor; - } - // The implementation is a Java port of algorithm described in the book - // "Hacker's Delight" (section "Unsigned short division from signed division"). - long q = ((dividend >>> 1) / divisor) << 1; - dividend -= q * divisor; - if (dividend < 0L || dividend >= divisor) { - dividend -= divisor; - } - return dividend; - } - return dividend >= 0L || dividend < divisor ? dividend : dividend - divisor; - } - - /** - * Returns the unsigned quotient of dividing the first argument by - * the second where each argument and the result is interpreted as - * an unsigned value. - * <p>Note that in two's complement arithmetic, the three other - * basic arithmetic operations of add, subtract, and multiply are - * bit-wise identical if the two operands are regarded as both - * being signed or both being unsigned. Therefore separate {@code - * addUnsigned}, etc. methods are not provided.</p> - * <p>This method does not use the {@code long} datatype.</p> - * - * @param dividend the value to be divided - * @param divisor the value doing the dividing - * @return the unsigned quotient of the first argument divided by - * the second argument - * - * @since 4.0 - */ - public static int divideUnsigned(int dividend, int divisor) { - if (divisor >= 0) { - if (dividend >= 0) { - return dividend / divisor; - } - // The implementation is a Java port of algorithm described in the book - // "Hacker's Delight" (section "Unsigned short division from signed division"). - int q = ((dividend >>> 1) / divisor) << 1; - dividend -= q * divisor; - if (dividend < 0L || dividend >= divisor) { - q++; - } - return q; - } - return dividend >= 0 || dividend < divisor ? 0 : 1; - } - - /** - * Returns the unsigned quotient of dividing the first argument by - * the second where each argument and the result is interpreted as - * an unsigned value. - * <p>Note that in two's complement arithmetic, the three other - * basic arithmetic operations of add, subtract, and multiply are - * bit-wise identical if the two operands are regarded as both - * being signed or both being unsigned. Therefore separate {@code - * addUnsigned}, etc. methods are not provided.</p> - * <p>This method does not use the {@code BigInteger} datatype.</p> - * - * @param dividend the value to be divided - * @param divisor the value doing the dividing - * @return the unsigned quotient of the first argument divided by - * the second argument. - * - * @since 4.0 - */ - public static long divideUnsigned(long dividend, long divisor) { - if (divisor >= 0L) { - if (dividend >= 0L) { - return dividend / divisor; - } - // The implementation is a Java port of algorithm described in the book - // "Hacker's Delight" (section "Unsigned short division from signed division"). - long q = ((dividend >>> 1) / divisor) << 1; - dividend -= q * divisor; - if (dividend < 0L || dividend >= divisor) { - q++; - } - return q; - } - return dividend >= 0L || dividend < divisor ? 0L : 1L; - } - -} http://git-wip-us.apache.org/repos/asf/commons-math/blob/50d29391/src/main/java/org/apache/commons/math4/util/Combinations.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/util/Combinations.java b/src/main/java/org/apache/commons/math4/util/Combinations.java index d88a393..bf8a423 100644 --- a/src/main/java/org/apache/commons/math4/util/Combinations.java +++ b/src/main/java/org/apache/commons/math4/util/Combinations.java @@ -22,6 +22,7 @@ import java.util.Comparator; import java.util.Iterator; import java.util.NoSuchElementException; +import org.apache.commons.numbers.core.ArithmeticUtils; import org.apache.commons.math4.exception.DimensionMismatchException; import org.apache.commons.math4.exception.MathInternalError; import org.apache.commons.math4.exception.OutOfRangeException; http://git-wip-us.apache.org/repos/asf/commons-math/blob/50d29391/src/main/java/org/apache/commons/math4/util/CombinatoricsUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/util/CombinatoricsUtils.java b/src/main/java/org/apache/commons/math4/util/CombinatoricsUtils.java index 0594dee..6645ed9 100644 --- a/src/main/java/org/apache/commons/math4/util/CombinatoricsUtils.java +++ b/src/main/java/org/apache/commons/math4/util/CombinatoricsUtils.java @@ -19,6 +19,7 @@ package org.apache.commons.math4.util; import java.util.Iterator; import java.util.concurrent.atomic.AtomicReference; +import org.apache.commons.numbers.core.ArithmeticUtils; import org.apache.commons.math4.exception.MathArithmeticException; import org.apache.commons.math4.exception.NotPositiveException; import org.apache.commons.math4.exception.NumberIsTooLargeException; http://git-wip-us.apache.org/repos/asf/commons-math/blob/50d29391/src/test/java/org/apache/commons/math4/analysis/differentiation/DerivativeStructureTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/analysis/differentiation/DerivativeStructureTest.java b/src/test/java/org/apache/commons/math4/analysis/differentiation/DerivativeStructureTest.java index e926cc3..7d2a1be 100644 --- a/src/test/java/org/apache/commons/math4/analysis/differentiation/DerivativeStructureTest.java +++ b/src/test/java/org/apache/commons/math4/analysis/differentiation/DerivativeStructureTest.java @@ -28,7 +28,7 @@ import org.apache.commons.math4.exception.DimensionMismatchException; import org.apache.commons.math4.exception.NumberIsTooLargeException; import org.apache.commons.rng.UniformRandomProvider; import org.apache.commons.rng.simple.RandomSource; -import org.apache.commons.math4.util.ArithmeticUtils; +import org.apache.commons.numbers.core.ArithmeticUtils; import org.apache.commons.math4.util.CombinatoricsUtils; import org.apache.commons.math4.util.FastMath; import org.apache.commons.numbers.core.Precision; http://git-wip-us.apache.org/repos/asf/commons-math/blob/50d29391/src/test/java/org/apache/commons/math4/fraction/FractionTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/fraction/FractionTest.java b/src/test/java/org/apache/commons/math4/fraction/FractionTest.java index 971e740..136ab1f 100644 --- a/src/test/java/org/apache/commons/math4/fraction/FractionTest.java +++ b/src/test/java/org/apache/commons/math4/fraction/FractionTest.java @@ -52,13 +52,13 @@ public class FractionTest { try { new Fraction(Integer.MIN_VALUE, -1); Assert.fail(); - } catch (MathArithmeticException ex) { + } catch (ArithmeticException ex) { // success } try { new Fraction(1, Integer.MIN_VALUE); Assert.fail(); - } catch (MathArithmeticException ex) { + } catch (ArithmeticException ex) { // success } @@ -295,8 +295,8 @@ public class FractionTest { f = new Fraction(0, 3); try { f = f.reciprocal(); - Assert.fail("expecting MathArithmeticException"); - } catch (MathArithmeticException ex) {} + Assert.fail("expecting ArithmeticException"); + } catch (ArithmeticException ex) {} // large values f = new Fraction(Integer.MAX_VALUE, 1); @@ -328,8 +328,8 @@ public class FractionTest { f = new Fraction(Integer.MIN_VALUE, 1); try { f = f.negate(); - Assert.fail("expecting MathArithmeticException"); - } catch (MathArithmeticException ex) {} + Assert.fail("expecting ArithmeticException"); + } catch (ArithmeticException ex) {} } @Test @@ -384,35 +384,35 @@ public class FractionTest { try { f = f.add(Fraction.ONE); // should overflow - Assert.fail("expecting MathArithmeticException but got: " + f.toString()); - } catch (MathArithmeticException ex) {} + Assert.fail("expecting ArithmeticException but got: " + f.toString()); + } catch (ArithmeticException ex) {} // denominator should not be a multiple of 2 or 3 to trigger overflow f1 = new Fraction(Integer.MIN_VALUE, 5); f2 = new Fraction(-1,5); try { f = f1.add(f2); // should overflow - Assert.fail("expecting MathArithmeticException but got: " + f.toString()); - } catch (MathArithmeticException ex) {} + Assert.fail("expecting ArithmeticException but got: " + f.toString()); + } catch (ArithmeticException ex) {} try { f= new Fraction(-Integer.MAX_VALUE, 1); f = f.add(f); - Assert.fail("expecting MathArithmeticException"); - } catch (MathArithmeticException ex) {} + Assert.fail("expecting ArithmeticException"); + } catch (ArithmeticException ex) {} try { f= new Fraction(-Integer.MAX_VALUE, 1); f = f.add(f); - Assert.fail("expecting MathArithmeticException"); - } catch (MathArithmeticException ex) {} + Assert.fail("expecting ArithmeticException"); + } catch (ArithmeticException ex) {} f1 = new Fraction(3,327680); f2 = new Fraction(2,59049); try { f = f1.add(f2); // should overflow - Assert.fail("expecting MathArithmeticException but got: " + f.toString()); - } catch (MathArithmeticException ex) {} + Assert.fail("expecting ArithmeticException but got: " + f.toString()); + } catch (ArithmeticException ex) {} } @Test @@ -429,8 +429,8 @@ public class FractionTest { Fraction f2 = Fraction.ZERO; try { f1.divide(f2); - Assert.fail("expecting MathArithmeticException"); - } catch (MathArithmeticException ex) {} + Assert.fail("expecting ArithmeticException"); + } catch (ArithmeticException ex) {} f1 = new Fraction(0, 5); f2 = new Fraction(2, 7); @@ -462,13 +462,13 @@ public class FractionTest { try { f1 = new Fraction(1, Integer.MAX_VALUE); f = f1.divide(f1.reciprocal()); // should overflow - Assert.fail("expecting MathArithmeticException"); - } catch (MathArithmeticException ex) {} + Assert.fail("expecting ArithmeticException"); + } catch (ArithmeticException ex) {} try { f1 = new Fraction(1, -Integer.MAX_VALUE); f = f1.divide(f1.reciprocal()); // should overflow - Assert.fail("expecting MathArithmeticException"); - } catch (MathArithmeticException ex) {} + Assert.fail("expecting ArithmeticException"); + } catch (ArithmeticException ex) {} f1 = new Fraction(6, 35); f = f1.divide(15); @@ -547,35 +547,35 @@ public class FractionTest { f1 = new Fraction(1, Integer.MAX_VALUE); f2 = new Fraction(1, Integer.MAX_VALUE - 1); f = f1.subtract(f2); - Assert.fail("expecting MathArithmeticException"); //should overflow - } catch (MathArithmeticException ex) {} + Assert.fail("expecting ArithmeticException"); //should overflow + } catch (ArithmeticException ex) {} // denominator should not be a multiple of 2 or 3 to trigger overflow f1 = new Fraction(Integer.MIN_VALUE, 5); f2 = new Fraction(1,5); try { f = f1.subtract(f2); // should overflow - Assert.fail("expecting MathArithmeticException but got: " + f.toString()); - } catch (MathArithmeticException ex) {} + Assert.fail("expecting ArithmeticException but got: " + f.toString()); + } catch (ArithmeticException ex) {} try { f= new Fraction(Integer.MIN_VALUE, 1); f = f.subtract(Fraction.ONE); - Assert.fail("expecting MathArithmeticException"); - } catch (MathArithmeticException ex) {} + Assert.fail("expecting ArithmeticException"); + } catch (ArithmeticException ex) {} try { f= new Fraction(Integer.MAX_VALUE, 1); f = f.subtract(Fraction.ONE.negate()); - Assert.fail("expecting MathArithmeticException"); - } catch (MathArithmeticException ex) {} + Assert.fail("expecting ArithmeticException"); + } catch (ArithmeticException ex) {} f1 = new Fraction(3,327680); f2 = new Fraction(2,59049); try { f = f1.subtract(f2); // should overflow - Assert.fail("expecting MathArithmeticException but got: " + f.toString()); - } catch (MathArithmeticException ex) {} + Assert.fail("expecting ArithmeticException but got: " + f.toString()); + } catch (ArithmeticException ex) {} } @Test @@ -599,8 +599,8 @@ public class FractionTest { Assert.assertTrue(Fraction.ZERO.equals(Fraction.getReducedFraction(0, -1))); try { Fraction.getReducedFraction(1, 0); - Assert.fail("expecting MathArithmeticException"); - } catch (MathArithmeticException ex) { + Assert.fail("expecting ArithmeticException"); + } catch (ArithmeticException ex) { // expected } Assert.assertEquals(Fraction.getReducedFraction http://git-wip-us.apache.org/repos/asf/commons-math/blob/50d29391/src/test/java/org/apache/commons/math4/linear/InverseHilbertMatrix.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/linear/InverseHilbertMatrix.java b/src/test/java/org/apache/commons/math4/linear/InverseHilbertMatrix.java index 0eba604..141f34b 100644 --- a/src/test/java/org/apache/commons/math4/linear/InverseHilbertMatrix.java +++ b/src/test/java/org/apache/commons/math4/linear/InverseHilbertMatrix.java @@ -20,7 +20,7 @@ import org.apache.commons.math4.exception.DimensionMismatchException; import org.apache.commons.math4.linear.ArrayRealVector; import org.apache.commons.math4.linear.RealLinearOperator; import org.apache.commons.math4.linear.RealVector; -import org.apache.commons.math4.util.ArithmeticUtils; +import org.apache.commons.numbers.core.ArithmeticUtils; import org.apache.commons.math4.util.CombinatoricsUtils; /** http://git-wip-us.apache.org/repos/asf/commons-math/blob/50d29391/src/test/java/org/apache/commons/math4/util/ArithmeticUtilsTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/util/ArithmeticUtilsTest.java b/src/test/java/org/apache/commons/math4/util/ArithmeticUtilsTest.java deleted file mode 100644 index 261fe64..0000000 --- a/src/test/java/org/apache/commons/math4/util/ArithmeticUtilsTest.java +++ /dev/null @@ -1,786 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.math4.util; - -import java.util.Arrays; -import java.math.BigInteger; - -import org.apache.commons.rng.UniformRandomProvider; -import org.apache.commons.rng.simple.RandomSource; -import org.apache.commons.rng.sampling.PermutationSampler; -import org.apache.commons.math4.exception.MathArithmeticException; -import org.apache.commons.math4.exception.MathIllegalArgumentException; -import org.junit.Assert; -import org.junit.Test; - -/** - * Test cases for the {@link ArithmeticUtils} class. - * - */ -public class ArithmeticUtilsTest { - - @Test - public void testAddAndCheck() { - int big = Integer.MAX_VALUE; - int bigNeg = Integer.MIN_VALUE; - Assert.assertEquals(big, ArithmeticUtils.addAndCheck(big, 0)); - try { - ArithmeticUtils.addAndCheck(big, 1); - Assert.fail("Expecting MathArithmeticException"); - } catch (MathArithmeticException ex) { - } - try { - ArithmeticUtils.addAndCheck(bigNeg, -1); - Assert.fail("Expecting MathArithmeticException"); - } catch (MathArithmeticException ex) { - } - } - - @Test - public void testAddAndCheckLong() { - long max = Long.MAX_VALUE; - long min = Long.MIN_VALUE; - Assert.assertEquals(max, ArithmeticUtils.addAndCheck(max, 0L)); - Assert.assertEquals(min, ArithmeticUtils.addAndCheck(min, 0L)); - Assert.assertEquals(max, ArithmeticUtils.addAndCheck(0L, max)); - Assert.assertEquals(min, ArithmeticUtils.addAndCheck(0L, min)); - Assert.assertEquals(1, ArithmeticUtils.addAndCheck(-1L, 2L)); - Assert.assertEquals(1, ArithmeticUtils.addAndCheck(2L, -1L)); - Assert.assertEquals(-3, ArithmeticUtils.addAndCheck(-2L, -1L)); - Assert.assertEquals(min, ArithmeticUtils.addAndCheck(min + 1, -1L)); - Assert.assertEquals(-1, ArithmeticUtils.addAndCheck(min, max)); - testAddAndCheckLongFailure(max, 1L); - testAddAndCheckLongFailure(min, -1L); - testAddAndCheckLongFailure(1L, max); - testAddAndCheckLongFailure(-1L, min); - testAddAndCheckLongFailure(max, max); - testAddAndCheckLongFailure(min, min); - } - - @Test - public void testGcd() { - int a = 30; - int b = 50; - int c = 77; - - Assert.assertEquals(0, ArithmeticUtils.gcd(0, 0)); - - Assert.assertEquals(b, ArithmeticUtils.gcd(0, b)); - Assert.assertEquals(a, ArithmeticUtils.gcd(a, 0)); - Assert.assertEquals(b, ArithmeticUtils.gcd(0, -b)); - Assert.assertEquals(a, ArithmeticUtils.gcd(-a, 0)); - - Assert.assertEquals(10, ArithmeticUtils.gcd(a, b)); - Assert.assertEquals(10, ArithmeticUtils.gcd(-a, b)); - Assert.assertEquals(10, ArithmeticUtils.gcd(a, -b)); - Assert.assertEquals(10, ArithmeticUtils.gcd(-a, -b)); - - Assert.assertEquals(1, ArithmeticUtils.gcd(a, c)); - Assert.assertEquals(1, ArithmeticUtils.gcd(-a, c)); - Assert.assertEquals(1, ArithmeticUtils.gcd(a, -c)); - Assert.assertEquals(1, ArithmeticUtils.gcd(-a, -c)); - - Assert.assertEquals(3 * (1<<15), ArithmeticUtils.gcd(3 * (1<<20), 9 * (1<<15))); - - Assert.assertEquals(Integer.MAX_VALUE, ArithmeticUtils.gcd(Integer.MAX_VALUE, 0)); - Assert.assertEquals(Integer.MAX_VALUE, ArithmeticUtils.gcd(-Integer.MAX_VALUE, 0)); - Assert.assertEquals(1<<30, ArithmeticUtils.gcd(1<<30, -Integer.MIN_VALUE)); - try { - // gcd(Integer.MIN_VALUE, 0) > Integer.MAX_VALUE - ArithmeticUtils.gcd(Integer.MIN_VALUE, 0); - Assert.fail("expecting MathArithmeticException"); - } catch (MathArithmeticException expected) { - // expected - } - try { - // gcd(0, Integer.MIN_VALUE) > Integer.MAX_VALUE - ArithmeticUtils.gcd(0, Integer.MIN_VALUE); - Assert.fail("expecting MathArithmeticException"); - } catch (MathArithmeticException expected) { - // expected - } - try { - // gcd(Integer.MIN_VALUE, Integer.MIN_VALUE) > Integer.MAX_VALUE - ArithmeticUtils.gcd(Integer.MIN_VALUE, Integer.MIN_VALUE); - Assert.fail("expecting MathArithmeticException"); - } catch (MathArithmeticException expected) { - // expected - } - } - - @Test - public void testGcdConsistency() { - int[] primeList = {19, 23, 53, 67, 73, 79, 101, 103, 111, 131}; - - final UniformRandomProvider rng = RandomSource.create(RandomSource.WELL_19937_A); - for (int i = 0; i < 20; i++) { - PermutationSampler.shuffle(rng, primeList); - int p1 = primeList[0]; - int p2 = primeList[1]; - int p3 = primeList[2]; - int p4 = primeList[3]; - int i1 = p1 * p2 * p3; - int i2 = p1 * p2 * p4; - int gcd = p1 * p2; - Assert.assertEquals(gcd, ArithmeticUtils.gcd(i1, i2)); - long l1 = i1; - long l2 = i2; - Assert.assertEquals(gcd, ArithmeticUtils.gcd(l1, l2)); - } - } - - @Test - public void testGcdLong(){ - long a = 30; - long b = 50; - long c = 77; - - Assert.assertEquals(0, ArithmeticUtils.gcd(0L, 0)); - - Assert.assertEquals(b, ArithmeticUtils.gcd(0, b)); - Assert.assertEquals(a, ArithmeticUtils.gcd(a, 0)); - Assert.assertEquals(b, ArithmeticUtils.gcd(0, -b)); - Assert.assertEquals(a, ArithmeticUtils.gcd(-a, 0)); - - Assert.assertEquals(10, ArithmeticUtils.gcd(a, b)); - Assert.assertEquals(10, ArithmeticUtils.gcd(-a, b)); - Assert.assertEquals(10, ArithmeticUtils.gcd(a, -b)); - Assert.assertEquals(10, ArithmeticUtils.gcd(-a, -b)); - - Assert.assertEquals(1, ArithmeticUtils.gcd(a, c)); - Assert.assertEquals(1, ArithmeticUtils.gcd(-a, c)); - Assert.assertEquals(1, ArithmeticUtils.gcd(a, -c)); - Assert.assertEquals(1, ArithmeticUtils.gcd(-a, -c)); - - Assert.assertEquals(3L * (1L<<45), ArithmeticUtils.gcd(3L * (1L<<50), 9L * (1L<<45))); - - Assert.assertEquals(1L<<45, ArithmeticUtils.gcd(1L<<45, Long.MIN_VALUE)); - - Assert.assertEquals(Long.MAX_VALUE, ArithmeticUtils.gcd(Long.MAX_VALUE, 0L)); - Assert.assertEquals(Long.MAX_VALUE, ArithmeticUtils.gcd(-Long.MAX_VALUE, 0L)); - Assert.assertEquals(1, ArithmeticUtils.gcd(60247241209L, 153092023L)); - try { - // gcd(Long.MIN_VALUE, 0) > Long.MAX_VALUE - ArithmeticUtils.gcd(Long.MIN_VALUE, 0); - Assert.fail("expecting MathArithmeticException"); - } catch (MathArithmeticException expected) { - // expected - } - try { - // gcd(0, Long.MIN_VALUE) > Long.MAX_VALUE - ArithmeticUtils.gcd(0, Long.MIN_VALUE); - Assert.fail("expecting MathArithmeticException"); - } catch (MathArithmeticException expected) { - // expected - } - try { - // gcd(Long.MIN_VALUE, Long.MIN_VALUE) > Long.MAX_VALUE - ArithmeticUtils.gcd(Long.MIN_VALUE, Long.MIN_VALUE); - Assert.fail("expecting MathArithmeticException"); - } catch (MathArithmeticException expected) { - // expected - } - } - - - @Test - public void testLcm() { - int a = 30; - int b = 50; - int c = 77; - - Assert.assertEquals(0, ArithmeticUtils.lcm(0, b)); - Assert.assertEquals(0, ArithmeticUtils.lcm(a, 0)); - Assert.assertEquals(b, ArithmeticUtils.lcm(1, b)); - Assert.assertEquals(a, ArithmeticUtils.lcm(a, 1)); - Assert.assertEquals(150, ArithmeticUtils.lcm(a, b)); - Assert.assertEquals(150, ArithmeticUtils.lcm(-a, b)); - Assert.assertEquals(150, ArithmeticUtils.lcm(a, -b)); - Assert.assertEquals(150, ArithmeticUtils.lcm(-a, -b)); - Assert.assertEquals(2310, ArithmeticUtils.lcm(a, c)); - - // Assert that no intermediate value overflows: - // The naive implementation of lcm(a,b) would be (a*b)/gcd(a,b) - Assert.assertEquals((1<<20)*15, ArithmeticUtils.lcm((1<<20)*3, (1<<20)*5)); - - // Special case - Assert.assertEquals(0, ArithmeticUtils.lcm(0, 0)); - - try { - // lcm == abs(MIN_VALUE) cannot be represented as a nonnegative int - ArithmeticUtils.lcm(Integer.MIN_VALUE, 1); - Assert.fail("Expecting MathArithmeticException"); - } catch (MathArithmeticException expected) { - // expected - } - - try { - // lcm == abs(MIN_VALUE) cannot be represented as a nonnegative int - ArithmeticUtils.lcm(Integer.MIN_VALUE, 1<<20); - Assert.fail("Expecting MathArithmeticException"); - } catch (MathArithmeticException expected) { - // expected - } - - try { - ArithmeticUtils.lcm(Integer.MAX_VALUE, Integer.MAX_VALUE - 1); - Assert.fail("Expecting MathArithmeticException"); - } catch (MathArithmeticException expected) { - // expected - } - } - - @Test - public void testLcmLong() { - long a = 30; - long b = 50; - long c = 77; - - Assert.assertEquals(0, ArithmeticUtils.lcm(0, b)); - Assert.assertEquals(0, ArithmeticUtils.lcm(a, 0)); - Assert.assertEquals(b, ArithmeticUtils.lcm(1, b)); - Assert.assertEquals(a, ArithmeticUtils.lcm(a, 1)); - Assert.assertEquals(150, ArithmeticUtils.lcm(a, b)); - Assert.assertEquals(150, ArithmeticUtils.lcm(-a, b)); - Assert.assertEquals(150, ArithmeticUtils.lcm(a, -b)); - Assert.assertEquals(150, ArithmeticUtils.lcm(-a, -b)); - Assert.assertEquals(2310, ArithmeticUtils.lcm(a, c)); - - Assert.assertEquals(Long.MAX_VALUE, ArithmeticUtils.lcm(60247241209L, 153092023L)); - - // Assert that no intermediate value overflows: - // The naive implementation of lcm(a,b) would be (a*b)/gcd(a,b) - Assert.assertEquals((1L<<50)*15, ArithmeticUtils.lcm((1L<<45)*3, (1L<<50)*5)); - - // Special case - Assert.assertEquals(0L, ArithmeticUtils.lcm(0L, 0L)); - - try { - // lcm == abs(MIN_VALUE) cannot be represented as a nonnegative int - ArithmeticUtils.lcm(Long.MIN_VALUE, 1); - Assert.fail("Expecting MathArithmeticException"); - } catch (MathArithmeticException expected) { - // expected - } - - try { - // lcm == abs(MIN_VALUE) cannot be represented as a nonnegative int - ArithmeticUtils.lcm(Long.MIN_VALUE, 1<<20); - Assert.fail("Expecting MathArithmeticException"); - } catch (MathArithmeticException expected) { - // expected - } - - Assert.assertEquals((long) Integer.MAX_VALUE * (Integer.MAX_VALUE - 1), - ArithmeticUtils.lcm((long)Integer.MAX_VALUE, Integer.MAX_VALUE - 1)); - try { - ArithmeticUtils.lcm(Long.MAX_VALUE, Long.MAX_VALUE - 1); - Assert.fail("Expecting MathArithmeticException"); - } catch (MathArithmeticException expected) { - // expected - } - } - - @Test - public void testMulAndCheck() { - int big = Integer.MAX_VALUE; - int bigNeg = Integer.MIN_VALUE; - Assert.assertEquals(big, ArithmeticUtils.mulAndCheck(big, 1)); - try { - ArithmeticUtils.mulAndCheck(big, 2); - Assert.fail("Expecting MathArithmeticException"); - } catch (MathArithmeticException ex) { - } - try { - ArithmeticUtils.mulAndCheck(bigNeg, 2); - Assert.fail("Expecting MathArithmeticException"); - } catch (MathArithmeticException ex) { - } - } - - @Test - public void testMulAndCheckLong() { - long max = Long.MAX_VALUE; - long min = Long.MIN_VALUE; - Assert.assertEquals(max, ArithmeticUtils.mulAndCheck(max, 1L)); - Assert.assertEquals(min, ArithmeticUtils.mulAndCheck(min, 1L)); - Assert.assertEquals(0L, ArithmeticUtils.mulAndCheck(max, 0L)); - Assert.assertEquals(0L, ArithmeticUtils.mulAndCheck(min, 0L)); - Assert.assertEquals(max, ArithmeticUtils.mulAndCheck(1L, max)); - Assert.assertEquals(min, ArithmeticUtils.mulAndCheck(1L, min)); - Assert.assertEquals(0L, ArithmeticUtils.mulAndCheck(0L, max)); - Assert.assertEquals(0L, ArithmeticUtils.mulAndCheck(0L, min)); - Assert.assertEquals(1L, ArithmeticUtils.mulAndCheck(-1L, -1L)); - Assert.assertEquals(min, ArithmeticUtils.mulAndCheck(min / 2, 2)); - testMulAndCheckLongFailure(max, 2L); - testMulAndCheckLongFailure(2L, max); - testMulAndCheckLongFailure(min, 2L); - testMulAndCheckLongFailure(2L, min); - testMulAndCheckLongFailure(min, -1L); - testMulAndCheckLongFailure(-1L, min); - } - - @Test - public void testSubAndCheck() { - int big = Integer.MAX_VALUE; - int bigNeg = Integer.MIN_VALUE; - Assert.assertEquals(big, ArithmeticUtils.subAndCheck(big, 0)); - Assert.assertEquals(bigNeg + 1, ArithmeticUtils.subAndCheck(bigNeg, -1)); - Assert.assertEquals(-1, ArithmeticUtils.subAndCheck(bigNeg, -big)); - try { - ArithmeticUtils.subAndCheck(big, -1); - Assert.fail("Expecting MathArithmeticException"); - } catch (MathArithmeticException ex) { - } - try { - ArithmeticUtils.subAndCheck(bigNeg, 1); - Assert.fail("Expecting MathArithmeticException"); - } catch (MathArithmeticException ex) { - } - } - - @Test - public void testSubAndCheckErrorMessage() { - int big = Integer.MAX_VALUE; - try { - ArithmeticUtils.subAndCheck(big, -1); - Assert.fail("Expecting MathArithmeticException"); - } catch (MathArithmeticException ex) { - Assert.assertTrue(ex.getMessage().length() > 1); - } - } - - @Test - public void testSubAndCheckLong() { - long max = Long.MAX_VALUE; - long min = Long.MIN_VALUE; - Assert.assertEquals(max, ArithmeticUtils.subAndCheck(max, 0)); - Assert.assertEquals(min, ArithmeticUtils.subAndCheck(min, 0)); - Assert.assertEquals(-max, ArithmeticUtils.subAndCheck(0, max)); - Assert.assertEquals(min + 1, ArithmeticUtils.subAndCheck(min, -1)); - // min == -1-max - Assert.assertEquals(-1, ArithmeticUtils.subAndCheck(-max - 1, -max)); - Assert.assertEquals(max, ArithmeticUtils.subAndCheck(-1, -1 - max)); - testSubAndCheckLongFailure(0L, min); - testSubAndCheckLongFailure(max, -1L); - testSubAndCheckLongFailure(min, 1L); - } - - @Test - public void testPow() { - - Assert.assertEquals(1801088541, ArithmeticUtils.pow(21, 7)); - Assert.assertEquals(1, ArithmeticUtils.pow(21, 0)); - try { - ArithmeticUtils.pow(21, -7); - Assert.fail("Expecting MathIllegalArgumentException"); - } catch (MathIllegalArgumentException e) { - // expected behavior - } - - Assert.assertEquals(1801088541, ArithmeticUtils.pow(21, 7)); - Assert.assertEquals(1, ArithmeticUtils.pow(21, 0)); - try { - ArithmeticUtils.pow(21, -7); - Assert.fail("Expecting MathIllegalArgumentException"); - } catch (MathIllegalArgumentException e) { - // expected behavior - } - - Assert.assertEquals(1801088541l, ArithmeticUtils.pow(21l, 7)); - Assert.assertEquals(1l, ArithmeticUtils.pow(21l, 0)); - try { - ArithmeticUtils.pow(21l, -7); - Assert.fail("Expecting MathIllegalArgumentException"); - } catch (MathIllegalArgumentException e) { - // expected behavior - } - - BigInteger twentyOne = BigInteger.valueOf(21l); - Assert.assertEquals(BigInteger.valueOf(1801088541l), ArithmeticUtils.pow(twentyOne, 7)); - Assert.assertEquals(BigInteger.ONE, ArithmeticUtils.pow(twentyOne, 0)); - try { - ArithmeticUtils.pow(twentyOne, -7); - Assert.fail("Expecting MathIllegalArgumentException"); - } catch (MathIllegalArgumentException e) { - // expected behavior - } - - Assert.assertEquals(BigInteger.valueOf(1801088541l), ArithmeticUtils.pow(twentyOne, 7l)); - Assert.assertEquals(BigInteger.ONE, ArithmeticUtils.pow(twentyOne, 0l)); - try { - ArithmeticUtils.pow(twentyOne, -7l); - Assert.fail("Expecting MathIllegalArgumentException"); - } catch (MathIllegalArgumentException e) { - // expected behavior - } - - Assert.assertEquals(BigInteger.valueOf(1801088541l), ArithmeticUtils.pow(twentyOne, BigInteger.valueOf(7l))); - Assert.assertEquals(BigInteger.ONE, ArithmeticUtils.pow(twentyOne, BigInteger.ZERO)); - try { - ArithmeticUtils.pow(twentyOne, BigInteger.valueOf(-7l)); - Assert.fail("Expecting MathIllegalArgumentException"); - } catch (MathIllegalArgumentException e) { - // expected behavior - } - - BigInteger bigOne = - new BigInteger("1543786922199448028351389769265814882661837148" + - "4763915343722775611762713982220306372888519211" + - "560905579993523402015636025177602059044911261"); - Assert.assertEquals(bigOne, ArithmeticUtils.pow(twentyOne, 103)); - Assert.assertEquals(bigOne, ArithmeticUtils.pow(twentyOne, 103l)); - Assert.assertEquals(bigOne, ArithmeticUtils.pow(twentyOne, BigInteger.valueOf(103l))); - - } - - @Test(expected=MathArithmeticException.class) - public void testPowIntOverflow() { - ArithmeticUtils.pow(21, 8); - } - - @Test - public void testPowInt() { - final int base = 21; - - Assert.assertEquals(85766121L, - ArithmeticUtils.pow(base, 6)); - Assert.assertEquals(1801088541L, - ArithmeticUtils.pow(base, 7)); - } - - @Test(expected=MathArithmeticException.class) - public void testPowNegativeIntOverflow() { - ArithmeticUtils.pow(-21, 8); - } - - @Test - public void testPowNegativeInt() { - final int base = -21; - - Assert.assertEquals(85766121, - ArithmeticUtils.pow(base, 6)); - Assert.assertEquals(-1801088541, - ArithmeticUtils.pow(base, 7)); - } - - @Test - public void testPowMinusOneInt() { - final int base = -1; - for (int i = 0; i < 100; i++) { - final int pow = ArithmeticUtils.pow(base, i); - Assert.assertEquals("i: " + i, i % 2 == 0 ? 1 : -1, pow); - } - } - - @Test - public void testPowOneInt() { - final int base = 1; - for (int i = 0; i < 100; i++) { - final int pow = ArithmeticUtils.pow(base, i); - Assert.assertEquals("i: " + i, 1, pow); - } - } - - @Test(expected=MathArithmeticException.class) - public void testPowLongOverflow() { - ArithmeticUtils.pow(21, 15); - } - - @Test - public void testPowLong() { - final long base = 21; - - Assert.assertEquals(154472377739119461L, - ArithmeticUtils.pow(base, 13)); - Assert.assertEquals(3243919932521508681L, - ArithmeticUtils.pow(base, 14)); - } - - @Test(expected=MathArithmeticException.class) - public void testPowNegativeLongOverflow() { - ArithmeticUtils.pow(-21L, 15); - } - - @Test - public void testPowNegativeLong() { - final long base = -21; - - Assert.assertEquals(-154472377739119461L, - ArithmeticUtils.pow(base, 13)); - Assert.assertEquals(3243919932521508681L, - ArithmeticUtils.pow(base, 14)); - } - - @Test - public void testPowMinusOneLong() { - final long base = -1; - for (int i = 0; i < 100; i++) { - final long pow = ArithmeticUtils.pow(base, i); - Assert.assertEquals("i: " + i, i % 2 == 0 ? 1 : -1, pow); - } - } - - @Test - public void testPowOneLong() { - final long base = 1; - for (int i = 0; i < 100; i++) { - final long pow = ArithmeticUtils.pow(base, i); - Assert.assertEquals("i: " + i, 1, pow); - } - } - - @Test - public void testIsPowerOfTwo() { - final int n = 1025; - final boolean[] expected = new boolean[n]; - Arrays.fill(expected, false); - for (int i = 1; i < expected.length; i *= 2) { - expected[i] = true; - } - for (int i = 0; i < expected.length; i++) { - final boolean actual = ArithmeticUtils.isPowerOfTwo(i); - Assert.assertTrue(Integer.toString(i), actual == expected[i]); - } - } - - private void testAddAndCheckLongFailure(long a, long b) { - try { - ArithmeticUtils.addAndCheck(a, b); - Assert.fail("Expecting MathArithmeticException"); - } catch (MathArithmeticException ex) { - // success - } - } - - private void testMulAndCheckLongFailure(long a, long b) { - try { - ArithmeticUtils.mulAndCheck(a, b); - Assert.fail("Expecting MathArithmeticException"); - } catch (MathArithmeticException ex) { - // success - } - } - - private void testSubAndCheckLongFailure(long a, long b) { - try { - ArithmeticUtils.subAndCheck(a, b); - Assert.fail("Expecting MathArithmeticException"); - } catch (MathArithmeticException ex) { - // success - } - } - - /** - * Testing helper method. - * @return an array of int numbers containing corner cases:<ul> - * <li>values near the beginning of int range,</li> - * <li>values near the end of int range,</li> - * <li>values near zero</li> - * <li>and some randomly distributed values.</li> - * </ul> - */ - private static int[] getIntSpecialCases() { - int ints[] = new int[100]; - int i = 0; - ints[i++] = Integer.MAX_VALUE; - ints[i++] = Integer.MAX_VALUE - 1; - ints[i++] = 100; - ints[i++] = 101; - ints[i++] = 102; - ints[i++] = 300; - ints[i++] = 567; - for (int j = 0; j < 20; j++) { - ints[i++] = j; - } - for (int j = i - 1; j >= 0; j--) { - ints[i++] = ints[j] > 0 ? -ints[j] : Integer.MIN_VALUE; - } - java.util.Random r = new java.util.Random(System.nanoTime()); - for (; i < ints.length;) { - ints[i++] = r.nextInt(); - } - return ints; - } - - /** - * Testing helper method. - * @return an array of long numbers containing corner cases:<ul> - * <li>values near the beginning of long range,</li> - * <li>values near the end of long range,</li> - * <li>values near the beginning of int range,</li> - * <li>values near the end of int range,</li> - * <li>values near zero</li> - * <li>and some randomly distributed values.</li> - * </ul> - */ - private static long[] getLongSpecialCases() { - long longs[] = new long[100]; - int i = 0; - longs[i++] = Long.MAX_VALUE; - longs[i++] = Long.MAX_VALUE - 1L; - longs[i++] = (long) Integer.MAX_VALUE + 1L; - longs[i++] = Integer.MAX_VALUE; - longs[i++] = Integer.MAX_VALUE - 1; - longs[i++] = 100L; - longs[i++] = 101L; - longs[i++] = 102L; - longs[i++] = 300L; - longs[i++] = 567L; - for (int j = 0; j < 20; j++) { - longs[i++] = j; - } - for (int j = i - 1; j >= 0; j--) { - longs[i++] = longs[j] > 0L ? -longs[j] : Long.MIN_VALUE; - } - java.util.Random r = new java.util.Random(System.nanoTime()); - for (; i < longs.length;) { - longs[i++] = r.nextLong(); - } - return longs; - } - - private static long toUnsignedLong(int number) { - return number < 0 ? 0x100000000L + (long)number : (long)number; - } - - private static int remainderUnsignedExpected(int dividend, int divisor) { - return (int)remainderUnsignedExpected(toUnsignedLong(dividend), toUnsignedLong(divisor)); - } - - private static int divideUnsignedExpected(int dividend, int divisor) { - return (int)divideUnsignedExpected(toUnsignedLong(dividend), toUnsignedLong(divisor)); - } - - private static BigInteger toUnsignedBigInteger(long number) { - return number < 0L ? BigInteger.ONE.shiftLeft(64).add(BigInteger.valueOf(number)) : BigInteger.valueOf(number); - } - - private static long remainderUnsignedExpected(long dividend, long divisor) { - return toUnsignedBigInteger(dividend).remainder(toUnsignedBigInteger(divisor)).longValue(); - } - - private static long divideUnsignedExpected(long dividend, long divisor) { - return toUnsignedBigInteger(dividend).divide(toUnsignedBigInteger(divisor)).longValue(); - } - - @Test(timeout=5000L) - public void testRemainderUnsignedInt() { - Assert.assertEquals(36, ArithmeticUtils.remainderUnsigned(-2147479015, 63)); - Assert.assertEquals(6, ArithmeticUtils.remainderUnsigned(-2147479015, 25)); - } - - @Test(timeout=5000L) - public void testRemainderUnsignedIntSpecialCases() { - int ints[] = getIntSpecialCases(); - for (int dividend : ints) { - for (int divisor : ints) { - if (divisor == 0) { - try { - ArithmeticUtils.remainderUnsigned(dividend, divisor); - Assert.fail("Should have failed with ArithmeticException: division by zero"); - } catch (ArithmeticException e) { - // Success. - } - } else { - Assert.assertEquals(remainderUnsignedExpected(dividend, divisor), ArithmeticUtils.remainderUnsigned(dividend, divisor)); - } - } - } - } - - @Test(timeout=5000L) - public void testRemainderUnsignedLong() { - Assert.assertEquals(48L, ArithmeticUtils.remainderUnsigned(-2147479015L, 63L)); - } - - @Test//(timeout=5000L) - public void testRemainderUnsignedLongSpecialCases() { - long longs[] = getLongSpecialCases(); - for (long dividend : longs) { - for (long divisor : longs) { - if (divisor == 0L) { - try { - ArithmeticUtils.remainderUnsigned(dividend, divisor); - Assert.fail("Should have failed with ArithmeticException: division by zero"); - } catch (ArithmeticException e) { - // Success. - } - } else { - Assert.assertEquals(remainderUnsignedExpected(dividend, divisor), ArithmeticUtils.remainderUnsigned(dividend, divisor)); - } - } - } - } - - @Test(timeout=5000L) - public void testDivideUnsignedInt() { - Assert.assertEquals(34087115, ArithmeticUtils.divideUnsigned(-2147479015, 63)); - Assert.assertEquals(85899531, ArithmeticUtils.divideUnsigned(-2147479015, 25)); - Assert.assertEquals(2147483646, ArithmeticUtils.divideUnsigned(-3, 2)); - Assert.assertEquals(330382098, ArithmeticUtils.divideUnsigned(-16, 13)); - Assert.assertEquals(306783377, ArithmeticUtils.divideUnsigned(-16, 14)); - Assert.assertEquals(2, ArithmeticUtils.divideUnsigned(-1, 2147483647)); - Assert.assertEquals(2, ArithmeticUtils.divideUnsigned(-2, 2147483647)); - Assert.assertEquals(1, ArithmeticUtils.divideUnsigned(-3, 2147483647)); - Assert.assertEquals(1, ArithmeticUtils.divideUnsigned(-16, 2147483647)); - Assert.assertEquals(1, ArithmeticUtils.divideUnsigned(-16, 2147483646)); - } - - @Test(timeout=5000L) - public void testDivideUnsignedIntSpecialCases() { - int ints[] = getIntSpecialCases(); - for (int dividend : ints) { - for (int divisor : ints) { - if (divisor == 0) { - try { - ArithmeticUtils.divideUnsigned(dividend, divisor); - Assert.fail("Should have failed with ArithmeticException: division by zero"); - } catch (ArithmeticException e) { - // Success. - } - } else { - Assert.assertEquals(divideUnsignedExpected(dividend, divisor), ArithmeticUtils.divideUnsigned(dividend, divisor)); - } - } - } - } - - @Test(timeout=5000L) - public void testDivideUnsignedLong() { - Assert.assertEquals(292805461453366231L, ArithmeticUtils.divideUnsigned(-2147479015L, 63L)); - } - - @Test(timeout=5000L) - public void testDivideUnsignedLongSpecialCases() { - long longs[] = getLongSpecialCases(); - for (long dividend : longs) { - for (long divisor : longs) { - if (divisor == 0L) { - try { - ArithmeticUtils.divideUnsigned(dividend, divisor); - Assert.fail("Should have failed with ArithmeticException: division by zero"); - } catch (ArithmeticException e) { - // Success. - } - } else { - Assert.assertEquals(divideUnsignedExpected(dividend, divisor), ArithmeticUtils.divideUnsigned(dividend, divisor)); - } - } - } - } -} http://git-wip-us.apache.org/repos/asf/commons-math/blob/50d29391/src/test/java/org/apache/commons/math4/util/CombinatoricsUtilsTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/util/CombinatoricsUtilsTest.java b/src/test/java/org/apache/commons/math4/util/CombinatoricsUtilsTest.java index faaf786..56d4b45 100644 --- a/src/test/java/org/apache/commons/math4/util/CombinatoricsUtilsTest.java +++ b/src/test/java/org/apache/commons/math4/util/CombinatoricsUtilsTest.java @@ -25,7 +25,7 @@ import org.apache.commons.math4.exception.MathArithmeticException; import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.NotPositiveException; import org.apache.commons.math4.exception.NumberIsTooLargeException; -import org.apache.commons.math4.util.ArithmeticUtils; +import org.apache.commons.numbers.core.ArithmeticUtils; import org.apache.commons.math4.util.CombinatoricsUtils; import org.apache.commons.math4.util.FastMath; import org.junit.Assert; @@ -137,14 +137,14 @@ public class CombinatoricsUtilsTest { try { CombinatoricsUtils.binomialCoefficient(67, 30); - Assert.fail("expecting MathArithmeticException"); - } catch (MathArithmeticException ex) { + Assert.fail("expecting ArithmeticException"); + } catch (ArithmeticException ex) { // ignored } try { CombinatoricsUtils.binomialCoefficient(67, 34); - Assert.fail("expecting MathArithmeticException"); - } catch (MathArithmeticException ex) { + Assert.fail("expecting ArithmeticException"); + } catch (ArithmeticException ex) { // ignored } double x = CombinatoricsUtils.binomialCoefficientDouble(1030, 515); @@ -167,12 +167,12 @@ public class CombinatoricsUtilsTest { boolean didThrow = false; try { ourResult = CombinatoricsUtils.binomialCoefficient(n, k); - } catch (MathArithmeticException ex) { + } catch (ArithmeticException ex) { didThrow = true; } try { exactResult = binomialCoefficient(n, k); - } catch (MathArithmeticException ex) { + } catch (ArithmeticException ex) { shouldThrow = true; } Assert.assertEquals(n + " choose " + k, exactResult, ourResult); @@ -199,8 +199,8 @@ public class CombinatoricsUtilsTest { // This one should throw try { CombinatoricsUtils.binomialCoefficient(700, 300); - Assert.fail("Expecting MathArithmeticException"); - } catch (MathArithmeticException ex) { + Assert.fail("Expecting ArithmeticException"); + } catch (ArithmeticException ex) { // Expected } @@ -248,8 +248,8 @@ public class CombinatoricsUtilsTest { } try { CombinatoricsUtils.factorial(21); - Assert.fail("expecting MathArithmeticException"); - } catch (MathArithmeticException ex) { + Assert.fail("expecting ArithmeticException"); + } catch (ArithmeticException ex) { // ignored } Assert.assertTrue("expecting infinite factorial value", Double.isInfinite(CombinatoricsUtils.factorialDouble(171))); @@ -307,7 +307,7 @@ public class CombinatoricsUtilsTest { CombinatoricsUtils.stirlingS2(3, 4); } - @Test(expected=MathArithmeticException.class) + @Test(expected=ArithmeticException.class) public void testStirlingS2Overflow() { CombinatoricsUtils.stirlingS2(26, 9); } @@ -333,7 +333,7 @@ public class CombinatoricsUtilsTest { /** * Exact (caching) recursive implementation to test against */ - private long binomialCoefficient(int n, int k) throws MathArithmeticException { + private long binomialCoefficient(int n, int k) throws ArithmeticException { if (binomialCache.size() > n) { Long cachedResult = binomialCache.get(n).get(Integer.valueOf(k)); if (cachedResult != null) { @@ -357,7 +357,7 @@ public class CombinatoricsUtilsTest { binomialCoefficient(n - 1, k)); } if (result == -1) { - throw new MathArithmeticException(); + throw new ArithmeticException(); } for (int i = binomialCache.size(); i < n + 1; i++) { binomialCache.add(new HashMap<Integer, Long>()); http://git-wip-us.apache.org/repos/asf/commons-math/blob/50d29391/src/test/java/org/apache/commons/math4/util/FastMathTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/util/FastMathTest.java b/src/test/java/org/apache/commons/math4/util/FastMathTest.java index 702510a..9035f34 100644 --- a/src/test/java/org/apache/commons/math4/util/FastMathTest.java +++ b/src/test/java/org/apache/commons/math4/util/FastMathTest.java @@ -27,6 +27,7 @@ import java.math.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; +import org.apache.commons.numbers.core.ArithmeticUtils; import org.apache.commons.numbers.core.Precision; import org.apache.commons.math4.TestUtils; import org.apache.commons.math4.dfp.Dfp;
