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

Print this page

        

*** 902,917 **** public BigDecimal(double val, MathContext mc) { if (Double.isInfinite(val) || Double.isNaN(val)) throw new NumberFormatException("Infinite or NaN"); // Translate the double into sign, exponent and significand, according // to the formulae in JLS, Section 20.10.22. - int sign = (val >= 0.0 ? 1 : -1); // Preserving sign of zero doesn't matter - int exponent = Math.getExponent(val); long valBits = Double.doubleToLongBits(val); ! long significand = (exponent == (Double.MIN_EXPONENT-1) ? (valBits & ((1L << 52) - 1)) << 1 : (valBits & ((1L << 52) - 1)) | (1L << 52)); // At this point, val == sign * significand * 2**exponent. /* * Special case zero to supress nonterminating normalization and bogus * scale calculation. --- 902,918 ---- public BigDecimal(double val, MathContext mc) { if (Double.isInfinite(val) || Double.isNaN(val)) throw new NumberFormatException("Infinite or NaN"); // Translate the double into sign, exponent and significand, according // to the formulae in JLS, Section 20.10.22. long valBits = Double.doubleToLongBits(val); ! int sign = ((valBits >> 63) == 0 ? 1 : -1); ! int exponent = (int) ((valBits >> 52) & 0x7ffL); ! long significand = (exponent == 0 ? (valBits & ((1L << 52) - 1)) << 1 : (valBits & ((1L << 52) - 1)) | (1L << 52)); + exponent -= 1075; // At this point, val == sign * significand * 2**exponent. /* * Special case zero to supress nonterminating normalization and bogus * scale calculation.
*** 931,943 **** int scale = 0; // Calculate intVal and scale BigInteger intVal; long compactVal = sign * significand; if (exponent == 0) { ! // If the exponent is zero, the significant fits in a long ! assert compactVal != INFLATED; ! intVal = null; } else { if (exponent < 0) { intVal = BigInteger.valueOf(5).pow(-exponent).multiply(compactVal); scale = -exponent; } else { // (exponent > 0) --- 932,942 ---- int scale = 0; // Calculate intVal and scale BigInteger intVal; long compactVal = sign * significand; if (exponent == 0) { ! intVal = (compactVal == INFLATED) ? INFLATED_BIGINT : null; } else { if (exponent < 0) { intVal = BigInteger.valueOf(5).pow(-exponent).multiply(compactVal); scale = -exponent; } else { // (exponent > 0)
*** 4160,4178 **** case ROUND_FLOOR: // Towards -infinity return qsign < 0; default: // Some kind of half-way rounding ! if (roundingMode == ROUND_HALF_DOWN || ! cmpFracHalf < 0 ) // We're closer to higher digit return false; ! else if (roundingMode == ROUND_HALF_UP || ! cmpFracHalf > 0 ) // We're closer to lower digit return true; ! else ! // roundingMode == ROUND_HALF_EVEN, true iff quotient is odd return oddQuot; } } /** * Tests if quotient has to be incremented according the roundingMode --- 4159,4192 ---- case ROUND_FLOOR: // Towards -infinity return qsign < 0; default: // Some kind of half-way rounding ! assert roundingMode >= ROUND_HALF_UP && ! roundingMode <= ROUND_HALF_EVEN: "Unexpected rounding mode" + RoundingMode.valueOf(roundingMode); ! ! if (cmpFracHalf < 0 ) // We're closer to higher digit return false; ! else if (cmpFracHalf > 0 ) // We're closer to lower digit return true; ! else { // half-way ! assert cmpFracHalf == 0; ! ! switch(roundingMode) { ! case ROUND_HALF_DOWN: ! return false; ! ! case ROUND_HALF_UP: ! return true; ! ! case ROUND_HALF_EVEN: return oddQuot; + + default: + throw new AssertionError("Unexpected rounding mode" + roundingMode); + } + } } } /** * Tests if quotient has to be incremented according the roundingMode