Lines Matching +refs:val +refs:result
52 * exact result cannot be represented, an exception is thrown;
67 * corollary of computing the exact result, the rounding mode setting
73 * result, an {@code ArithmeticException} is thrown. Otherwise, the
74 * exact result of the division is returned, as done for other
89 * and rounding must specify both the numerical result and the scale
90 * used in the result's representation.
95 * the exact result has more digits (perhaps infinitely many in the
101 * the result's <i>precision</i>. The digit count starts from the
102 * leftmost nonzero digit of the exact result. The rounding mode
104 * result.
107 * though an exact intermediate result were first calculated and then
110 * result is not returned, some digit positions of the exact result
112 * returned result, it is possible for a new digit position to be
117 * the leading digit position of the returned result.
119 * <p>Besides a logical exact result, each arithmetic operation has a
120 * preferred scale for representing a result. The preferred
135 * larger scale since the exact result may have more digits. For
139 * result is the preferred scale for that operation. If the exact
140 * numerical result cannot be represented in {@code precision}
142 * of the result is reduced from the scale of the intermediate result
144 * digits actually returned. If the exact result can be represented
146 * of the result with the scale closest to the preferred scale is
160 * scale will equal the number of digit positions of the exact result
163 * result is discarded than when no new digit position is created.
166 * For example, the result of the {@code pow} method using the
168 * occasionally differ from the rounded mathematical result by more
351 * Trusted simply means if val is INFLATED, intVal could not be null and
352 * if intVal is null, val could not be INFLATED.
354 BigDecimal(BigInteger intVal, long val, int scale, int prec) {
357 this.intCompact = val;
571 * @throws ArithmeticException if the result is inexact but the
618 * @throws ArithmeticException if the result is inexact but the
733 * @param val String representation of {@code BigDecimal}.
735 * @throws NumberFormatException if {@code val} is not a valid
738 public BigDecimal(String val) {
739 this(val.toCharArray(), 0, val.length());
748 * @param val string representation of a {@code BigDecimal}.
750 * @throws ArithmeticException if the result is inexact but the
752 * @throws NumberFormatException if {@code val} is not a valid
756 public BigDecimal(String val, MathContext mc) {
757 this(val.toCharArray(), 0, val.length());
767 * <tt>(10<sup>scale</sup> × val)</tt> is an integer.
795 * exact conversion; it does not give the same result as
798 * {@link #BigDecimal(String)} constructor. To get that result,
802 * @param val {@code double} value to be converted to
804 * @throws NumberFormatException if {@code val} is infinite or NaN.
806 public BigDecimal(double val) {
807 if (Double.isInfinite(val) || Double.isNaN(val))
812 long valBits = Double.doubleToLongBits(val);
818 // At this point, val == sign * significand * 2**exponent.
856 * <tt>(10<sup>scale</sup> × val)</tt> is an integer.
862 * @param val {@code double} value to be converted to
865 * @throws ArithmeticException if the result is inexact but the
867 * @throws NumberFormatException if {@code val} is infinite or NaN.
870 public BigDecimal(double val, MathContext mc) {
871 this(val);
880 * @param val {@code BigInteger} value to be converted to
883 public BigDecimal(BigInteger val) {
884 intCompact = compactValFor(val);
885 intVal = (intCompact != INFLATED) ? null : val;
893 * @param val {@code BigInteger} value to be converted to
896 * @throws ArithmeticException if the result is inexact but the
900 public BigDecimal(BigInteger val, MathContext mc) {
901 this(val);
932 * @throws ArithmeticException if the result is inexact but the
947 * @param val {@code int} value to be converted to
951 public BigDecimal(int val) {
952 intCompact = val;
960 * @param val {@code int} value to be converted to {@code BigDecimal}.
962 * @throws ArithmeticException if the result is inexact but the
966 public BigDecimal(int val, MathContext mc) {
967 intCompact = val;
976 * @param val {@code long} value to be converted to {@code BigDecimal}.
979 public BigDecimal(long val) {
980 this.intCompact = val;
981 this.intVal = (val == INFLATED) ? BigInteger.valueOf(val) : null;
989 * @param val {@code long} value to be converted to {@code BigDecimal}.
991 * @throws ArithmeticException if the result is inexact but the
995 public BigDecimal(long val, MathContext mc) {
996 this(val);
1036 * @param val value of the {@code BigDecimal}.
1037 * @return a {@code BigDecimal} whose value is {@code val}.
1039 public static BigDecimal valueOf(long val) {
1040 if (val >= 0 && val < zeroThroughTen.length)
1041 return zeroThroughTen[(int)val];
1042 else if (val != INFLATED)
1043 return new BigDecimal(null, val, 0, 0);
1044 return new BigDecimal(BigInteger.valueOf(val), val, 0, 0);
1056 * result of using {@link Double#toString(double)}.
1058 * @param val {@code double} to convert to a {@code BigDecimal}.
1060 * equal to the value of {@code val}.
1061 * @throws NumberFormatException if {@code val} is infinite or NaN.
1064 public static BigDecimal valueOf(double val) {
1069 return new BigDecimal(Double.toString(val));
1125 * the other number, rounded if necessary, is used as the result.
1130 * @throws ArithmeticException if the result is inexact but the
1144 // scaled if necessary, is used as the result.
1151 BigDecimal result;
1157 result = lhsIsZero ? doRound(augend, mc) : doRound(lhs, mc);
1159 if (result.scale() == preferredScale)
1160 return result;
1161 else if (result.scale() > preferredScale) {
1163 new BigDecimal(result.intVal, result.intCompact,
1164 result.scale, 0);
1167 } else { // result.scale < preferredScale
1168 int precisionDiff = mc.precision - result.precision();
1169 int scaleDiff = preferredScale - result.scale();
1172 return result.setScale(preferredScale); // can achieve target scale
1174 return result.setScale(result.scale() + precisionDiff);
1198 * condensed into a {@literal "sticky bit"} and the end result will
1200 * result does not include the high order digit of the small
1208 * variable precision of the result as determined by the
1229 * This is the estimated scale of an ulp of the result; it
1230 * assumes that the result doesn't have a carry-out on a true
1243 * directly visible in the result.
1254 BigDecimal[] result = {big, small};
1255 return result;
1275 * result. If this is zero then the result is {@code subtrahend.negate(mc)}.
1280 * @throws ArithmeticException if the result is inexact but the
1341 * @throws ArithmeticException if the result is inexact but the
1354 * be performed to generate a result with the specified scale, the
1366 * the specified scale is insufficient to represent the result
1410 * trailing zeros of the result is stripped to match the preferredScale.
1499 * be performed to generate a result with the specified scale, the
1508 * the specified scale is insufficient to represent the result
1519 * rounding must be performed to generate a result with the given
1530 * {@code this.scale()} is insufficient to represent the result
1550 * rounding must be performed to generate a result with the given
1558 * {@code this.scale()} is insufficient to represent the result
1617 "no exact representable decimal result.");
1641 * @throws ArithmeticException if the result is inexact but the
1656 // to normalize the values here to achieve the desired result.
1662 // will give a result in the range 0.1 to 1 rounded to exactly
1663 // the right number of digits (except in the case of a result of
1685 // In order to find out whether the divide generates the exact result,
1708 * preferred scale of the result is {@code (this.scale() -
1753 * method. The preferred scale of the result is
1763 * @throws ArithmeticException if {@code mc.precision} {@literal >} 0 and the result
1769 if (mc.precision == 0 || // exact result
1770 (this.compareMagnitude(divisor) < 0) ) // zero result
1783 BigDecimal result = this.
1786 if (result.scale() < 0) {
1792 BigDecimal product = result.multiply(divisor);
1798 } else if (result.scale() > 0) {
1804 result = result.setScale(0, RoundingMode.DOWN);
1806 // else result.scale() == 0;
1809 if ((preferredScale > result.scale()) &&
1810 (precisionDiff = mc.precision - result.precision()) > 0) {
1811 return result.setScale(result.scale() +
1812 Math.min(precisionDiff, preferredScale - result.scale) );
1814 result.stripZerosToMatchScale(preferredScale);
1815 return result;
1824 * Note that this is not the modulo operation (the result can be
1849 * operation (the result can be negative).
1855 * @throws ArithmeticException if the result is inexact but the
1857 * {@literal >} 0 and the result of {@code this.divideToIntgralValue(divisor)} would
1869 * result of {@code divideToIntegralValue} followed by the result of
1880 * (the result of {@code divideToIntegralValue}) is the initial element
1889 BigDecimal[] result = new BigDecimal[2];
1891 result[0] = this.divideToIntegralValue(divisor);
1892 result[1] = this.subtract(result[0].multiply(divisor));
1893 return result;
1898 * result of {@code divideToIntegralValue} followed by the result of
1911 * (the result of {@code divideToIntegralValue}) is the
1914 * @throws ArithmeticException if the result is inexact but the
1916 * {@literal >} 0 and the result of {@code this.divideToIntgralValue(divisor)} would
1926 BigDecimal[] result = new BigDecimal[2];
1929 result[0] = lhs.divideToIntegralValue(divisor, mc);
1930 result[1] = lhs.subtract(result[0].multiply(divisor));
1931 return result;
1954 // No need to calculate pow(n) if result will over/underflow.
1986 * <li> if {@code n} is positive, the result is calculated via
1994 * <li> if {@code n} is negative, the result is calculated as if
2007 * @throws ArithmeticException if the result is inexact but the
2071 * @throws ArithmeticException if the result is inexact but the
2086 BigDecimal result;
2088 result = BigDecimal.valueOf(-intCompact, scale);
2090 result = new BigDecimal(intVal.negate(), scale);
2091 result.precision = precision;
2093 return result;
2102 * @throws ArithmeticException if the result is inexact but the
2134 * @return {@code this}, rounded as necessary. A zero result will
2136 * @throws ArithmeticException if the result is inexact but the
2183 int result = precision;
2184 if (result == 0) {
2187 result = longDigitLength(s);
2189 result = bigDigitLength(inflate());
2190 precision = result;
2192 return result;
2275 * result, hence no rounding is necessary. If this rounding mode is
2276 * specified on an operation that yields an inexact result, an
2316 * this method do <i>not</i> result in the original object being
2349 * this method do <i>not</i> result in the original object being
2426 * <p>This method returns the same result as the two-argument
2431 * calls of this method do <i>not</i> result in the original
2501 * the result is {@code (this.scale() - n)}.
2528 BigDecimal result = new BigDecimal(intVal, scale);
2529 result.stripZerosToMatchScale(Long.MIN_VALUE);
2530 return result;
2547 * @param val {@code BigDecimal} to which this {@code BigDecimal} is
2550 * less than, equal to, or greater than {@code val}.
2552 public int compareTo(BigDecimal val) {
2554 if (scale == val.scale) {
2556 long ys = val.intCompact;
2561 int ysign = val.signum();
2566 int cmp = compareMagnitude(val);
2573 private int compareMagnitude(BigDecimal val) {
2575 long ys = val.intCompact;
2582 int sdiff = this.scale - val.scale;
2586 int yae = val.precision() - val.scale; // [-1]
2597 return rb.compareMagnitude(val.intVal);
2603 rb = val.bigMultiplyPowerTen(sdiff);
2613 return this.intVal.compareMagnitude(val.intVal);
2655 * {@code val}.
2657 * @param val value with which the minimum is to be computed.
2659 * {@code BigDecimal} and {@code val}. If they are equal,
2664 public BigDecimal min(BigDecimal val) {
2665 return (compareTo(val) <= 0 ? this : val);
2669 * Returns the maximum of this {@code BigDecimal} and {@code val}.
2671 * @param val value with which the maximum is to be computed.
2673 * {@code BigDecimal} and {@code val}. If they are equal,
2678 public BigDecimal max(BigDecimal val) {
2679 return (compareTo(val) >= 0 ? this : val);
2775 * {@code BigDecimal} values and the result of this conversion.
2778 * as a result of using {@code toString}. If that string
2829 * #BigDecimal(String) string constructor}. The result of this method meets
2831 * result from applying the string constructor to the method's output.
2849 * in the result.
2856 * Note that if the result of this method is passed to the
2955 * as return a result with the opposite sign.
2969 * {@code long} result then an {@code ArithmeticException} is
3023 * value as well as return a result with the opposite sign.
3037 * {@code int} result then an {@code ArithmeticException} is
3057 * {@code short} result then an {@code ArithmeticException} is
3077 * {@code byte} result then an {@code ArithmeticException} is
3145 * equal to 1 with the scale of {@code this}. The result is
3146 * stored with the same scale as {@code this} so the result
3481 * Compute val * 10 ^ n; return this product if it is
3484 private static long longMultiplyPowerTen(long val, int n) {
3485 if (val == 0 || n <= 0)
3486 return val;
3491 if (val == 1)
3493 if (Math.abs(val) <= bounds[n])
3494 return val * tenpower;
3527 * <p>If the scales of val[0] and val[1] differ, rescale
3533 * @param val array of two elements referring to the two
3536 private static void matchScale(BigDecimal[] val) {
3537 if (val[0].scale == val[1].scale) {
3539 } else if (val[0].scale < val[1].scale) {
3540 val[0] = val[0].setScale(val[1].scale, ROUND_UNNECESSARY);
3541 } else if (val[1].scale < val[0].scale) {
3542 val[1] = val[1].setScale(val[0].scale, ROUND_UNNECESSARY);
3679 * @param val The new scale.
3684 private int checkScale(long val) {
3685 int asInt = (int)val;
3686 if (asInt != val) {
3687 asInt = val>Integer.MAX_VALUE ? Integer.MAX_VALUE : Integer.MIN_VALUE;
3702 * @throws ArithmeticException if the result is inexact but the
3742 * result is inexact.
3840 long val = intVal.longValue();
3841 if (val != intCompact) {
3844 intCompact + "\t intVal=" + val);