809N/A/*
2362N/A * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
809N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
809N/A *
809N/A * This code is free software; you can redistribute it and/or modify it
809N/A * under the terms of the GNU General Public License version 2 only, as
809N/A * published by the Free Software Foundation.
809N/A *
809N/A * This code is distributed in the hope that it will be useful, but WITHOUT
809N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
809N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
809N/A * version 2 for more details (a copy is included in the LICENSE file that
809N/A * accompanied this code).
809N/A *
809N/A * You should have received a copy of the GNU General Public License version
809N/A * 2 along with this work; if not, write to the Free Software Foundation,
809N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
809N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
809N/A */
809N/A
809N/A/*
809N/A * @test
1598N/A * @bug 4851776 4907265 6177836 6876282
809N/A * @summary Some tests for the divide methods.
809N/A * @author Joseph D. Darcy
809N/A */
809N/A
809N/Aimport java.math.*;
809N/Aimport static java.math.BigDecimal.*;
809N/A
809N/Apublic class DivideTests {
809N/A
809N/A // Preliminary exact divide method; could be used for comparison
809N/A // purposes.
809N/A BigDecimal anotherDivide(BigDecimal dividend, BigDecimal divisor) {
809N/A /*
809N/A * Handle zero cases first.
809N/A */
809N/A if (divisor.signum() == 0) { // x/0
809N/A if (dividend.signum() == 0) // 0/0
809N/A throw new ArithmeticException("Division undefined"); // NaN
809N/A throw new ArithmeticException("Division by zero");
809N/A }
809N/A if (dividend.signum() == 0) // 0/y
809N/A return BigDecimal.ZERO;
809N/A else {
809N/A /*
809N/A * Determine if there is a result with a terminating
809N/A * decimal expansion. Putting aside overflow and
809N/A * underflow considerations, the existance of an exact
809N/A * result only depends on the ratio of the intVal's of the
809N/A * dividend (i.e. this) and and divisor since the scales
809N/A * of the argument just affect where the decimal point
809N/A * lies.
809N/A *
809N/A * For the ratio of (a = this.intVal) and (b =
809N/A * divisor.intVal) to have a finite decimal expansion,
809N/A * once a/b is put in lowest terms, b must be equal to
809N/A * (2^i)*(5^j) for some integer i,j >= 0. Therefore, we
809N/A * first compute to see if b_prime =(b/gcd(a,b)) is equal
809N/A * to (2^i)*(5^j).
809N/A */
809N/A BigInteger TWO = BigInteger.valueOf(2);
809N/A BigInteger FIVE = BigInteger.valueOf(5);
809N/A BigInteger TEN = BigInteger.valueOf(10);
809N/A
809N/A BigInteger divisorIntvalue = divisor.scaleByPowerOfTen(divisor.scale()).toBigInteger().abs();
809N/A BigInteger dividendIntvalue = dividend.scaleByPowerOfTen(dividend.scale()).toBigInteger().abs();
809N/A
809N/A BigInteger b_prime = divisorIntvalue.divide(dividendIntvalue.gcd(divisorIntvalue));
809N/A
809N/A boolean goodDivisor = false;
809N/A int i=0, j=0;
809N/A
809N/A badDivisor: {
809N/A while(! b_prime.equals(BigInteger.ONE) ) {
809N/A int b_primeModTen = b_prime.mod(TEN).intValue() ;
809N/A
809N/A switch(b_primeModTen) {
809N/A case 0:
809N/A // b_prime divisible by 10=2*5, increment i and j
809N/A i++;
809N/A j++;
809N/A b_prime = b_prime.divide(TEN);
809N/A break;
809N/A
809N/A case 5:
809N/A // b_prime divisible by 5, increment j
809N/A j++;
809N/A b_prime = b_prime.divide(FIVE);
809N/A break;
809N/A
809N/A case 2:
809N/A case 4:
809N/A case 6:
809N/A case 8:
809N/A // b_prime divisible by 2, increment i
809N/A i++;
809N/A b_prime = b_prime.divide(TWO);
809N/A break;
809N/A
809N/A default: // hit something we shouldn't have
809N/A b_prime = BigInteger.ONE; // terminate loop
809N/A break badDivisor;
809N/A }
809N/A }
809N/A
809N/A goodDivisor = true;
809N/A }
809N/A
809N/A if( ! goodDivisor ) {
809N/A throw new ArithmeticException("Non terminating decimal expansion");
809N/A }
809N/A else {
809N/A // What is a rule for determining how many digits are
809N/A // needed? Once that is determined, cons up a new
809N/A // MathContext object and pass it on to the divide(bd,
809N/A // mc) method; precision == ?, roundingMode is unnecessary.
809N/A
809N/A // Are we sure this is the right scale to use? Should
809N/A // also determine a precision-based method.
809N/A MathContext mc = new MathContext(dividend.precision() +
809N/A (int)Math.ceil(
809N/A 10.0*divisor.precision()/3.0),
809N/A RoundingMode.UNNECESSARY);
809N/A // Should do some more work here to rescale, etc.
809N/A return dividend.divide(divisor, mc);
809N/A }
809N/A }
809N/A }
809N/A
809N/A public static int powersOf2and5() {
809N/A int failures = 0;
809N/A
809N/A for(int i = 0; i < 6; i++) {
809N/A int powerOf2 = (int)StrictMath.pow(2.0, i);
809N/A
809N/A for(int j = 0; j < 6; j++) {
809N/A int powerOf5 = (int)StrictMath.pow(5.0, j);
809N/A int product;
809N/A
809N/A BigDecimal bd;
809N/A
809N/A try {
809N/A bd = BigDecimal.ONE.divide(new BigDecimal(product=powerOf2*powerOf5));
809N/A } catch (ArithmeticException e) {
809N/A failures++;
809N/A System.err.println((new BigDecimal(powerOf2)).toString() + " / " +
809N/A (new BigDecimal(powerOf5)).toString() + " threw an exception.");
809N/A e.printStackTrace();
809N/A }
809N/A
809N/A try {
809N/A bd = new BigDecimal(powerOf2).divide(new BigDecimal(powerOf5));
809N/A } catch (ArithmeticException e) {
809N/A failures++;
809N/A System.err.println((new BigDecimal(powerOf2)).toString() + " / " +
809N/A (new BigDecimal(powerOf5)).toString() + " threw an exception.");
809N/A e.printStackTrace();
809N/A }
809N/A
809N/A try {
809N/A bd = new BigDecimal(powerOf5).divide(new BigDecimal(powerOf2));
809N/A } catch (ArithmeticException e) {
809N/A failures++;
809N/A System.err.println((new BigDecimal(powerOf5)).toString() + " / " +
809N/A (new BigDecimal(powerOf2)).toString() + " threw an exception.");
809N/A
809N/A e.printStackTrace();
809N/A }
809N/A
809N/A }
809N/A }
809N/A return failures;
809N/A }
809N/A
809N/A public static int nonTerminating() {
809N/A int failures = 0;
809N/A int[] primes = {1, 3, 7, 13, 17};
809N/A
809N/A // For each pair of prime products, verify the ratio of
809N/A // non-equal products has a non-terminating expansion.
809N/A
809N/A for(int i = 0; i < primes.length; i++) {
809N/A for(int j = i+1; j < primes.length; j++) {
809N/A
809N/A for(int m = 0; m < primes.length; m++) {
809N/A for(int n = m+1; n < primes.length; n++) {
809N/A int dividend = primes[i] * primes[j];
809N/A int divisor = primes[m] * primes[n];
809N/A
809N/A if ( ((dividend/divisor) * divisor) != dividend ) {
809N/A try {
809N/A BigDecimal quotient = (new BigDecimal(dividend).
809N/A divide(new BigDecimal(divisor)));
809N/A failures++;
809N/A System.err.println("Exact quotient " + quotient.toString() +
809N/A " returned for non-terminating fraction " +
809N/A dividend + " / " + divisor + ".");
809N/A }
809N/A catch (ArithmeticException e) {
809N/A ; // Correct result
809N/A }
809N/A }
809N/A
809N/A }
809N/A }
809N/A }
809N/A }
809N/A
809N/A return failures;
809N/A }
809N/A
809N/A public static int properScaleTests(){
809N/A int failures = 0;
809N/A
809N/A BigDecimal[][] testCases = {
809N/A {new BigDecimal("1"), new BigDecimal("5"), new BigDecimal("2e-1")},
809N/A {new BigDecimal("1"), new BigDecimal("50e-1"), new BigDecimal("2e-1")},
809N/A {new BigDecimal("10e-1"), new BigDecimal("5"), new BigDecimal("2e-1")},
809N/A {new BigDecimal("1"), new BigDecimal("500e-2"), new BigDecimal("2e-1")},
809N/A {new BigDecimal("100e-2"), new BigDecimal("5"), new BigDecimal("20e-2")},
809N/A {new BigDecimal("1"), new BigDecimal("32"), new BigDecimal("3125e-5")},
809N/A {new BigDecimal("1"), new BigDecimal("64"), new BigDecimal("15625e-6")},
809N/A {new BigDecimal("1.0000000"), new BigDecimal("64"), new BigDecimal("156250e-7")},
809N/A };
809N/A
809N/A
809N/A for(BigDecimal[] tc : testCases) {
809N/A BigDecimal quotient;
809N/A if (! (quotient = tc[0].divide(tc[1])).equals(tc[2]) ) {
809N/A failures++;
809N/A System.err.println("Unexpected quotient from " + tc[0] + " / " + tc[1] +
809N/A "; expected " + tc[2] + " got " + quotient);
809N/A }
809N/A }
809N/A
809N/A return failures;
809N/A }
809N/A
809N/A public static int trailingZeroTests() {
809N/A int failures = 0;
809N/A
809N/A MathContext mc = new MathContext(3, RoundingMode.FLOOR);
809N/A BigDecimal[][] testCases = {
809N/A {new BigDecimal("19"), new BigDecimal("100"), new BigDecimal("0.19")},
809N/A {new BigDecimal("21"), new BigDecimal("110"), new BigDecimal("0.190")},
809N/A };
809N/A
809N/A for(BigDecimal[] tc : testCases) {
809N/A BigDecimal quotient;
809N/A if (! (quotient = tc[0].divide(tc[1], mc)).equals(tc[2]) ) {
809N/A failures++;
809N/A System.err.println("Unexpected quotient from " + tc[0] + " / " + tc[1] +
809N/A "; expected " + tc[2] + " got " + quotient);
809N/A }
809N/A }
809N/A
809N/A return failures;
809N/A }
809N/A
809N/A public static int scaledRoundedDivideTests() {
809N/A int failures = 0;
809N/A // Tests of the traditional scaled divide under different
809N/A // rounding modes.
809N/A
809N/A // Encode rounding mode and scale for the divide in a
809N/A // BigDecimal with the significand equal to the rounding mode
809N/A // and the scale equal to the number's scale.
809N/A
809N/A // {dividend, dividisor, rounding, quotient}
809N/A BigDecimal a = new BigDecimal("31415");
809N/A BigDecimal a_minus = a.negate();
809N/A BigDecimal b = new BigDecimal("10000");
809N/A
809N/A BigDecimal c = new BigDecimal("31425");
809N/A BigDecimal c_minus = c.negate();
809N/A
1246N/A // Ad hoc tests
1246N/A BigDecimal d = new BigDecimal(new BigInteger("-37361671119238118911893939591735"), 10);
1246N/A BigDecimal e = new BigDecimal(new BigInteger("74723342238476237823787879183470"), 15);
1246N/A
809N/A BigDecimal[][] testCases = {
809N/A {a, b, BigDecimal.valueOf(ROUND_UP, 3), new BigDecimal("3.142")},
809N/A {a_minus, b, BigDecimal.valueOf(ROUND_UP, 3), new BigDecimal("-3.142")},
809N/A
809N/A {a, b, BigDecimal.valueOf(ROUND_DOWN, 3), new BigDecimal("3.141")},
809N/A {a_minus, b, BigDecimal.valueOf(ROUND_DOWN, 3), new BigDecimal("-3.141")},
809N/A
809N/A {a, b, BigDecimal.valueOf(ROUND_CEILING, 3), new BigDecimal("3.142")},
809N/A {a_minus, b, BigDecimal.valueOf(ROUND_CEILING, 3), new BigDecimal("-3.141")},
809N/A
809N/A {a, b, BigDecimal.valueOf(ROUND_FLOOR, 3), new BigDecimal("3.141")},
809N/A {a_minus, b, BigDecimal.valueOf(ROUND_FLOOR, 3), new BigDecimal("-3.142")},
809N/A
809N/A {a, b, BigDecimal.valueOf(ROUND_HALF_UP, 3), new BigDecimal("3.142")},
809N/A {a_minus, b, BigDecimal.valueOf(ROUND_HALF_UP, 3), new BigDecimal("-3.142")},
809N/A
809N/A {a, b, BigDecimal.valueOf(ROUND_DOWN, 3), new BigDecimal("3.141")},
809N/A {a_minus, b, BigDecimal.valueOf(ROUND_DOWN, 3), new BigDecimal("-3.141")},
809N/A
809N/A {a, b, BigDecimal.valueOf(ROUND_HALF_EVEN, 3), new BigDecimal("3.142")},
809N/A {a_minus, b, BigDecimal.valueOf(ROUND_HALF_EVEN, 3), new BigDecimal("-3.142")},
809N/A
809N/A {c, b, BigDecimal.valueOf(ROUND_HALF_EVEN, 3), new BigDecimal("3.142")},
809N/A {c_minus, b, BigDecimal.valueOf(ROUND_HALF_EVEN, 3), new BigDecimal("-3.142")},
1246N/A
1246N/A {d, e, BigDecimal.valueOf(ROUND_HALF_UP, -5), BigDecimal.valueOf(-1, -5)},
1246N/A {d, e, BigDecimal.valueOf(ROUND_HALF_DOWN, -5), BigDecimal.valueOf(0, -5)},
1246N/A {d, e, BigDecimal.valueOf(ROUND_HALF_EVEN, -5), BigDecimal.valueOf(0, -5)},
809N/A };
809N/A
809N/A for(BigDecimal tc[] : testCases) {
809N/A int scale = tc[2].scale();
809N/A int rm = tc[2].unscaledValue().intValue();
809N/A
809N/A BigDecimal quotient = tc[0].divide(tc[1], scale, rm);
809N/A if (!quotient.equals(tc[3])) {
809N/A failures++;
809N/A System.err.println("Unexpected quotient from " + tc[0] + " / " + tc[1] +
809N/A " scale " + scale + " rounding mode " + RoundingMode.valueOf(rm) +
809N/A "; expected " + tc[3] + " got " + quotient);
809N/A }
809N/A }
809N/A
1598N/A // 6876282
1598N/A BigDecimal[][] testCases2 = {
1598N/A // { dividend, divisor, expected quotient }
1598N/A { new BigDecimal(3090), new BigDecimal(7), new BigDecimal(441) },
1598N/A { new BigDecimal("309000000000000000000000"), new BigDecimal("700000000000000000000"),
1598N/A new BigDecimal(441) },
1598N/A { new BigDecimal("962.430000000000"), new BigDecimal("8346463.460000000000"),
1598N/A new BigDecimal("0.000115309916") },
1598N/A { new BigDecimal("18446744073709551631"), new BigDecimal("4611686018427387909"),
1598N/A new BigDecimal(4) },
1598N/A { new BigDecimal("18446744073709551630"), new BigDecimal("4611686018427387909"),
1598N/A new BigDecimal(4) },
1598N/A { new BigDecimal("23058430092136939523"), new BigDecimal("4611686018427387905"),
1598N/A new BigDecimal(5) },
1598N/A { new BigDecimal("-18446744073709551661"), new BigDecimal("-4611686018427387919"),
1598N/A new BigDecimal(4) },
1598N/A { new BigDecimal("-18446744073709551660"), new BigDecimal("-4611686018427387919"),
1598N/A new BigDecimal(4) },
1598N/A };
1598N/A
1598N/A for (BigDecimal test[] : testCases2) {
1598N/A BigDecimal quo = test[0].divide(test[1], RoundingMode.HALF_UP);
1598N/A if (!quo.equals(test[2])) {
1598N/A failures++;
1598N/A System.err.println("Unexpected quotient from " + test[0] + " / " + test[1] +
1598N/A " rounding mode HALF_UP" +
1598N/A "; expected " + test[2] + " got " + quo);
1598N/A }
1598N/A }
809N/A return failures;
809N/A }
809N/A
809N/A public static void main(String argv[]) {
809N/A int failures = 0;
809N/A
809N/A failures += powersOf2and5();
809N/A failures += nonTerminating();
809N/A failures += properScaleTests();
809N/A failures += trailingZeroTests();
809N/A failures += scaledRoundedDivideTests();
809N/A
809N/A if (failures > 0) {
809N/A throw new RuntimeException("Incurred " + failures +
809N/A " failures while testing exact divide.");
809N/A }
809N/A }
809N/A}