src/java.base/share/classes/java/math/BigDecimal.java

Print this page
8152237 Support BigInteger.TWO
   1 /*
   2  * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 269 
 270     // All 18-digit base ten strings fit into a long; not all 19-digit
 271     // strings will
 272     private static final int MAX_COMPACT_DIGITS = 18;
 273 
 274     /* Appease the serialization gods */
 275     private static final long serialVersionUID = 6108874887143696463L;
 276 
 277     private static final ThreadLocal<StringBuilderHelper>
 278         threadLocalStringBuilderHelper = new ThreadLocal<StringBuilderHelper>() {
 279         @Override
 280         protected StringBuilderHelper initialValue() {
 281             return new StringBuilderHelper();
 282         }
 283     };
 284 
 285     // Cache of common small BigDecimal values.
 286     private static final BigDecimal ZERO_THROUGH_TEN[] = {
 287         new BigDecimal(BigInteger.ZERO,       0,  0, 1),
 288         new BigDecimal(BigInteger.ONE,        1,  0, 1),
 289         new BigDecimal(BigInteger.valueOf(2), 2,  0, 1),
 290         new BigDecimal(BigInteger.valueOf(3), 3,  0, 1),
 291         new BigDecimal(BigInteger.valueOf(4), 4,  0, 1),
 292         new BigDecimal(BigInteger.valueOf(5), 5,  0, 1),
 293         new BigDecimal(BigInteger.valueOf(6), 6,  0, 1),
 294         new BigDecimal(BigInteger.valueOf(7), 7,  0, 1),
 295         new BigDecimal(BigInteger.valueOf(8), 8,  0, 1),
 296         new BigDecimal(BigInteger.valueOf(9), 9,  0, 1),
 297         new BigDecimal(BigInteger.TEN,        10, 0, 2),
 298     };
 299 
 300     // Cache of zero scaled by 0 - 15
 301     private static final BigDecimal[] ZERO_SCALED_BY = {
 302         ZERO_THROUGH_TEN[0],
 303         new BigDecimal(BigInteger.ZERO, 0, 1, 1),
 304         new BigDecimal(BigInteger.ZERO, 0, 2, 1),
 305         new BigDecimal(BigInteger.ZERO, 0, 3, 1),
 306         new BigDecimal(BigInteger.ZERO, 0, 4, 1),
 307         new BigDecimal(BigInteger.ZERO, 0, 5, 1),
 308         new BigDecimal(BigInteger.ZERO, 0, 6, 1),
 309         new BigDecimal(BigInteger.ZERO, 0, 7, 1),


 919             this.intCompact = 0;
 920             this.precision = 1;
 921             return;
 922         }
 923         // Normalize
 924         while ((significand & 1) == 0) { // i.e., significand is even
 925             significand >>= 1;
 926             exponent++;
 927         }
 928         int scl = 0;
 929         // Calculate intVal and scale
 930         BigInteger rb;
 931         long compactVal = sign * significand;
 932         if (exponent == 0) {
 933             rb = (compactVal == INFLATED) ? INFLATED_BIGINT : null;
 934         } else {
 935             if (exponent < 0) {
 936                 rb = BigInteger.valueOf(5).pow(-exponent).multiply(compactVal);
 937                 scl = -exponent;
 938             } else { //  (exponent > 0)
 939                 rb = BigInteger.valueOf(2).pow(exponent).multiply(compactVal);
 940             }
 941             compactVal = compactValFor(rb);
 942         }
 943         int prec = 0;
 944         int mcp = mc.precision;
 945         if (mcp > 0) { // do rounding
 946             int mode = mc.roundingMode.oldMode;
 947             int drop;
 948             if (compactVal == INFLATED) {
 949                 prec = bigDigitLength(rb);
 950                 drop = prec - mcp;
 951                 while (drop > 0) {
 952                     scl = checkScaleNonZero((long) scl - drop);
 953                     rb = divideAndRoundByTenPow(rb, drop, mode);
 954                     compactVal = compactValFor(rb);
 955                     if (compactVal != INFLATED) {
 956                         break;
 957                     }
 958                     prec = bigDigitLength(rb);
 959                     drop = prec - mcp;


   1 /*
   2  * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 269 
 270     // All 18-digit base ten strings fit into a long; not all 19-digit
 271     // strings will
 272     private static final int MAX_COMPACT_DIGITS = 18;
 273 
 274     /* Appease the serialization gods */
 275     private static final long serialVersionUID = 6108874887143696463L;
 276 
 277     private static final ThreadLocal<StringBuilderHelper>
 278         threadLocalStringBuilderHelper = new ThreadLocal<StringBuilderHelper>() {
 279         @Override
 280         protected StringBuilderHelper initialValue() {
 281             return new StringBuilderHelper();
 282         }
 283     };
 284 
 285     // Cache of common small BigDecimal values.
 286     private static final BigDecimal ZERO_THROUGH_TEN[] = {
 287         new BigDecimal(BigInteger.ZERO,       0,  0, 1),
 288         new BigDecimal(BigInteger.ONE,        1,  0, 1),
 289         new BigDecimal(BigInteger.TWO,        2,  0, 1),
 290         new BigDecimal(BigInteger.valueOf(3), 3,  0, 1),
 291         new BigDecimal(BigInteger.valueOf(4), 4,  0, 1),
 292         new BigDecimal(BigInteger.valueOf(5), 5,  0, 1),
 293         new BigDecimal(BigInteger.valueOf(6), 6,  0, 1),
 294         new BigDecimal(BigInteger.valueOf(7), 7,  0, 1),
 295         new BigDecimal(BigInteger.valueOf(8), 8,  0, 1),
 296         new BigDecimal(BigInteger.valueOf(9), 9,  0, 1),
 297         new BigDecimal(BigInteger.TEN,        10, 0, 2),
 298     };
 299 
 300     // Cache of zero scaled by 0 - 15
 301     private static final BigDecimal[] ZERO_SCALED_BY = {
 302         ZERO_THROUGH_TEN[0],
 303         new BigDecimal(BigInteger.ZERO, 0, 1, 1),
 304         new BigDecimal(BigInteger.ZERO, 0, 2, 1),
 305         new BigDecimal(BigInteger.ZERO, 0, 3, 1),
 306         new BigDecimal(BigInteger.ZERO, 0, 4, 1),
 307         new BigDecimal(BigInteger.ZERO, 0, 5, 1),
 308         new BigDecimal(BigInteger.ZERO, 0, 6, 1),
 309         new BigDecimal(BigInteger.ZERO, 0, 7, 1),


 919             this.intCompact = 0;
 920             this.precision = 1;
 921             return;
 922         }
 923         // Normalize
 924         while ((significand & 1) == 0) { // i.e., significand is even
 925             significand >>= 1;
 926             exponent++;
 927         }
 928         int scl = 0;
 929         // Calculate intVal and scale
 930         BigInteger rb;
 931         long compactVal = sign * significand;
 932         if (exponent == 0) {
 933             rb = (compactVal == INFLATED) ? INFLATED_BIGINT : null;
 934         } else {
 935             if (exponent < 0) {
 936                 rb = BigInteger.valueOf(5).pow(-exponent).multiply(compactVal);
 937                 scl = -exponent;
 938             } else { //  (exponent > 0)
 939                 rb = BigInteger.TWO.pow(exponent).multiply(compactVal);
 940             }
 941             compactVal = compactValFor(rb);
 942         }
 943         int prec = 0;
 944         int mcp = mc.precision;
 945         if (mcp > 0) { // do rounding
 946             int mode = mc.roundingMode.oldMode;
 947             int drop;
 948             if (compactVal == INFLATED) {
 949                 prec = bigDigitLength(rb);
 950                 drop = prec - mcp;
 951                 while (drop > 0) {
 952                     scl = checkScaleNonZero((long) scl - drop);
 953                     rb = divideAndRoundByTenPow(rb, drop, mode);
 954                     compactVal = compactValFor(rb);
 955                     if (compactVal != INFLATED) {
 956                         break;
 957                     }
 958                     prec = bigDigitLength(rb);
 959                     drop = prec - mcp;