src/share/classes/java/math/BigInteger.java

Print this page
rev 7924 : 6378503: In java.math.BigDecimal, division by one returns zero
6446965: Using BigDecimal.divideToIntegralValue with extreme scales can lead to an incorrect result
Summary: Fix overflow of ints and ensure appropriate values passed to checkScaleNonZero()
Reviewed-by: darcy, martin
Contributed-by: Brian Burkhalter <brian.burkhalter@oracle.com>


2092                 if (signum < 0 && (exponent&1) == 1) {
2093                     return NEGATIVE_ONE.shiftLeft(powersOfTwo*exponent);
2094                 } else {
2095                     return ONE.shiftLeft(powersOfTwo*exponent);
2096                 }
2097             }
2098         } else {
2099             remainingBits = partToSquare.bitLength();
2100             if (remainingBits == 1) { // Nothing left but +/- 1?
2101                 if (signum < 0  && (exponent&1) == 1) {
2102                     return NEGATIVE_ONE;
2103                 } else {
2104                     return ONE;
2105                 }
2106             }
2107         }
2108 
2109         // This is a quick way to approximate the size of the result,
2110         // similar to doing log2[n] * exponent.  This will give an upper bound
2111         // of how big the result can be, and which algorithm to use.
2112         int scaleFactor = remainingBits * exponent;
2113 
2114         // Use slightly different algorithms for small and large operands.
2115         // See if the result will safely fit into a long. (Largest 2^63-1)
2116         if (partToSquare.mag.length == 1 && scaleFactor <= 62) {
2117             // Small number algorithm.  Everything fits into a long.
2118             int newSign = (signum <0  && (exponent&1) == 1 ? -1 : 1);
2119             long result = 1;
2120             long baseToPow2 = partToSquare.mag[0] & LONG_MASK;
2121 
2122             int workingExponent = exponent;
2123 
2124             // Perform exponentiation using repeated squaring trick
2125             while (workingExponent != 0) {
2126                 if ((workingExponent & 1) == 1) {
2127                     result = result * baseToPow2;
2128                 }
2129 
2130                 if ((workingExponent >>>= 1) != 0) {
2131                     baseToPow2 = baseToPow2 * baseToPow2;
2132                 }




2092                 if (signum < 0 && (exponent&1) == 1) {
2093                     return NEGATIVE_ONE.shiftLeft(powersOfTwo*exponent);
2094                 } else {
2095                     return ONE.shiftLeft(powersOfTwo*exponent);
2096                 }
2097             }
2098         } else {
2099             remainingBits = partToSquare.bitLength();
2100             if (remainingBits == 1) { // Nothing left but +/- 1?
2101                 if (signum < 0  && (exponent&1) == 1) {
2102                     return NEGATIVE_ONE;
2103                 } else {
2104                     return ONE;
2105                 }
2106             }
2107         }
2108 
2109         // This is a quick way to approximate the size of the result,
2110         // similar to doing log2[n] * exponent.  This will give an upper bound
2111         // of how big the result can be, and which algorithm to use.
2112         long scaleFactor = (long)remainingBits * exponent;
2113 
2114         // Use slightly different algorithms for small and large operands.
2115         // See if the result will safely fit into a long. (Largest 2^63-1)
2116         if (partToSquare.mag.length == 1 && scaleFactor <= 62) {
2117             // Small number algorithm.  Everything fits into a long.
2118             int newSign = (signum <0  && (exponent&1) == 1 ? -1 : 1);
2119             long result = 1;
2120             long baseToPow2 = partToSquare.mag[0] & LONG_MASK;
2121 
2122             int workingExponent = exponent;
2123 
2124             // Perform exponentiation using repeated squaring trick
2125             while (workingExponent != 0) {
2126                 if ((workingExponent & 1) == 1) {
2127                     result = result * baseToPow2;
2128                 }
2129 
2130                 if ((workingExponent >>>= 1) != 0) {
2131                     baseToPow2 = baseToPow2 * baseToPow2;
2132                 }