1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
|
/*
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4851776 4907265 6177836 6876282 8066842
* @summary Some tests for the divide methods.
* @author Joseph D. Darcy
*/
import java.math.*;
import static java.math.BigDecimal.*;
public class DivideTests {
// Preliminary exact divide method; could be used for comparison
// purposes.
BigDecimal anotherDivide(BigDecimal dividend, BigDecimal divisor) {
/*
* Handle zero cases first.
*/
if (divisor.signum() == 0) { // x/0
if (dividend.signum() == 0) // 0/0
throw new ArithmeticException("Division undefined"); // NaN
throw new ArithmeticException("Division by zero");
}
if (dividend.signum() == 0) // 0/y
return BigDecimal.ZERO;
else {
/*
* Determine if there is a result with a terminating
* decimal expansion. Putting aside overflow and
* underflow considerations, the existance of an exact
* result only depends on the ratio of the intVal's of the
* dividend (i.e. this) and and divisor since the scales
* of the argument just affect where the decimal point
* lies.
*
* For the ratio of (a = this.intVal) and (b =
* divisor.intVal) to have a finite decimal expansion,
* once a/b is put in lowest terms, b must be equal to
* (2^i)*(5^j) for some integer i,j >= 0. Therefore, we
* first compute to see if b_prime =(b/gcd(a,b)) is equal
* to (2^i)*(5^j).
*/
BigInteger TWO = BigInteger.valueOf(2);
BigInteger FIVE = BigInteger.valueOf(5);
BigInteger TEN = BigInteger.valueOf(10);
BigInteger divisorIntvalue = divisor.scaleByPowerOfTen(divisor.scale()).toBigInteger().abs();
BigInteger dividendIntvalue = dividend.scaleByPowerOfTen(dividend.scale()).toBigInteger().abs();
BigInteger b_prime = divisorIntvalue.divide(dividendIntvalue.gcd(divisorIntvalue));
boolean goodDivisor = false;
int i=0, j=0;
badDivisor: {
while(! b_prime.equals(BigInteger.ONE) ) {
int b_primeModTen = b_prime.mod(TEN).intValue() ;
switch(b_primeModTen) {
case 0:
// b_prime divisible by 10=2*5, increment i and j
i++;
j++;
b_prime = b_prime.divide(TEN);
break;
case 5:
// b_prime divisible by 5, increment j
j++;
b_prime = b_prime.divide(FIVE);
break;
case 2:
case 4:
case 6:
case 8:
// b_prime divisible by 2, increment i
i++;
b_prime = b_prime.divide(TWO);
break;
default: // hit something we shouldn't have
b_prime = BigInteger.ONE; // terminate loop
break badDivisor;
}
}
goodDivisor = true;
}
if( ! goodDivisor ) {
throw new ArithmeticException("Non terminating decimal expansion");
}
else {
// What is a rule for determining how many digits are
// needed? Once that is determined, cons up a new
// MathContext object and pass it on to the divide(bd,
// mc) method; precision == ?, roundingMode is unnecessary.
// Are we sure this is the right scale to use? Should
// also determine a precision-based method.
MathContext mc = new MathContext(dividend.precision() +
(int)Math.ceil(
10.0*divisor.precision()/3.0),
RoundingMode.UNNECESSARY);
// Should do some more work here to rescale, etc.
return dividend.divide(divisor, mc);
}
}
}
public static int powersOf2and5() {
int failures = 0;
for(int i = 0; i < 6; i++) {
int powerOf2 = (int)StrictMath.pow(2.0, i);
for(int j = 0; j < 6; j++) {
int powerOf5 = (int)StrictMath.pow(5.0, j);
int product;
BigDecimal bd;
try {
bd = BigDecimal.ONE.divide(new BigDecimal(product=powerOf2*powerOf5));
} catch (ArithmeticException e) {
failures++;
System.err.println((new BigDecimal(powerOf2)).toString() + " / " +
(new BigDecimal(powerOf5)).toString() + " threw an exception.");
e.printStackTrace();
}
try {
bd = new BigDecimal(powerOf2).divide(new BigDecimal(powerOf5));
} catch (ArithmeticException e) {
failures++;
System.err.println((new BigDecimal(powerOf2)).toString() + " / " +
(new BigDecimal(powerOf5)).toString() + " threw an exception.");
e.printStackTrace();
}
try {
bd = new BigDecimal(powerOf5).divide(new BigDecimal(powerOf2));
} catch (ArithmeticException e) {
failures++;
System.err.println((new BigDecimal(powerOf5)).toString() + " / " +
(new BigDecimal(powerOf2)).toString() + " threw an exception.");
e.printStackTrace();
}
}
}
return failures;
}
public static int nonTerminating() {
int failures = 0;
int[] primes = {1, 3, 7, 13, 17};
// For each pair of prime products, verify the ratio of
// non-equal products has a non-terminating expansion.
for(int i = 0; i < primes.length; i++) {
for(int j = i+1; j < primes.length; j++) {
for(int m = 0; m < primes.length; m++) {
for(int n = m+1; n < primes.length; n++) {
int dividend = primes[i] * primes[j];
int divisor = primes[m] * primes[n];
if ( ((dividend/divisor) * divisor) != dividend ) {
try {
BigDecimal quotient = (new BigDecimal(dividend).
divide(new BigDecimal(divisor)));
failures++;
System.err.println("Exact quotient " + quotient.toString() +
" returned for non-terminating fraction " +
dividend + " / " + divisor + ".");
}
catch (ArithmeticException e) {
; // Correct result
}
}
}
}
}
}
return failures;
}
public static int properScaleTests(){
int failures = 0;
BigDecimal[][] testCases = {
{new BigDecimal("1"), new BigDecimal("5"), new BigDecimal("2e-1")},
{new BigDecimal("1"), new BigDecimal("50e-1"), new BigDecimal("2e-1")},
{new BigDecimal("10e-1"), new BigDecimal("5"), new BigDecimal("2e-1")},
{new BigDecimal("1"), new BigDecimal("500e-2"), new BigDecimal("2e-1")},
{new BigDecimal("100e-2"), new BigDecimal("5"), new BigDecimal("20e-2")},
{new BigDecimal("1"), new BigDecimal("32"), new BigDecimal("3125e-5")},
{new BigDecimal("1"), new BigDecimal("64"), new BigDecimal("15625e-6")},
{new BigDecimal("1.0000000"), new BigDecimal("64"), new BigDecimal("156250e-7")},
};
for(BigDecimal[] tc : testCases) {
BigDecimal quotient;
if (! (quotient = tc[0].divide(tc[1])).equals(tc[2]) ) {
failures++;
System.err.println("Unexpected quotient from " + tc[0] + " / " + tc[1] +
"; expected " + tc[2] + " got " + quotient);
}
}
return failures;
}
public static int trailingZeroTests() {
int failures = 0;
MathContext mc = new MathContext(3, RoundingMode.FLOOR);
BigDecimal[][] testCases = {
{new BigDecimal("19"), new BigDecimal("100"), new BigDecimal("0.19")},
{new BigDecimal("21"), new BigDecimal("110"), new BigDecimal("0.190")},
};
for(BigDecimal[] tc : testCases) {
BigDecimal quotient;
if (! (quotient = tc[0].divide(tc[1], mc)).equals(tc[2]) ) {
failures++;
System.err.println("Unexpected quotient from " + tc[0] + " / " + tc[1] +
"; expected " + tc[2] + " got " + quotient);
}
}
return failures;
}
public static int scaledRoundedDivideTests() {
int failures = 0;
// Tests of the traditional scaled divide under different
// rounding modes.
// Encode rounding mode and scale for the divide in a
// BigDecimal with the significand equal to the rounding mode
// and the scale equal to the number's scale.
// {dividend, dividisor, rounding, quotient}
BigDecimal a = new BigDecimal("31415");
BigDecimal a_minus = a.negate();
BigDecimal b = new BigDecimal("10000");
BigDecimal c = new BigDecimal("31425");
BigDecimal c_minus = c.negate();
// Ad hoc tests
BigDecimal d = new BigDecimal(new BigInteger("-37361671119238118911893939591735"), 10);
BigDecimal e = new BigDecimal(new BigInteger("74723342238476237823787879183470"), 15);
BigDecimal[][] testCases = {
{a, b, BigDecimal.valueOf(ROUND_UP, 3), new BigDecimal("3.142")},
{a_minus, b, BigDecimal.valueOf(ROUND_UP, 3), new BigDecimal("-3.142")},
{a, b, BigDecimal.valueOf(ROUND_DOWN, 3), new BigDecimal("3.141")},
{a_minus, b, BigDecimal.valueOf(ROUND_DOWN, 3), new BigDecimal("-3.141")},
{a, b, BigDecimal.valueOf(ROUND_CEILING, 3), new BigDecimal("3.142")},
{a_minus, b, BigDecimal.valueOf(ROUND_CEILING, 3), new BigDecimal("-3.141")},
{a, b, BigDecimal.valueOf(ROUND_FLOOR, 3), new BigDecimal("3.141")},
{a_minus, b, BigDecimal.valueOf(ROUND_FLOOR, 3), new BigDecimal("-3.142")},
{a, b, BigDecimal.valueOf(ROUND_HALF_UP, 3), new BigDecimal("3.142")},
{a_minus, b, BigDecimal.valueOf(ROUND_HALF_UP, 3), new BigDecimal("-3.142")},
{a, b, BigDecimal.valueOf(ROUND_DOWN, 3), new BigDecimal("3.141")},
{a_minus, b, BigDecimal.valueOf(ROUND_DOWN, 3), new BigDecimal("-3.141")},
{a, b, BigDecimal.valueOf(ROUND_HALF_EVEN, 3), new BigDecimal("3.142")},
{a_minus, b, BigDecimal.valueOf(ROUND_HALF_EVEN, 3), new BigDecimal("-3.142")},
{c, b, BigDecimal.valueOf(ROUND_HALF_EVEN, 3), new BigDecimal("3.142")},
{c_minus, b, BigDecimal.valueOf(ROUND_HALF_EVEN, 3), new BigDecimal("-3.142")},
{d, e, BigDecimal.valueOf(ROUND_HALF_UP, -5), BigDecimal.valueOf(-1, -5)},
{d, e, BigDecimal.valueOf(ROUND_HALF_DOWN, -5), BigDecimal.valueOf(0, -5)},
{d, e, BigDecimal.valueOf(ROUND_HALF_EVEN, -5), BigDecimal.valueOf(0, -5)},
};
for(BigDecimal tc[] : testCases) {
int scale = tc[2].scale();
int rm = tc[2].unscaledValue().intValue();
BigDecimal quotient = tc[0].divide(tc[1], scale, rm);
if (!quotient.equals(tc[3])) {
failures++;
System.err.println("Unexpected quotient from " + tc[0] + " / " + tc[1] +
" scale " + scale + " rounding mode " + RoundingMode.valueOf(rm) +
"; expected " + tc[3] + " got " + quotient);
}
}
// 6876282
BigDecimal[][] testCases2 = {
// { dividend, divisor, expected quotient }
{ new BigDecimal(3090), new BigDecimal(7), new BigDecimal(441) },
{ new BigDecimal("309000000000000000000000"), new BigDecimal("700000000000000000000"),
new BigDecimal(441) },
{ new BigDecimal("962.430000000000"), new BigDecimal("8346463.460000000000"),
new BigDecimal("0.000115309916") },
{ new BigDecimal("18446744073709551631"), new BigDecimal("4611686018427387909"),
new BigDecimal(4) },
{ new BigDecimal("18446744073709551630"), new BigDecimal("4611686018427387909"),
new BigDecimal(4) },
{ new BigDecimal("23058430092136939523"), new BigDecimal("4611686018427387905"),
new BigDecimal(5) },
{ new BigDecimal("-18446744073709551661"), new BigDecimal("-4611686018427387919"),
new BigDecimal(4) },
{ new BigDecimal("-18446744073709551660"), new BigDecimal("-4611686018427387919"),
new BigDecimal(4) },
};
for (BigDecimal test[] : testCases2) {
BigDecimal quo = test[0].divide(test[1], RoundingMode.HALF_UP);
if (!quo.equals(test[2])) {
failures++;
System.err.println("Unexpected quotient from " + test[0] + " / " + test[1] +
" rounding mode HALF_UP" +
"; expected " + test[2] + " got " + quo);
}
}
return failures;
}
private static int divideByOneTests() {
int failures = 0;
//problematic divisor: one with scale 17
BigDecimal one = BigDecimal.ONE.setScale(17);
RoundingMode rounding = RoundingMode.UNNECESSARY;
long[][] unscaledAndScale = new long[][] {
{ Long.MAX_VALUE, 17},
{-Long.MAX_VALUE, 17},
{ Long.MAX_VALUE, 0},
{-Long.MAX_VALUE, 0},
{ Long.MAX_VALUE, 100},
{-Long.MAX_VALUE, 100}
};
for (long[] uas : unscaledAndScale) {
long unscaled = uas[0];
int scale = (int)uas[1];
BigDecimal noRound = null;
try {
noRound = BigDecimal.valueOf(unscaled, scale).
divide(one, RoundingMode.UNNECESSARY);
} catch (ArithmeticException e) {
failures++;
System.err.println("ArithmeticException for value " + unscaled
+ " and scale " + scale + " without rounding");
}
BigDecimal roundDown = null;
try {
roundDown = BigDecimal.valueOf(unscaled, scale).
divide(one, RoundingMode.DOWN);
} catch (ArithmeticException e) {
failures++;
System.err.println("ArithmeticException for value " + unscaled
+ " and scale " + scale + " with rounding down");
}
if (noRound != null && roundDown != null
&& noRound.compareTo(roundDown) != 0) {
failures++;
System.err.println("Equality failure for value " + unscaled
+ " and scale " + scale);
}
}
return failures;
}
public static void main(String argv[]) {
int failures = 0;
failures += powersOf2and5();
failures += nonTerminating();
failures += properScaleTests();
failures += trailingZeroTests();
failures += scaledRoundedDivideTests();
failures += divideByOneTests();
if (failures > 0) {
throw new RuntimeException("Incurred " + failures +
" failures while testing division.");
}
}
}
|