273 * compactly stored in this field and used in computations.
274 */
275 private final transient long intCompact;
276
277 // All 18-digit base ten strings fit into a long; not all 19-digit
278 // strings will
279 private static final int MAX_COMPACT_DIGITS = 18;
280
281 /* Appease the serialization gods */
282 private static final long serialVersionUID = 6108874887143696463L;
283
284 private static final ThreadLocal<StringBuilderHelper>
285 threadLocalStringBuilderHelper = new ThreadLocal<StringBuilderHelper>() {
286 @Override
287 protected StringBuilderHelper initialValue() {
288 return new StringBuilderHelper();
289 }
290 };
291
292 // Cache of common small BigDecimal values.
293 private static final BigDecimal ZERO_THROUGH_TEN[] = {
294 new BigDecimal(BigInteger.ZERO, 0, 0, 1),
295 new BigDecimal(BigInteger.ONE, 1, 0, 1),
296 new BigDecimal(BigInteger.TWO, 2, 0, 1),
297 new BigDecimal(BigInteger.valueOf(3), 3, 0, 1),
298 new BigDecimal(BigInteger.valueOf(4), 4, 0, 1),
299 new BigDecimal(BigInteger.valueOf(5), 5, 0, 1),
300 new BigDecimal(BigInteger.valueOf(6), 6, 0, 1),
301 new BigDecimal(BigInteger.valueOf(7), 7, 0, 1),
302 new BigDecimal(BigInteger.valueOf(8), 8, 0, 1),
303 new BigDecimal(BigInteger.valueOf(9), 9, 0, 1),
304 new BigDecimal(BigInteger.TEN, 10, 0, 2),
305 };
306
307 // Cache of zero scaled by 0 - 15
308 private static final BigDecimal[] ZERO_SCALED_BY = {
309 ZERO_THROUGH_TEN[0],
310 new BigDecimal(BigInteger.ZERO, 0, 1, 1),
311 new BigDecimal(BigInteger.ZERO, 0, 2, 1),
312 new BigDecimal(BigInteger.ZERO, 0, 3, 1),
313 new BigDecimal(BigInteger.ZERO, 0, 4, 1),
516 }
517 if (prec == 0) // no digits found
518 throw new NumberFormatException("No digits found.");
519 // Adjust scale if exp is not zero.
520 if (exp != 0) { // had significant exponent
521 scl = adjustScale(scl, exp);
522 }
523 rs = isneg ? -rs : rs;
524 int mcp = mc.precision;
525 int drop = prec - mcp; // prec has range [1, MAX_INT], mcp has range [0, MAX_INT];
526 // therefore, this subtract cannot overflow
527 if (mcp > 0 && drop > 0) { // do rounding
528 while (drop > 0) {
529 scl = checkScaleNonZero((long) scl - drop);
530 rs = divideAndRound(rs, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode);
531 prec = longDigitLength(rs);
532 drop = prec - mcp;
533 }
534 }
535 } else {
536 char coeff[] = new char[len];
537 for (; len > 0; offset++, len--) {
538 c = in[offset];
539 // have digit
540 if ((c >= '0' && c <= '9') || Character.isDigit(c)) {
541 // First compact case, we need not to preserve the character
542 // and we can just compute the value in place.
543 if (c == '0' || Character.digit(c, 10) == 0) {
544 if (prec == 0) {
545 coeff[idx] = c;
546 prec = 1;
547 } else if (idx != 0) {
548 coeff[idx++] = c;
549 ++prec;
550 } // else c must be a redundant leading zero
551 } else {
552 if (prec != 1 || idx != 0)
553 ++prec; // prec unchanged if preceded by 0s
554 coeff[idx++] = c;
555 }
556 if (dot)
1357 result = lhsIsZero ? doRound(augend, mc) : doRound(lhs, mc);
1358
1359 if (result.scale() == preferredScale)
1360 return result;
1361 else if (result.scale() > preferredScale) {
1362 return stripZerosToMatchScale(result.intVal, result.intCompact, result.scale, preferredScale);
1363 } else { // result.scale < preferredScale
1364 int precisionDiff = mc.precision - result.precision();
1365 int scaleDiff = preferredScale - result.scale();
1366
1367 if (precisionDiff >= scaleDiff)
1368 return result.setScale(preferredScale); // can achieve target scale
1369 else
1370 return result.setScale(result.scale() + precisionDiff);
1371 }
1372 }
1373 }
1374
1375 long padding = (long) lhs.scale - augend.scale;
1376 if (padding != 0) { // scales differ; alignment needed
1377 BigDecimal arg[] = preAlign(lhs, augend, padding, mc);
1378 matchScale(arg);
1379 lhs = arg[0];
1380 augend = arg[1];
1381 }
1382 return doRound(lhs.inflated().add(augend.inflated()), lhs.scale, mc);
1383 }
1384
1385 /**
1386 * Returns an array of length two, the sum of whose entries is
1387 * equal to the rounded sum of the {@code BigDecimal} arguments.
1388 *
1389 * <p>If the digit positions of the arguments have a sufficient
1390 * gap between them, the value smaller in magnitude can be
1391 * condensed into a {@literal "sticky bit"} and the end result will
1392 * round the same way <em>if</em> the precision of the final
1393 * result does not include the high order digit of the small
1394 * magnitude operand.
1395 *
1396 * <p>Note that while strictly speaking this is an optimization,
1397 * it makes a much wider range of additions practical.
1896 Math.min(precisionDiff, preferredScale - result.scale) );
1897 } else {
1898 return stripZerosToMatchScale(result.intVal,result.intCompact,result.scale,preferredScale);
1899 }
1900 }
1901
1902 /**
1903 * Returns a {@code BigDecimal} whose value is {@code (this % divisor)}.
1904 *
1905 * <p>The remainder is given by
1906 * {@code this.subtract(this.divideToIntegralValue(divisor).multiply(divisor))}.
1907 * Note that this is <em>not</em> the modulo operation (the result can be
1908 * negative).
1909 *
1910 * @param divisor value by which this {@code BigDecimal} is to be divided.
1911 * @return {@code this % divisor}.
1912 * @throws ArithmeticException if {@code divisor==0}
1913 * @since 1.5
1914 */
1915 public BigDecimal remainder(BigDecimal divisor) {
1916 BigDecimal divrem[] = this.divideAndRemainder(divisor);
1917 return divrem[1];
1918 }
1919
1920
1921 /**
1922 * Returns a {@code BigDecimal} whose value is {@code (this %
1923 * divisor)}, with rounding according to the context settings.
1924 * The {@code MathContext} settings affect the implicit divide
1925 * used to compute the remainder. The remainder computation
1926 * itself is by definition exact. Therefore, the remainder may
1927 * contain more than {@code mc.getPrecision()} digits.
1928 *
1929 * <p>The remainder is given by
1930 * {@code this.subtract(this.divideToIntegralValue(divisor,
1931 * mc).multiply(divisor))}. Note that this is not the modulo
1932 * operation (the result can be negative).
1933 *
1934 * @param divisor value by which this {@code BigDecimal} is to be divided.
1935 * @param mc the context to use.
1936 * @return {@code this % divisor}, rounded as necessary.
1937 * @throws ArithmeticException if {@code divisor==0}
1938 * @throws ArithmeticException if the result is inexact but the
1939 * rounding mode is {@code UNNECESSARY}, or {@code mc.precision}
1940 * {@literal >} 0 and the result of {@code this.divideToIntgralValue(divisor)} would
1941 * require a precision of more than {@code mc.precision} digits.
1942 * @see #divideToIntegralValue(java.math.BigDecimal, java.math.MathContext)
1943 * @since 1.5
1944 */
1945 public BigDecimal remainder(BigDecimal divisor, MathContext mc) {
1946 BigDecimal divrem[] = this.divideAndRemainder(divisor, mc);
1947 return divrem[1];
1948 }
1949
1950 /**
1951 * Returns a two-element {@code BigDecimal} array containing the
1952 * result of {@code divideToIntegralValue} followed by the result of
1953 * {@code remainder} on the two operands.
1954 *
1955 * <p>Note that if both the integer quotient and remainder are
1956 * needed, this method is faster than using the
1957 * {@code divideToIntegralValue} and {@code remainder} methods
1958 * separately because the division need only be carried out once.
1959 *
1960 * @param divisor value by which this {@code BigDecimal} is to be divided,
1961 * and the remainder computed.
1962 * @return a two element {@code BigDecimal} array: the quotient
1963 * (the result of {@code divideToIntegralValue}) is the initial element
1964 * and the remainder is the final element.
1965 * @throws ArithmeticException if {@code divisor==0}
1966 * @see #divideToIntegralValue(java.math.BigDecimal, java.math.MathContext)
3616 if (Math.abs(intCompact) < 1L<<52 ) {
3617 // Don't have too guard against
3618 // Math.abs(MIN_VALUE) because of outer check
3619 // against INFLATED.
3620 if (scale > 0 && scale < DOUBLE_10_POW.length) {
3621 return (double)intCompact / DOUBLE_10_POW[scale];
3622 } else if (scale < 0 && scale > -DOUBLE_10_POW.length) {
3623 return (double)intCompact * DOUBLE_10_POW[-scale];
3624 }
3625 }
3626 }
3627 }
3628 // Somewhat inefficient, but guaranteed to work.
3629 return Double.parseDouble(this.toString());
3630 }
3631
3632 /**
3633 * Powers of 10 which can be represented exactly in {@code
3634 * double}.
3635 */
3636 private static final double DOUBLE_10_POW[] = {
3637 1.0e0, 1.0e1, 1.0e2, 1.0e3, 1.0e4, 1.0e5,
3638 1.0e6, 1.0e7, 1.0e8, 1.0e9, 1.0e10, 1.0e11,
3639 1.0e12, 1.0e13, 1.0e14, 1.0e15, 1.0e16, 1.0e17,
3640 1.0e18, 1.0e19, 1.0e20, 1.0e21, 1.0e22
3641 };
3642
3643 /**
3644 * Powers of 10 which can be represented exactly in {@code
3645 * float}.
3646 */
3647 private static final float FLOAT_10_POW[] = {
3648 1.0e0f, 1.0e1f, 1.0e2f, 1.0e3f, 1.0e4f, 1.0e5f,
3649 1.0e6f, 1.0e7f, 1.0e8f, 1.0e9f, 1.0e10f
3650 };
3651
3652 /**
3653 * Returns the size of an ulp, a unit in the last place, of this
3654 * {@code BigDecimal}. An ulp of a nonzero {@code BigDecimal}
3655 * value is the positive distance between this value and the
3656 * {@code BigDecimal} value next larger in magnitude with the
3657 * same number of digits. An ulp of a zero value is numerically
3658 * equal to 1 with the scale of {@code this}. The result is
3659 * stored with the same scale as {@code this} so the result
3660 * for zero and nonzero values is equal to {@code [1,
3661 * this.scale()]}.
3662 *
3663 * @return the size of an ulp of {@code this}
3664 * @since 1.5
3665 */
3666 public BigDecimal ulp() {
3667 return BigDecimal.valueOf(1, this.scale(), 1);
3935 10, // 1 / 10^1
3936 100, // 2 / 10^2
3937 1000, // 3 / 10^3
3938 10000, // 4 / 10^4
3939 100000, // 5 / 10^5
3940 1000000, // 6 / 10^6
3941 10000000, // 7 / 10^7
3942 100000000, // 8 / 10^8
3943 1000000000, // 9 / 10^9
3944 10000000000L, // 10 / 10^10
3945 100000000000L, // 11 / 10^11
3946 1000000000000L, // 12 / 10^12
3947 10000000000000L, // 13 / 10^13
3948 100000000000000L, // 14 / 10^14
3949 1000000000000000L, // 15 / 10^15
3950 10000000000000000L, // 16 / 10^16
3951 100000000000000000L, // 17 / 10^17
3952 1000000000000000000L // 18 / 10^18
3953 };
3954
3955 private static volatile BigInteger BIG_TEN_POWERS_TABLE[] = {
3956 BigInteger.ONE,
3957 BigInteger.valueOf(10),
3958 BigInteger.valueOf(100),
3959 BigInteger.valueOf(1000),
3960 BigInteger.valueOf(10000),
3961 BigInteger.valueOf(100000),
3962 BigInteger.valueOf(1000000),
3963 BigInteger.valueOf(10000000),
3964 BigInteger.valueOf(100000000),
3965 BigInteger.valueOf(1000000000),
3966 BigInteger.valueOf(10000000000L),
3967 BigInteger.valueOf(100000000000L),
3968 BigInteger.valueOf(1000000000000L),
3969 BigInteger.valueOf(10000000000000L),
3970 BigInteger.valueOf(100000000000000L),
3971 BigInteger.valueOf(1000000000000000L),
3972 BigInteger.valueOf(10000000000000000L),
3973 BigInteger.valueOf(100000000000000000L),
3974 BigInteger.valueOf(1000000000000000000L)
3975 };
3976
3977 private static final int BIG_TEN_POWERS_TABLE_INITLEN =
3978 BIG_TEN_POWERS_TABLE.length;
3979 private static final int BIG_TEN_POWERS_TABLE_MAX =
3980 16 * BIG_TEN_POWERS_TABLE_INITLEN;
3981
3982 private static final long THRESHOLDS_TABLE[] = {
3983 Long.MAX_VALUE, // 0
3984 Long.MAX_VALUE/10L, // 1
3985 Long.MAX_VALUE/100L, // 2
3986 Long.MAX_VALUE/1000L, // 3
3987 Long.MAX_VALUE/10000L, // 4
3988 Long.MAX_VALUE/100000L, // 5
3989 Long.MAX_VALUE/1000000L, // 6
3990 Long.MAX_VALUE/10000000L, // 7
3991 Long.MAX_VALUE/100000000L, // 8
3992 Long.MAX_VALUE/1000000000L, // 9
3993 Long.MAX_VALUE/10000000000L, // 10
3994 Long.MAX_VALUE/100000000000L, // 11
3995 Long.MAX_VALUE/1000000000000L, // 12
3996 Long.MAX_VALUE/10000000000000L, // 13
3997 Long.MAX_VALUE/100000000000000L, // 14
3998 Long.MAX_VALUE/1000000000000000L, // 15
3999 Long.MAX_VALUE/10000000000000000L, // 16
4000 Long.MAX_VALUE/100000000000000000L, // 17
4001 Long.MAX_VALUE/1000000000000000000L // 18
4002 };
4695 /**
4696 * Tests if quotient has to be incremented according the roundingMode
4697 */
4698 private static boolean needIncrement(MutableBigInteger mdivisor, int roundingMode,
4699 int qsign, MutableBigInteger mq, MutableBigInteger mr) {
4700 assert !mr.isZero();
4701 int cmpFracHalf = mr.compareHalf(mdivisor);
4702 return commonNeedIncrement(roundingMode, qsign, cmpFracHalf, mq.isOdd());
4703 }
4704
4705 /**
4706 * Remove insignificant trailing zeros from this
4707 * {@code BigInteger} value until the preferred scale is reached or no
4708 * more zeros can be removed. If the preferred scale is less than
4709 * Integer.MIN_VALUE, all the trailing zeros will be removed.
4710 *
4711 * @return new {@code BigDecimal} with a scale possibly reduced
4712 * to be closed to the preferred scale.
4713 */
4714 private static BigDecimal createAndStripZerosToMatchScale(BigInteger intVal, int scale, long preferredScale) {
4715 BigInteger qr[]; // quotient-remainder pair
4716 while (intVal.compareMagnitude(BigInteger.TEN) >= 0
4717 && scale > preferredScale) {
4718 if (intVal.testBit(0))
4719 break; // odd number cannot end in 0
4720 qr = intVal.divideAndRemainder(BigInteger.TEN);
4721 if (qr[1].signum() != 0)
4722 break; // non-0 remainder
4723 intVal = qr[0];
4724 scale = checkScale(intVal,(long) scale - 1); // could Overflow
4725 }
4726 return valueOf(intVal, scale, 0);
4727 }
4728
4729 /**
4730 * Remove insignificant trailing zeros from this
4731 * {@code long} value until the preferred scale is reached or no
4732 * more zeros can be removed. If the preferred scale is less than
4733 * Integer.MIN_VALUE, all the trailing zeros will be removed.
4734 *
4735 * @return new {@code BigDecimal} with a scale possibly reduced
|
273 * compactly stored in this field and used in computations.
274 */
275 private final transient long intCompact;
276
277 // All 18-digit base ten strings fit into a long; not all 19-digit
278 // strings will
279 private static final int MAX_COMPACT_DIGITS = 18;
280
281 /* Appease the serialization gods */
282 private static final long serialVersionUID = 6108874887143696463L;
283
284 private static final ThreadLocal<StringBuilderHelper>
285 threadLocalStringBuilderHelper = new ThreadLocal<StringBuilderHelper>() {
286 @Override
287 protected StringBuilderHelper initialValue() {
288 return new StringBuilderHelper();
289 }
290 };
291
292 // Cache of common small BigDecimal values.
293 private static final BigDecimal[] ZERO_THROUGH_TEN = {
294 new BigDecimal(BigInteger.ZERO, 0, 0, 1),
295 new BigDecimal(BigInteger.ONE, 1, 0, 1),
296 new BigDecimal(BigInteger.TWO, 2, 0, 1),
297 new BigDecimal(BigInteger.valueOf(3), 3, 0, 1),
298 new BigDecimal(BigInteger.valueOf(4), 4, 0, 1),
299 new BigDecimal(BigInteger.valueOf(5), 5, 0, 1),
300 new BigDecimal(BigInteger.valueOf(6), 6, 0, 1),
301 new BigDecimal(BigInteger.valueOf(7), 7, 0, 1),
302 new BigDecimal(BigInteger.valueOf(8), 8, 0, 1),
303 new BigDecimal(BigInteger.valueOf(9), 9, 0, 1),
304 new BigDecimal(BigInteger.TEN, 10, 0, 2),
305 };
306
307 // Cache of zero scaled by 0 - 15
308 private static final BigDecimal[] ZERO_SCALED_BY = {
309 ZERO_THROUGH_TEN[0],
310 new BigDecimal(BigInteger.ZERO, 0, 1, 1),
311 new BigDecimal(BigInteger.ZERO, 0, 2, 1),
312 new BigDecimal(BigInteger.ZERO, 0, 3, 1),
313 new BigDecimal(BigInteger.ZERO, 0, 4, 1),
516 }
517 if (prec == 0) // no digits found
518 throw new NumberFormatException("No digits found.");
519 // Adjust scale if exp is not zero.
520 if (exp != 0) { // had significant exponent
521 scl = adjustScale(scl, exp);
522 }
523 rs = isneg ? -rs : rs;
524 int mcp = mc.precision;
525 int drop = prec - mcp; // prec has range [1, MAX_INT], mcp has range [0, MAX_INT];
526 // therefore, this subtract cannot overflow
527 if (mcp > 0 && drop > 0) { // do rounding
528 while (drop > 0) {
529 scl = checkScaleNonZero((long) scl - drop);
530 rs = divideAndRound(rs, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode);
531 prec = longDigitLength(rs);
532 drop = prec - mcp;
533 }
534 }
535 } else {
536 char[] coeff = new char[len];
537 for (; len > 0; offset++, len--) {
538 c = in[offset];
539 // have digit
540 if ((c >= '0' && c <= '9') || Character.isDigit(c)) {
541 // First compact case, we need not to preserve the character
542 // and we can just compute the value in place.
543 if (c == '0' || Character.digit(c, 10) == 0) {
544 if (prec == 0) {
545 coeff[idx] = c;
546 prec = 1;
547 } else if (idx != 0) {
548 coeff[idx++] = c;
549 ++prec;
550 } // else c must be a redundant leading zero
551 } else {
552 if (prec != 1 || idx != 0)
553 ++prec; // prec unchanged if preceded by 0s
554 coeff[idx++] = c;
555 }
556 if (dot)
1357 result = lhsIsZero ? doRound(augend, mc) : doRound(lhs, mc);
1358
1359 if (result.scale() == preferredScale)
1360 return result;
1361 else if (result.scale() > preferredScale) {
1362 return stripZerosToMatchScale(result.intVal, result.intCompact, result.scale, preferredScale);
1363 } else { // result.scale < preferredScale
1364 int precisionDiff = mc.precision - result.precision();
1365 int scaleDiff = preferredScale - result.scale();
1366
1367 if (precisionDiff >= scaleDiff)
1368 return result.setScale(preferredScale); // can achieve target scale
1369 else
1370 return result.setScale(result.scale() + precisionDiff);
1371 }
1372 }
1373 }
1374
1375 long padding = (long) lhs.scale - augend.scale;
1376 if (padding != 0) { // scales differ; alignment needed
1377 BigDecimal[] arg = preAlign(lhs, augend, padding, mc);
1378 matchScale(arg);
1379 lhs = arg[0];
1380 augend = arg[1];
1381 }
1382 return doRound(lhs.inflated().add(augend.inflated()), lhs.scale, mc);
1383 }
1384
1385 /**
1386 * Returns an array of length two, the sum of whose entries is
1387 * equal to the rounded sum of the {@code BigDecimal} arguments.
1388 *
1389 * <p>If the digit positions of the arguments have a sufficient
1390 * gap between them, the value smaller in magnitude can be
1391 * condensed into a {@literal "sticky bit"} and the end result will
1392 * round the same way <em>if</em> the precision of the final
1393 * result does not include the high order digit of the small
1394 * magnitude operand.
1395 *
1396 * <p>Note that while strictly speaking this is an optimization,
1397 * it makes a much wider range of additions practical.
1896 Math.min(precisionDiff, preferredScale - result.scale) );
1897 } else {
1898 return stripZerosToMatchScale(result.intVal,result.intCompact,result.scale,preferredScale);
1899 }
1900 }
1901
1902 /**
1903 * Returns a {@code BigDecimal} whose value is {@code (this % divisor)}.
1904 *
1905 * <p>The remainder is given by
1906 * {@code this.subtract(this.divideToIntegralValue(divisor).multiply(divisor))}.
1907 * Note that this is <em>not</em> the modulo operation (the result can be
1908 * negative).
1909 *
1910 * @param divisor value by which this {@code BigDecimal} is to be divided.
1911 * @return {@code this % divisor}.
1912 * @throws ArithmeticException if {@code divisor==0}
1913 * @since 1.5
1914 */
1915 public BigDecimal remainder(BigDecimal divisor) {
1916 BigDecimal[] divrem = this.divideAndRemainder(divisor);
1917 return divrem[1];
1918 }
1919
1920
1921 /**
1922 * Returns a {@code BigDecimal} whose value is {@code (this %
1923 * divisor)}, with rounding according to the context settings.
1924 * The {@code MathContext} settings affect the implicit divide
1925 * used to compute the remainder. The remainder computation
1926 * itself is by definition exact. Therefore, the remainder may
1927 * contain more than {@code mc.getPrecision()} digits.
1928 *
1929 * <p>The remainder is given by
1930 * {@code this.subtract(this.divideToIntegralValue(divisor,
1931 * mc).multiply(divisor))}. Note that this is not the modulo
1932 * operation (the result can be negative).
1933 *
1934 * @param divisor value by which this {@code BigDecimal} is to be divided.
1935 * @param mc the context to use.
1936 * @return {@code this % divisor}, rounded as necessary.
1937 * @throws ArithmeticException if {@code divisor==0}
1938 * @throws ArithmeticException if the result is inexact but the
1939 * rounding mode is {@code UNNECESSARY}, or {@code mc.precision}
1940 * {@literal >} 0 and the result of {@code this.divideToIntgralValue(divisor)} would
1941 * require a precision of more than {@code mc.precision} digits.
1942 * @see #divideToIntegralValue(java.math.BigDecimal, java.math.MathContext)
1943 * @since 1.5
1944 */
1945 public BigDecimal remainder(BigDecimal divisor, MathContext mc) {
1946 BigDecimal[] divrem = this.divideAndRemainder(divisor, mc);
1947 return divrem[1];
1948 }
1949
1950 /**
1951 * Returns a two-element {@code BigDecimal} array containing the
1952 * result of {@code divideToIntegralValue} followed by the result of
1953 * {@code remainder} on the two operands.
1954 *
1955 * <p>Note that if both the integer quotient and remainder are
1956 * needed, this method is faster than using the
1957 * {@code divideToIntegralValue} and {@code remainder} methods
1958 * separately because the division need only be carried out once.
1959 *
1960 * @param divisor value by which this {@code BigDecimal} is to be divided,
1961 * and the remainder computed.
1962 * @return a two element {@code BigDecimal} array: the quotient
1963 * (the result of {@code divideToIntegralValue}) is the initial element
1964 * and the remainder is the final element.
1965 * @throws ArithmeticException if {@code divisor==0}
1966 * @see #divideToIntegralValue(java.math.BigDecimal, java.math.MathContext)
3616 if (Math.abs(intCompact) < 1L<<52 ) {
3617 // Don't have too guard against
3618 // Math.abs(MIN_VALUE) because of outer check
3619 // against INFLATED.
3620 if (scale > 0 && scale < DOUBLE_10_POW.length) {
3621 return (double)intCompact / DOUBLE_10_POW[scale];
3622 } else if (scale < 0 && scale > -DOUBLE_10_POW.length) {
3623 return (double)intCompact * DOUBLE_10_POW[-scale];
3624 }
3625 }
3626 }
3627 }
3628 // Somewhat inefficient, but guaranteed to work.
3629 return Double.parseDouble(this.toString());
3630 }
3631
3632 /**
3633 * Powers of 10 which can be represented exactly in {@code
3634 * double}.
3635 */
3636 private static final double[] DOUBLE_10_POW = {
3637 1.0e0, 1.0e1, 1.0e2, 1.0e3, 1.0e4, 1.0e5,
3638 1.0e6, 1.0e7, 1.0e8, 1.0e9, 1.0e10, 1.0e11,
3639 1.0e12, 1.0e13, 1.0e14, 1.0e15, 1.0e16, 1.0e17,
3640 1.0e18, 1.0e19, 1.0e20, 1.0e21, 1.0e22
3641 };
3642
3643 /**
3644 * Powers of 10 which can be represented exactly in {@code
3645 * float}.
3646 */
3647 private static final float[] FLOAT_10_POW = {
3648 1.0e0f, 1.0e1f, 1.0e2f, 1.0e3f, 1.0e4f, 1.0e5f,
3649 1.0e6f, 1.0e7f, 1.0e8f, 1.0e9f, 1.0e10f
3650 };
3651
3652 /**
3653 * Returns the size of an ulp, a unit in the last place, of this
3654 * {@code BigDecimal}. An ulp of a nonzero {@code BigDecimal}
3655 * value is the positive distance between this value and the
3656 * {@code BigDecimal} value next larger in magnitude with the
3657 * same number of digits. An ulp of a zero value is numerically
3658 * equal to 1 with the scale of {@code this}. The result is
3659 * stored with the same scale as {@code this} so the result
3660 * for zero and nonzero values is equal to {@code [1,
3661 * this.scale()]}.
3662 *
3663 * @return the size of an ulp of {@code this}
3664 * @since 1.5
3665 */
3666 public BigDecimal ulp() {
3667 return BigDecimal.valueOf(1, this.scale(), 1);
3935 10, // 1 / 10^1
3936 100, // 2 / 10^2
3937 1000, // 3 / 10^3
3938 10000, // 4 / 10^4
3939 100000, // 5 / 10^5
3940 1000000, // 6 / 10^6
3941 10000000, // 7 / 10^7
3942 100000000, // 8 / 10^8
3943 1000000000, // 9 / 10^9
3944 10000000000L, // 10 / 10^10
3945 100000000000L, // 11 / 10^11
3946 1000000000000L, // 12 / 10^12
3947 10000000000000L, // 13 / 10^13
3948 100000000000000L, // 14 / 10^14
3949 1000000000000000L, // 15 / 10^15
3950 10000000000000000L, // 16 / 10^16
3951 100000000000000000L, // 17 / 10^17
3952 1000000000000000000L // 18 / 10^18
3953 };
3954
3955 private static volatile BigInteger[] BIG_TEN_POWERS_TABLE = {
3956 BigInteger.ONE,
3957 BigInteger.valueOf(10),
3958 BigInteger.valueOf(100),
3959 BigInteger.valueOf(1000),
3960 BigInteger.valueOf(10000),
3961 BigInteger.valueOf(100000),
3962 BigInteger.valueOf(1000000),
3963 BigInteger.valueOf(10000000),
3964 BigInteger.valueOf(100000000),
3965 BigInteger.valueOf(1000000000),
3966 BigInteger.valueOf(10000000000L),
3967 BigInteger.valueOf(100000000000L),
3968 BigInteger.valueOf(1000000000000L),
3969 BigInteger.valueOf(10000000000000L),
3970 BigInteger.valueOf(100000000000000L),
3971 BigInteger.valueOf(1000000000000000L),
3972 BigInteger.valueOf(10000000000000000L),
3973 BigInteger.valueOf(100000000000000000L),
3974 BigInteger.valueOf(1000000000000000000L)
3975 };
3976
3977 private static final int BIG_TEN_POWERS_TABLE_INITLEN =
3978 BIG_TEN_POWERS_TABLE.length;
3979 private static final int BIG_TEN_POWERS_TABLE_MAX =
3980 16 * BIG_TEN_POWERS_TABLE_INITLEN;
3981
3982 private static final long[] THRESHOLDS_TABLE = {
3983 Long.MAX_VALUE, // 0
3984 Long.MAX_VALUE/10L, // 1
3985 Long.MAX_VALUE/100L, // 2
3986 Long.MAX_VALUE/1000L, // 3
3987 Long.MAX_VALUE/10000L, // 4
3988 Long.MAX_VALUE/100000L, // 5
3989 Long.MAX_VALUE/1000000L, // 6
3990 Long.MAX_VALUE/10000000L, // 7
3991 Long.MAX_VALUE/100000000L, // 8
3992 Long.MAX_VALUE/1000000000L, // 9
3993 Long.MAX_VALUE/10000000000L, // 10
3994 Long.MAX_VALUE/100000000000L, // 11
3995 Long.MAX_VALUE/1000000000000L, // 12
3996 Long.MAX_VALUE/10000000000000L, // 13
3997 Long.MAX_VALUE/100000000000000L, // 14
3998 Long.MAX_VALUE/1000000000000000L, // 15
3999 Long.MAX_VALUE/10000000000000000L, // 16
4000 Long.MAX_VALUE/100000000000000000L, // 17
4001 Long.MAX_VALUE/1000000000000000000L // 18
4002 };
4695 /**
4696 * Tests if quotient has to be incremented according the roundingMode
4697 */
4698 private static boolean needIncrement(MutableBigInteger mdivisor, int roundingMode,
4699 int qsign, MutableBigInteger mq, MutableBigInteger mr) {
4700 assert !mr.isZero();
4701 int cmpFracHalf = mr.compareHalf(mdivisor);
4702 return commonNeedIncrement(roundingMode, qsign, cmpFracHalf, mq.isOdd());
4703 }
4704
4705 /**
4706 * Remove insignificant trailing zeros from this
4707 * {@code BigInteger} value until the preferred scale is reached or no
4708 * more zeros can be removed. If the preferred scale is less than
4709 * Integer.MIN_VALUE, all the trailing zeros will be removed.
4710 *
4711 * @return new {@code BigDecimal} with a scale possibly reduced
4712 * to be closed to the preferred scale.
4713 */
4714 private static BigDecimal createAndStripZerosToMatchScale(BigInteger intVal, int scale, long preferredScale) {
4715 BigInteger[] qr; // quotient-remainder pair
4716 while (intVal.compareMagnitude(BigInteger.TEN) >= 0
4717 && scale > preferredScale) {
4718 if (intVal.testBit(0))
4719 break; // odd number cannot end in 0
4720 qr = intVal.divideAndRemainder(BigInteger.TEN);
4721 if (qr[1].signum() != 0)
4722 break; // non-0 remainder
4723 intVal = qr[0];
4724 scale = checkScale(intVal,(long) scale - 1); // could Overflow
4725 }
4726 return valueOf(intVal, scale, 0);
4727 }
4728
4729 /**
4730 * Remove insignificant trailing zeros from this
4731 * {@code long} value until the preferred scale is reached or no
4732 * more zeros can be removed. If the preferred scale is less than
4733 * Integer.MIN_VALUE, all the trailing zeros will be removed.
4734 *
4735 * @return new {@code BigDecimal} with a scale possibly reduced
|