1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package libcore.java.math; 19 20 import java.math.BigDecimal; 21 import java.math.BigInteger; 22 import java.math.MathContext; 23 import java.math.RoundingMode; 24 import junit.framework.TestCase; 25 26 public class OldBigDecimalArithmeticTest extends TestCase { 27 28 public void testAddMathContextNonTrivial() { 29 MathContext mc; 30 BigDecimal a, b, res; 31 32 mc = new MathContext(17, RoundingMode.FLOOR); 33 a = new BigDecimal("123456789012345.678"); 34 b = new BigDecimal("100000000000000.009"); 35 assertEquals("incorrect value", "123456789012345.67", 36 a.round(mc).toString()); 37 assertEquals("incorrect value", "100000000000000.00", 38 b.round(mc).toString()); 39 assertEquals("incorrect value", "223456789012345.67", 40 a.round(mc).add(b.round(mc)).toString()); 41 res = a.add(b, mc); 42 assertEquals("incorrect value", "223456789012345.68", res.toString()); 43 44 mc = new MathContext(33, RoundingMode.UNNECESSARY); 45 a = new BigDecimal("1234567890123456789012345678.9012395"); 46 b = new BigDecimal("1000000000000000090000000000.0000005"); 47 res = a.add(b, mc); 48 assertEquals("Incorrect value!", "2234567890123456879012345678.90124", res.toString()); 49 assertEquals("Incorrect scale!", 5, res.scale()); 50 assertEquals("Incorrect precision!", 33, res.precision()); 51 } 52 53 public void testSubtractMathContextNonTrivial() { 54 MathContext mc; 55 BigDecimal a, b, res; 56 57 mc = new MathContext(17, RoundingMode.FLOOR); 58 a = new BigDecimal("12345678901234567.8"); 59 b = new BigDecimal("10000000000000000.9"); 60 assertEquals("incorrect value", "2345678901234567", 61 a.round(mc).subtract(b.round(mc)).toString()); 62 res = a.subtract(b, mc); 63 assertEquals("incorrect value", "2345678901234566.9", res.toString()); 64 assertEquals("Incorrect scale!", 1, res.scale()); 65 assertEquals("Incorrect precision!", 17, res.precision()); 66 67 mc = new MathContext(33, RoundingMode.UNNECESSARY); 68 a = new BigDecimal("1234567890123456789012345678.9012395"); 69 b = new BigDecimal("1000000000000000090000000000.0000005"); 70 res = a.subtract(b, mc); 71 assertEquals("incorrect value", "234567890123456699012345678.901239", res.toString()); 72 assertEquals("Incorrect scale!", 6, res.scale()); 73 assertEquals("Incorrect precision!", 33, res.precision()); 74 } 75 76 public void testMultiplyMathContextNonTrivial() { 77 MathContext mc; 78 BigDecimal a, b, res; 79 80 mc = new MathContext(17, RoundingMode.FLOOR); 81 a = new BigDecimal("92345678901234567.8"); 82 b = new BigDecimal("10000000000000000.9"); 83 res = a.round(mc).multiply(b.round(mc)); 84 assertEquals("incorrect value", "923456789012345670000000000000000", res.toString()); 85 res = res.round(mc); 86 assertEquals("incorrect value", "9.2345678901234567E+32", res.toString()); 87 res = a.multiply(b, mc); 88 assertEquals("incorrect value", "9.2345678901234576E+32", res.toString()); 89 assertEquals("Incorrect scale!", -16, res.scale()); 90 assertEquals("Incorrect precision!", 17, res.precision()); 91 } 92 93 public void testPowNonTrivial() { 94 BigDecimal a, b, res; 95 96 a = new BigDecimal("100.9"); 97 try { 98 res = a.pow(-1); 99 fail("ArithmeticException is not thrown for negative exponent"); 100 } catch (ArithmeticException e) { 101 // expected 102 } 103 try { 104 res = a.pow(-103); 105 fail("ArithmeticException is not thrown for negative exponent"); 106 } catch (ArithmeticException e) { 107 // expected 108 } 109 } 110 111 public void testPowMathContext() { 112 String a = "123121247898748298842980"; 113 int aScale = 10; 114 int exp = 10; 115 String c = "8.0044E+130"; 116 int cScale = -126; 117 BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); 118 MathContext mc = new MathContext(5, RoundingMode.HALF_UP); 119 BigDecimal result = aNumber.pow(exp, mc); 120 assertEquals("incorrect value", c, result.toString()); 121 assertEquals("incorrect scale", cScale, result.scale()); 122 } 123 124 public void testPowMathContextNonTrivial() { 125 MathContext mc; 126 BigDecimal a, b, res; 127 128 mc = new MathContext(7, RoundingMode.FLOOR); 129 a = new BigDecimal("1000000.9"); 130 assertEquals("incorrect value", "1.000000E+6000", 131 a.round(mc).pow(1000).round(mc).toString()); 132 res = a.pow(1000, mc); 133 assertEquals("incorrect value", "1.000900E+6000", res.toString()); 134 135 mc = new MathContext(4, RoundingMode.FLOOR); 136 a = new BigDecimal("1000.9"); 137 assertEquals("incorrect value", "1.000E+3000", 138 a.round(mc).pow(1000).round(mc).toString()); 139 res = a.pow(1000, mc); 140 assertEquals("incorrect value", "2.458E+3000", res.toString()); 141 142 mc = new MathContext(2, RoundingMode.UNNECESSARY); 143 a = new BigDecimal("1234"); 144 try { 145 res = a.pow(-2, mc); 146 fail("ArithmeticException is not thrown"); 147 } catch (ArithmeticException e) { 148 // expected 149 } 150 151 a = new BigDecimal("100"); 152 mc = new MathContext(4, RoundingMode.UNNECESSARY); 153 res = a.pow(-2, mc); 154 assertEquals("incorrect value", "0.0001", res.toString()); 155 156 a = new BigDecimal("1000.9"); 157 try { 158 mc = new MathContext(0, RoundingMode.FLOOR); 159 res = a.pow(-1, mc); 160 fail("ArithmeticException is not thrown for negative exponent and precision = 0"); 161 } catch (ArithmeticException e) { 162 // expected 163 } 164 165 a = new BigDecimal("000.0001"); 166 try { 167 mc = new MathContext(0, RoundingMode.FLOOR); 168 res = a.pow(-1, mc); 169 fail("ArithmeticException is not thrown for negative exponent and precision = 0"); 170 } catch (ArithmeticException e) { 171 // expected 172 } 173 174 a = new BigDecimal("1E-400"); 175 mc = new MathContext(4, RoundingMode.UNNECESSARY); 176 res = a.pow(-1, mc); 177 assertEquals("incorrect value", "1E+400", res.toString()); 178 179 // Doesn't succeed against JDK of Sun!: 180 // mc = new MathContext(3, RoundingMode.FLOOR); 181 // a = new BigDecimal("100.9"); 182 // assertEquals("incorrect value", "1.00E+2000", 183 // a.round(mc).pow(1000).round(mc).toString()); 184 // res = a.pow(1000).round(mc); 185 // res = a.pow(1000, mc); 186 // assertEquals("incorrect value", "7.783E+2003", res.toString()); 187 } 188 189 public void testDivideINonTrivial() { 190 MathContext mc; 191 BigDecimal a, b, res; 192 193 mc = new MathContext(17, RoundingMode.FLOOR); 194 a = new BigDecimal("12345678901234567E1234"); 195 b = new BigDecimal("1.23456789012345679"); 196 assertEquals("incorrect value", "1E+1250", 197 a.round(mc).divide(b.round(mc)).toString()); 198 res = a.divide(b, BigDecimal.ROUND_FLOOR); 199 assertEquals("incorrect value", "9.999999999999999E+1249", res.toString()); 200 201 a = new BigDecimal("1234567890123456789012345678.9012395"); 202 b = new BigDecimal("6172839450617283945061728394.5061975"); 203 res = a.divide(b, BigDecimal.ROUND_UNNECESSARY); 204 assertEquals("incorrect value", "0.2000000", res.toString()); 205 206 a = new BigDecimal("1234567890123456789012345678.9012395"); 207 b = new BigDecimal("1000000000000000090000000000.0000005"); 208 try { 209 res = a.divide(b, BigDecimal.ROUND_UNNECESSARY); 210 fail("ArithmeticException is not thrown for RoundingMode.UNNECESSARY"); 211 } catch (ArithmeticException e) { 212 // expected 213 } 214 } 215 216 public void testDivideIINonTrivial() { 217 MathContext mc; 218 BigDecimal a, b, res; 219 220 mc = new MathContext(17, RoundingMode.FLOOR); 221 a = new BigDecimal("12345678901234567E1234"); 222 b = new BigDecimal("1.23456789012345679"); 223 res = a.divide(b, -1220, BigDecimal.ROUND_FLOOR); 224 assertEquals("incorrect value", "9.99999999999999927099999343899E+1249", res.toString()); 225 226 a = new BigDecimal("1234567890123456789012345678.9012395"); 227 b = new BigDecimal("6172839450617283945061728394.5061975"); 228 res = a.divide(b, 1, BigDecimal.ROUND_UNNECESSARY); 229 assertEquals("incorrect value", "0.2", res.toString()); 230 231 a = new BigDecimal("1234567890123456789012345678.9012395"); 232 b = new BigDecimal("6172839450617283945061728394.5061975"); 233 try { 234 res = a.divide(b, 0, BigDecimal.ROUND_UNNECESSARY); 235 fail("ArithmeticException is not thrown for RoundingMode.UNNECESSARY"); 236 } catch (ArithmeticException e) { 237 // expected 238 } 239 } 240 241 // Has a rounding problem. seems like the precision is 242 // 1 too small and cuts off the last digit. also this test might 243 // not be correct. The name implies that scale should be used. 244 public void testDivideScaleRoundingModeNonTrivial() { 245 MathContext mc; 246 BigDecimal a, b, res; 247 248 mc = new MathContext(17, RoundingMode.FLOOR); 249 a = new BigDecimal("12345678901234567.1"); 250 b = new BigDecimal("12345678901234567.9"); 251 assertEquals("incorrect value", "1", 252 a.round(mc).divide(b.round(mc)).toString()); 253 res = a.divide(b, mc); 254 assertEquals("incorrect value", "0.99999999999999993", res.toString()); 255 256 mc = new MathContext(13, RoundingMode.UNNECESSARY); 257 a = new BigDecimal("1234567890123456789012345678.9012395"); 258 b = new BigDecimal("6172839450617283945061728394.5061975"); 259 res = a.divide(b, mc); 260 assertEquals("incorrect value", "0.2", res.toString()); 261 262 mc = new MathContext(33, RoundingMode.UNNECESSARY); 263 a = new BigDecimal("1234567890123456789012345678.9012395"); 264 b = new BigDecimal("1000000000000000090000000000.0000005"); 265 try { 266 res = a.divide(b, mc); 267 fail("ArithmeticException is not thrown for RoundingMode.UNNECESSARY"); 268 } catch (ArithmeticException e) { 269 // expected 270 } 271 } 272 273 // The same test and the same problem like testDivideScaleRoundingModeNonTrivial. 274 public void testDivideMathContextNonTrivial() { 275 MathContext mc; 276 BigDecimal a, b, res; 277 278 // FAILS AGAINST RI!: 279 // mc = new MathContext(6, RoundingMode.FLOOR); 280 // a = new BigDecimal("12345.1"); 281 // b = new BigDecimal("12345.9"); 282 // assertEquals("incorrect value", "1", 283 // a.round(mc).divide(b.round(mc)).toString()); 284 // res = a.divide(b, mc); 285 // assertEquals("incorrect value", "0.99993", res.toString()); 286 287 mc = new MathContext(5, RoundingMode.FLOOR); 288 a = new BigDecimal("12345.1"); 289 b = new BigDecimal("12345.9"); 290 assertEquals("incorrect value", "1", 291 a.round(mc).divide(b.round(mc)).toString()); 292 res = a.divide(b, mc); 293 assertEquals("incorrect value", "0.99993", res.toString()); 294 295 mc = new MathContext(17, RoundingMode.FLOOR); 296 a = new BigDecimal("12345678901234567.1"); 297 b = new BigDecimal("12345678901234567.9"); 298 assertEquals("incorrect value", "1", 299 a.round(mc).divide(b.round(mc)).toString()); 300 res = a.divide(b, mc); 301 assertEquals("incorrect value", "0.99999999999999993", res.toString()); 302 assertEquals("incorrect value", res.round(mc).toString(), res.toString()); 303 304 mc = new MathContext(13, RoundingMode.UNNECESSARY); 305 a = new BigDecimal("1234567890123456789012345678.9012395"); 306 b = new BigDecimal("6172839450617283945061728394.5061975"); 307 res = a.divide(b, mc); 308 assertEquals("incorrect value", "0.2", res.toString()); 309 310 mc = new MathContext(33, RoundingMode.UNNECESSARY); 311 a = new BigDecimal("1234567890123456789012345678.9012395"); 312 b = new BigDecimal("1000000000000000090000000000.0000005"); 313 try { 314 res = a.divide(b, mc); 315 fail("ArithmeticException is not thrown for RoundingMode.UNNECESSARY"); 316 } catch (ArithmeticException e) { 317 // expected 318 } 319 } 320 321 public void testDivideToIntegralValueByZero() { 322 String a = "3736186567876876578956958765675671119238118911893939591735"; 323 int aScale = 45; 324 String b = "0"; 325 int bScale = -70; 326 String res = "277923185514690367474770683"; 327 int resScale = 0; 328 String rem = "1.3032693871288309587558885943391070087960319452465789990E-15"; 329 int remScale = 70; 330 BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); 331 BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); 332 try { 333 BigDecimal result = aNumber.divideToIntegralValue(bNumber); 334 fail("ArithmeticException not thrown for division by 0"); 335 } catch (ArithmeticException e) { 336 // expected 337 } 338 } 339 340 public void testDivideToIntegralValueMathContextNonTrivial() { 341 MathContext mc; 342 BigDecimal a, b, res; 343 344 a = new BigDecimal("92345678901234567.8"); 345 b = new BigDecimal("43"); 346 res = a.multiply(b); 347 assertEquals("incorrect value", "3970864192753086415.4", res.toString()); 348 349 mc = new MathContext(20, RoundingMode.DOWN); 350 a = new BigDecimal("3970864192753086415.4"); 351 b = new BigDecimal("92345678901234567.8"); 352 b = new BigDecimal("92345678901234567.8001"); 353 assertEquals("incorrect value", "43", 354 a.round(mc).divideToIntegralValue(b.round(mc)).toString()); 355 res = a.divideToIntegralValue(b, mc); 356 assertEquals("incorrect value", "42", res.toString()); 357 358 // mc = new MathContext(1, RoundingMode.DOWN); 359 // res = a.divideToIntegralValue(b, mc); 360 // assertEquals("incorrect value", "42", res.toString()); 361 362 363 mc = new MathContext(17, RoundingMode.FLOOR); 364 a = new BigDecimal("518518513851851830"); 365 b = new BigDecimal("12345678901234567.9"); 366 assertEquals("incorrect value", "42", 367 a.round(mc).divideToIntegralValue(b.round(mc)).toString()); 368 res = a.divideToIntegralValue(b, mc); 369 assertEquals("incorrect value", "41", res.toString()); 370 } 371 372 /** 373 * divideAndRemainder(BigDecimal) 374 */ 375 public void testDivideAndRemainderByZero() { 376 String a = "3736186567876876578956958765675671119238118911893939591735"; 377 int aScale = 45; 378 String b = "0"; 379 int bScale = -70; 380 String res = "277923185514690367474770683"; 381 int resScale = 0; 382 String rem = "1.3032693871288309587558885943391070087960319452465789990E-15"; 383 int remScale = 70; 384 BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); 385 BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); 386 try { 387 BigDecimal result[] = aNumber.divideAndRemainder(bNumber); 388 fail("ArithmeticException not thrown for division by 0"); 389 } catch (ArithmeticException e) { 390 // expected 391 } 392 } 393 394 public void testDivideAndRemainderMathContextNonTrivial() { 395 MathContext mc; 396 BigDecimal a, b, res[]; 397 398 mc = new MathContext(13, RoundingMode.FLOOR); 399 a = new BigDecimal("12345678901234567.1"); 400 b = new BigDecimal("12345678901234567.9"); 401 assertEquals("incorrect value", "0E+4", 402 a.round(mc).divideAndRemainder(b.round(mc))[1].toString()); 403 res = a.divideAndRemainder(b, mc); 404 assertEquals("incorrect value", "12345678901234567.1", res[1].toString()); 405 406 mc = new MathContext(1, RoundingMode.UNNECESSARY); 407 a = new BigDecimal("6172839450617283945061728394.5061976"); 408 b = new BigDecimal("1234567890123456789012345678.9012395"); 409 res = a.divideAndRemainder(b, mc); 410 assertEquals("incorrect value", "1E-7", res[1].toString()); 411 412 mc = new MathContext(3, RoundingMode.UNNECESSARY); 413 a = new BigDecimal("6172839450617283945061728394.6000000"); 414 b = new BigDecimal("1234567890123456789012345678.9012395"); 415 try { 416 res = a.divideAndRemainder(b, mc); 417 assertEquals("incorrect value", "0.0938025", res[1].toString()); 418 assertEquals("incorrect value", "0.09", res[1].round(mc).toString()); 419 // fail("ArithmeticException is not thrown for RoundingMode.UNNECESSARY"); 420 } catch (ArithmeticException e) { 421 // expected 422 } 423 } 424 public void testRemainderByZero() { 425 String a = "3736186567876876578956958765675671119238118911893939591735"; 426 int aScale = 45; 427 String b = "0"; 428 int bScale = -70; 429 String res = "277923185514690367474770683"; 430 int resScale = 0; 431 String rem = "1.3032693871288309587558885943391070087960319452465789990E-15"; 432 int remScale = 70; 433 BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); 434 BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); 435 try { 436 BigDecimal result = aNumber.remainder(bNumber); 437 fail("ArithmeticException not thrown for division by 0"); 438 } catch (ArithmeticException e) { 439 // expected 440 } 441 } 442 443 public void testRemainderMathContextNonTrivial() { 444 MathContext mc; 445 BigDecimal a, b, res; 446 447 mc = new MathContext(13, RoundingMode.DOWN); 448 a = new BigDecimal("12345678901234567.1"); 449 b = new BigDecimal("12345678901234567.9"); 450 assertEquals("incorrect value", "0E+4", 451 a.round(mc).divideAndRemainder(b.round(mc))[1].toString()); 452 res = a.remainder(b, mc); 453 assertEquals("incorrect value", "12345678901234567.1", res.toString()); 454 455 mc = new MathContext(1, RoundingMode.UNNECESSARY); 456 a = new BigDecimal("6172839450617283945061728394.5061976"); 457 b = new BigDecimal("1234567890123456789012345678.9012395"); 458 res = a.remainder(b, mc); 459 assertEquals("incorrect value", "1E-7", res.toString()); 460 461 mc = new MathContext(3, RoundingMode.UNNECESSARY); 462 a = new BigDecimal("6172839450617283945061728394.6000000"); 463 b = new BigDecimal("1234567890123456789012345678.9012395"); 464 try { 465 res = a.remainder(b, mc); 466 assertEquals("incorrect value", "0.0938025", res.toString()); 467 assertEquals("incorrect value", "0.09", res.round(mc).toString()); 468 // fail("ArithmeticException is not thrown for RoundingMode.UNNECESSARY"); 469 } catch (ArithmeticException e) { 470 // expected 471 } 472 } 473 public void testRoundNonTrivial() { 474 MathContext mc; 475 String biStr = new String( "12345678901234567890123456789012345.0E+10"); 476 String nbiStr = new String("-12345678901234567890123456789012345.E+10"); 477 BigDecimal bd; 478 479 mc = new MathContext(17, RoundingMode.FLOOR); 480 bd = new BigDecimal(new BigInteger("123456789012345678"), 3, mc); 481 assertEquals("incorrect value", "123456789012345.67", bd.toString()); 482 483 mc = new MathContext(31, RoundingMode.UP); 484 bd = (new BigDecimal(biStr)).round(mc); 485 assertEquals("incorrect value", "1.234567890123456789012345678902E+44", bd.toString()); 486 bd = (new BigDecimal(nbiStr)).round(mc); 487 assertEquals("incorrect value", "-1.234567890123456789012345678902E+44", bd.toString()); 488 489 mc = new MathContext(28, RoundingMode.DOWN); 490 bd = (new BigDecimal(biStr)).round(mc); 491 assertEquals("incorrect value", "1.234567890123456789012345678E+44", bd.toString()); 492 bd = (new BigDecimal(nbiStr)).round(mc); 493 assertEquals("incorrect value", "-1.234567890123456789012345678E+44", bd.toString()); 494 495 mc = new MathContext(33, RoundingMode.CEILING); 496 bd = (new BigDecimal(biStr)).round(mc); 497 assertEquals("incorrect value", "1.23456789012345678901234567890124E+44", bd.toString()); 498 bd = (new BigDecimal(nbiStr)).round(mc); 499 assertEquals("incorrect value", "-1.23456789012345678901234567890123E+44", bd.toString()); 500 501 mc = new MathContext(34, RoundingMode.UNNECESSARY); 502 try { 503 bd = (new BigDecimal(biStr)).round(mc); 504 fail("No ArithmeticException for RoundingMode.UNNECESSARY"); 505 } catch (ArithmeticException e) { 506 // expected 507 } 508 try { 509 bd = (new BigDecimal(nbiStr)).round(mc); 510 fail("No ArithmeticException for RoundingMode.UNNECESSARY"); 511 } catch (ArithmeticException e) { 512 // expected 513 } 514 515 mc = new MathContext(7, RoundingMode.FLOOR); 516 bd = new BigDecimal("1000000.9", mc); 517 assertEquals("incorrect value", "1000000", bd.toString()); 518 } 519 /** 520 * java.math.BigDecimal#add(java.math.BigDecimal) 521 */ 522 public void test_addBigDecimal() { 523 BigDecimal add1 = new BigDecimal("23.456"); 524 BigDecimal add2 = new BigDecimal("3849.235"); 525 BigDecimal sum = add1.add(add2); 526 assertTrue("the sum of 23.456 + 3849.235 is wrong", sum.unscaledValue().toString().equals( 527 "3872691") 528 && sum.scale() == 3); 529 assertTrue("the sum of 23.456 + 3849.235 is not printed correctly", sum.toString().equals( 530 "3872.691")); 531 BigDecimal add3 = new BigDecimal(12.34E02D); 532 assertTrue("the sum of 23.456 + 12.34E02 is not printed correctly", (add1.add(add3)) 533 .toString().equals("1257.456")); 534 } 535 536 /** 537 * java.math.BigDecimal#divide(java.math.BigDecimal, 538 * java.math.MathContext) divide(BigDecimal, RoundingMode) 539 */ 540 public void test_DivideBigDecimalRoundingModeUP() { 541 String a = "-37361671119238118911893939591735"; 542 String b = "74723342238476237823787879183470"; 543 RoundingMode rm = RoundingMode.UP; 544 String c = "-1"; 545 BigDecimal aNumber = new BigDecimal(new BigInteger(a)); 546 BigDecimal bNumber = new BigDecimal(new BigInteger(b)); 547 BigDecimal result = aNumber.divide(bNumber, rm); 548 assertEquals("incorrect value", c, result.toString()); 549 } 550 551 /** 552 * java.math.BigDecimal#divide(java.math.BigDecimal, 553 * java.math.RoundingMode) divide(BigDecimal, RoundingMode) 554 */ 555 public void test_DivideBigDecimalRoundingModeDOWN() { 556 String a = "-37361671119238118911893939591735"; 557 String b = "74723342238476237823787879183470"; 558 RoundingMode rm = RoundingMode.DOWN; 559 String c = "0"; 560 BigDecimal aNumber = new BigDecimal(new BigInteger(a)); 561 BigDecimal bNumber = new BigDecimal(new BigInteger(b)); 562 BigDecimal result = aNumber.divide(bNumber, rm); 563 assertEquals("incorrect value", c, result.toString()); 564 } 565 566 /** 567 * java.math.BigDecimal#divide(java.math.BigDecimal, 568 * java.math.RoundingMode) divide(BigDecimal, RoundingMode) 569 */ 570 public void test_DivideBigDecimalRoundingModeCEILING() { 571 String a = "3736186567876876578956958765675671119238118911893939591735"; 572 String b = "74723342238476237823787879183470"; 573 RoundingMode rm = RoundingMode.CEILING; 574 String c = "50000260373164286401361914"; 575 BigDecimal aNumber = new BigDecimal(new BigInteger(a)); 576 BigDecimal bNumber = new BigDecimal(new BigInteger(b)); 577 BigDecimal result = aNumber.divide(bNumber, rm); 578 assertEquals("incorrect value", c, result.toString()); 579 } 580 581 /** 582 * java.math.BigDecimal#divide(java.math.BigDecimal, 583 * java.math.RoundingMode) divide(BigDecimal, RoundingMode) 584 */ 585 public void test_DivideBigDecimalRoundingModeFLOOR() { 586 String a = "3736186567876876578956958765675671119238118911893939591735"; 587 String b = "74723342238476237823787879183470"; 588 RoundingMode rm = RoundingMode.FLOOR; 589 String c = "50000260373164286401361913"; 590 BigDecimal aNumber = new BigDecimal(new BigInteger(a)); 591 BigDecimal bNumber = new BigDecimal(new BigInteger(b)); 592 BigDecimal result = aNumber.divide(bNumber, rm); 593 assertEquals("incorrect value", c, result.toString()); 594 } 595 596 /** 597 * java.math.BigDecimal#divide(java.math.BigDecimal, 598 * java.math.RoundingMode) divide(BigDecimal, RoundingMode) 599 */ 600 public void test_DivideBigDecimalRoundingModeHALF_UP() { 601 String a = "3736186567876876578956958765675671119238118911893939591735"; 602 String b = "74723342238476237823787879183470"; 603 RoundingMode rm = RoundingMode.HALF_UP; 604 String c = "50000260373164286401361913"; 605 BigDecimal aNumber = new BigDecimal(new BigInteger(a)); 606 BigDecimal bNumber = new BigDecimal(new BigInteger(b)); 607 BigDecimal result = aNumber.divide(bNumber, rm); 608 assertEquals("incorrect value", c, result.toString()); 609 } 610 611 /** 612 * java.math.BigDecimal#divide(java.math.BigDecimal, 613 * java.math.RoundingMode) divide(BigDecimal, RoundingMode) 614 */ 615 public void test_DivideBigDecimalRoundingModeHALF_DOWN() { 616 String a = "3736186567876876578956958765675671119238118911893939591735"; 617 int aScale = 5; 618 String b = "74723342238476237823787879183470"; 619 int bScale = 15; 620 int newScale = 7; 621 RoundingMode rm = RoundingMode.HALF_DOWN; 622 String c = "500002603731642864013619132621009722.1803810"; 623 BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); 624 BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); 625 BigDecimal result = aNumber.divide(bNumber, newScale, rm); 626 assertEquals("incorrect value", c, result.toString()); 627 assertEquals("incorrect scale", newScale, result.scale()); 628 } 629 630 /** 631 * java.math.BigDecimal#divide(java.math.BigDecimal, 632 * java.math.RoundingMode) divide(BigDecimal, RoundingMode) 633 */ 634 public void test_DivideBigDecimalRoundingModeHALF_EVEN() { 635 String a = "3736186567876876578956958765675671119238118911893939591735"; 636 String b = "74723342238476237823787879183470"; 637 RoundingMode rm = RoundingMode.HALF_EVEN; 638 String c = "50000260373164286401361913"; 639 BigDecimal aNumber = new BigDecimal(new BigInteger(a)); 640 BigDecimal bNumber = new BigDecimal(new BigInteger(b)); 641 BigDecimal result = aNumber.divide(bNumber, rm); 642 assertEquals("incorrect value", c, result.toString()); 643 } 644 645 /** 646 * java.math.BigDecimal#divide(java.math.BigDecimal, 647 * java.math.RoundingMode) divide(BigDecimal, RoundingMode) 648 */ 649 public void test_DivideBigDecimalRoundingExc() { 650 String a = "3736186567876876578956958765675671119238118911893939591735"; 651 String b = "74723342238476237823787879183470"; 652 RoundingMode rm = RoundingMode.UNNECESSARY; 653 BigDecimal aNumber = new BigDecimal(new BigInteger(a)); 654 BigDecimal bNumber = new BigDecimal(new BigInteger(b)); 655 try { 656 aNumber.divide(bNumber, rm); 657 fail("ArithmeticException is not thrown for RoundingMode.UNNECESSARY divider"); 658 } catch (java.lang.ArithmeticException ae) { 659 // expected 660 } 661 try { 662 bNumber = new BigDecimal(0); 663 aNumber.divide(bNumber, rm); 664 fail("ArithmeticException is not thrown for zero divider"); 665 } catch (java.lang.ArithmeticException ae) { 666 // expected 667 } 668 } 669 } 670