Lines Matching defs:scale

38  * <i>unscaled value</i> and a 32-bit integer <i>scale</i>.  If zero
39 * or positive, the scale is the number of digits to the right of the
41 * multiplied by ten to the power of the negation of the scale. The
43 * therefore <tt>(unscaledValue &times; 10<sup>-scale</sup>)</tt>.
46 * arithmetic, scale manipulation, rounding, comparison, hashing, and
89 * and rounding must specify both the numerical result and the scale
120 * preferred scale for representing a result. The preferred
121 * scale for each operation is listed in the table below.
127 * <tr><td>Add</td><td>max(addend.scale(), augend.scale())</td>
128 * <tr><td>Subtract</td><td>max(minuend.scale(), subtrahend.scale())</td>
129 * <tr><td>Multiply</td><td>multiplier.scale() + multiplicand.scale()</td>
130 * <tr><td>Divide</td><td>dividend.scale() - divisor.scale()</td>
135 * larger scale since the exact result may have more digits. For
138 * <p>Before rounding, the scale of the logical exact intermediate
139 * result is the preferred scale for that operation. If the exact
141 * digits, rounding selects the set of digits to return and the scale
142 * of the result is reduced from the scale of the intermediate result
143 * to the least scale which can represent the {@code precision}
146 * of the result with the scale closest to the preferred scale is
149 * trailing zeros and decreasing the scale. For example, rounding to
153 * {@code 19/100 = 0.19 // integer=19, scale=2} <br>
157 * {@code 21/110 = 0.190 // integer=190, scale=3} <br>
160 * scale will equal the number of digit positions of the exact result
171 * <p>Two types of operations are provided for manipulating the scale
176 * to that of the operand, but whose scale or precision is the
194 * the particular {@code BigInteger} and scale pair defining a
196 * {@code BigDecimal} numerically equal to 0.19 having a scale of 2.
230 * The scale of this BigDecimal, as returned by {@link #scale}.
233 * @see #scale
235 private int scale; // Note: this may have any value, so
324 * The value 0, with a scale of 0.
332 * The value 1, with a scale of 0.
340 * The value 10, with a scale of 0.
354 BigDecimal(BigInteger intVal, long val, int scale, int prec) {
355 this.scale = scale;
391 int scl = 0; // record scale value
518 // Adjust scale if exp is not zero.
549 this.scale = scl;
690 * <p>The scale of the returned {@code BigDecimal} will be the
694 * subtracted from the scale. The value of the resulting scale
707 * [{@code BigInteger}, {@code scale}] is shown on the right.
765 * binary floating-point value. The scale of the returned
767 * <tt>(10<sup>scale</sup> &times; val)</tt> is an integer.
775 * 0.1 (an unscaled value of 1, with a scale of 1), but it is
822 * and bogus scale calculation.
837 // Calculate intVal and scale
842 scale = -exponent;
854 * rounding according to the context settings. The scale of the
856 * <tt>(10<sup>scale</sup> &times; val)</tt> is an integer.
878 * The scale of the {@code BigDecimal} is zero.
890 * rounding according to the context settings. The scale of the
908 * {@code int} scale into a {@code BigDecimal}. The value of
910 * <tt>(unscaledVal &times; 10<sup>-scale</sup>)</tt>.
913 * @param scale scale of the {@code BigDecimal}.
915 public BigDecimal(BigInteger unscaledVal, int scale) {
918 this.scale = scale;
923 * {@code int} scale into a {@code BigDecimal}, with rounding
926 * 10<sup>-scale</sup>)</tt>, rounded according to the
930 * @param scale scale of the {@code BigDecimal}.
936 public BigDecimal(BigInteger unscaledVal, int scale, MathContext mc) {
938 this.scale = scale;
945 * scale of the {@code BigDecimal} is zero.
957 * rounding according to the context settings. The scale of the
974 * scale of the {@code BigDecimal} is zero.
986 * rounding according to the context settings. The scale of the
1005 * {@code int} scale into a {@code BigDecimal}. This
1011 * @param scale scale of the {@code BigDecimal}.
1013 * <tt>(unscaledVal &times; 10<sup>-scale</sup>)</tt>.
1015 public static BigDecimal valueOf(long unscaledVal, int scale) {
1016 if (scale == 0)
1019 if (scale > 0 && scale < ZERO_SCALED_BY.length)
1020 return ZERO_SCALED_BY[scale];
1022 return new BigDecimal(BigInteger.ZERO, 0, scale, 1);
1026 unscaledVal, scale, 0);
1031 * with a scale of zero. This {@literal "static factory method"}
1075 * augend)}, and whose scale is {@code max(this.scale(),
1076 * augend.scale())}.
1086 int rscale = this.scale;
1088 long sdiff = (long)rscale - augend.scale;
1092 rscale = augend.scale;
1150 int preferredScale = Math.max(lhs.scale(), augend.scale());
1159 if (result.scale() == preferredScale)
1161 else if (result.scale() > preferredScale) {
1164 result.scale, 0);
1167 } else { // result.scale < preferredScale
1169 int scaleDiff = preferredScale - result.scale();
1172 return result.setScale(preferredScale); // can achieve target scale
1174 return result.setScale(result.scale() + precisionDiff);
1179 long padding = (long)lhs.scale - augend.scale;
1188 lhs.scale);
1229 * This is the estimated scale of an ulp of the result; it
1234 long estResultUlpScale = (long)big.scale - big.precision() + mc.precision;
1237 * The low-order digit position of big is big.scale(). This
1239 * negative scale. The high-order digit position of small is
1240 * small.scale - (small.precision() - 1). To do the full
1245 long smallHighDigitPos = (long)small.scale - small.precision() + 1;
1246 if (smallHighDigitPos > big.scale + 2 && // big and small disjoint
1249 this.checkScale(Math.max(big.scale, estResultUlpScale) + 3));
1260 * subtrahend)}, and whose scale is {@code max(this.scale(),
1261 * subtrahend.scale())}.
1294 * multiplicand)</tt>, and whose scale is {@code (this.scale() +
1295 * multiplicand.scale())}.
1303 int productScale = checkScale((long)scale + multiplicand.scale);
1353 * divisor)}, and whose scale is as specified. If rounding must
1354 * be performed to generate a result with the specified scale, the
1361 * @param scale scale of the {@code BigDecimal} quotient to be returned.
1366 * the specified scale is insufficient to represent the result
1379 public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) {
1383 * scale is then modified.
1393 if (checkScale((long)scale + divisor.scale) > this.scale)
1394 dividend = this.setScale(scale + divisor.scale, ROUND_UNNECESSARY);
1396 divisor = divisor.setScale(checkScale((long)this.scale - scale),
1400 scale, roundingMode, scale);
1406 * returned {@code BigDecimal} object is the quotient whose scale is set to
1407 * the passed in scale. If the remainder is not zero, it will be rounded
1409 * the last parameter, i.e. preferredScale is NOT equal to scale, the
1414 int scale, int roundingMode,
1425 if (roundingMode == ROUND_DOWN && scale == preferredScale)
1426 return new BigDecimal(null, q, scale, 0);
1485 res = new BigDecimal(null, (increment ? q + qsign : q), scale, 0);
1489 res = mq.toBigDecimal(qsign, scale);
1491 if (isRemainderZero && preferredScale != scale)
1498 * divisor)}, and whose scale is as specified. If rounding must
1499 * be performed to generate a result with the specified scale, the
1503 * @param scale scale of the {@code BigDecimal} quotient to be returned.
1508 * the specified scale is insufficient to represent the result
1512 public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode) {
1513 return divide(divisor, scale, roundingMode.oldMode);
1518 * divisor)}, and whose scale is {@code this.scale()}. If
1520 * scale, the specified rounding mode is applied.
1530 * {@code this.scale()} is insufficient to represent the result
1544 return this.divide(divisor, scale, roundingMode);
1549 * divisor)}, and whose scale is {@code this.scale()}. If
1551 * scale, the specified rounding mode is applied.
1558 * {@code this.scale()} is insufficient to represent the result
1563 return this.divide(divisor, scale, roundingMode.oldMode);
1568 * divisor)}, and whose preferred scale is {@code (this.scale() -
1569 * divisor.scale())}; if the exact quotient cannot be
1590 // Calculate preferred scale
1591 int preferredScale = saturateLong((long)this.scale - divisor.scale);
1620 int quotientScale = quotient.scale();
1653 long preferredScale = (long)dividend.scale - divisor.scale;
1655 // divide-and-round method, but as this rounds to scale we have
1661 // Dividing x'/y' with the required scale set to mc.precision then
1683 yscale = divisor.scale -= 1; // [that is, divisor *= 10]
1687 // return BigDecimal object whose scale will be set to 'scl'.
1708 * preferred scale of the result is {@code (this.scale() -
1709 * divisor.scale())}.
1717 // Calculate preferred scale
1718 int preferredScale = saturateLong((long)this.scale - divisor.scale);
1732 Math.abs((long)this.scale() - divisor.scale()) + 2,
1736 if (quotient.scale > 0) {
1741 if (quotient.scale < preferredScale) {
1753 * method. The preferred scale of the result is
1754 * {@code (this.scale() - divisor.scale())}. An
1773 // Calculate preferred scale
1774 int preferredScale = saturateLong((long)this.scale - divisor.scale);
1781 * quotient and adjust the scale to the preferred value.
1786 if (result.scale() < 0) {
1798 } else if (result.scale() > 0) {
1801 * digits; recompute quotient to scale 0 to avoid double
1806 // else result.scale() == 0;
1809 if ((preferredScale > result.scale()) &&
1811 return result.setScale(result.scale() +
1812 Math.min(precisionDiff, preferredScale - result.scale) );
1956 int newScale = checkScale((long)scale * n);
2055 * of this {@code BigDecimal}, and whose scale is
2056 * {@code this.scale()}.
2081 * and whose scale is {@code this.scale()}.
2088 result = BigDecimal.valueOf(-intCompact, scale);
2090 result = new BigDecimal(intVal.negate(), scale);
2112 * scale is {@code this.scale()}.
2135 * have a scale of 0.
2160 * Returns the <i>scale</i> of this {@code BigDecimal}. If zero
2161 * or positive, the scale is the number of digits to the right of
2164 * scale. For example, a scale of {@code -3} means the unscaled
2167 * @return the scale of this {@code BigDecimal}.
2169 public int scale() {
2170 return scale;
2199 * 10<sup>this.scale()</sup>)</tt>.)
2306 * Returns a {@code BigDecimal} whose scale is the specified
2310 * scale is reduced by the operation, the unscaled value must be
2320 * scale; the returned object may or may not be newly allocated.
2322 * @param newScale scale of the {@code BigDecimal} value to be returned.
2324 * @return a {@code BigDecimal} whose scale is the specified value,
2339 * Returns a {@code BigDecimal} whose scale is the specified
2343 * scale is reduced by the operation, the unscaled value must be
2353 * scale; the returned object may or may not be newly allocated.
2358 * @param newScale scale of the {@code BigDecimal} value to be returned.
2360 * @return a {@code BigDecimal} whose scale is the specified value,
2382 int oldScale = this.scale;
2385 if (this.signum() == 0) // zero can have any scale
2413 * Returns a {@code BigDecimal} whose scale is the specified
2418 * <p>This call is typically used to increase the scale, in which
2420 * of the specified scale and the correct value. The call can
2421 * also be used to reduce the scale if the caller knows that the
2435 * object with the proper scale; the returned object may or may
2438 * @param newScale scale of the {@code BigDecimal} value to be returned.
2439 * @return a {@code BigDecimal} whose scale is the specified value, and
2458 * the scale. If {@code n} is negative, the call is equivalent
2461 * 10<sup>-n</sup>)</tt> and scale {@code max(this.scale()+n,
2467 * @throws ArithmeticException if scale overflows.
2471 int newScale = checkScale((long)scale + n);
2473 return num.scale < 0 ? num.setScale(0, ROUND_UNNECESSARY) : num;
2480 * {@code n} from the scale. If {@code n} is negative, the call
2483 * &times; 10<sup>n</sup>)</tt> and scale {@code max(this.scale()-n,
2489 * @throws ArithmeticException if scale overflows.
2493 int newScale = checkScale((long)scale - n);
2495 return num.scale < 0 ? num.setScale(0, ROUND_UNNECESSARY) : num;
2500 * ({@code this} * 10<sup>n</sup>). The scale of
2501 * the result is {@code (this.scale() - n)}.
2503 * @throws ArithmeticException if the scale would be
2510 checkScale((long)scale - n), precision);
2518 * [{@code BigInteger}, {@code scale}] components equals to
2520 * {@code scale}] components equals to [6, -2]
2528 BigDecimal result = new BigDecimal(intVal, scale);
2538 * equal in value but have a different scale (like 2.0 and 2.00)
2553 // Quick path for equal scale and non-inflated case.
2554 if (scale == val.scale) {
2582 int sdiff = this.scale - val.scale;
2585 int xae = this.precision() - this.scale; // [-1]
2586 int yae = val.precision() - val.scale; // [-1]
2621 * value and scale (thus 2.0 is not equal to 2.00 when compared by
2627 * {@code BigDecimal} whose value and scale are equal to this
2639 if (scale != xDec.scale)
2687 * differ in scale (like 2.0 and 2.00) will generally <i>not</i>
2699 return 31*((intCompact < 0) ?-temp:temp) + scale;
2701 return 31*intVal.hashCode() + scale;
2719 * negated scale, plus the number of characters in the converted
2721 * {@code -scale+(ulength-1)}, where {@code ulength} is the
2725 * <p>If the scale is greater than or equal to zero and the
2728 * exponential notation. In this case, if the scale is zero then
2729 * no decimal point is added and if the scale is positive a
2730 * decimal point will be inserted with the scale specifying the
2737 * <p>Otherwise (that is, if the scale is negative, or the
2758 * <p>For each representation [<i>unscaled value</i>, <i>scale</i>]
2777 * (unscaled value and scale) has a unique string representation
2824 * that the scale of the zero value is preserved. Note that
2827 * scale] pair of this {@code BigDecimal} if the output string is
2843 * without an exponent field. For values with a positive scale,
2845 * to indicate scale. For values with a zero or negative scale,
2847 * converted to a numerically equal value with zero scale and as
2848 * if all the trailing zeros of the zero scale value were present
2860 * may have a different scale. In particular, if this
2861 * {@code BigDecimal} has a negative scale, the string resulting
2862 * from this method will have a scale of zero when processed by
2876 if (bd.scale < 0)
2879 if (bd.scale == 0) // No decimal point
2881 return bd.getValueString(bd.signum(), bd.intVal.abs().toString(), bd.scale);
2885 private String getValueString(int signum, String intString, int scale) {
2888 int insertionPoint = intString.length() - scale;
2960 return (intCompact != INFLATED && scale == 0) ?
2978 if (intCompact != INFLATED && scale == 0)
2981 if ((precision() - scale) > 19) // [OK for negative scale too]
2987 if ((this.precision() - this.scale) <= 0)
3028 return (intCompact != INFLATED && scale == 0) ?
3110 if (scale == 0 && intCompact != INFLATED)
3133 if (scale == 0 && intCompact != INFLATED)
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
3148 * this.scale()]}.
3154 return BigDecimal.valueOf(1, this.scale());
3267 if (scale == 0) // zero scale is trivial
3292 long adjusted = -(long)scale + (coeffLen -1);
3293 if ((scale >= 0) && (adjusted >= -6)) { // plain number
3294 int pad = scale - coeffLen; // count of padding zeros
3305 buf.append(coeff, -pad + offset, scale);
3530 * replaced by a reference to a new object with the same scale as
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);
3560 // [all values of scale are now allowed]
3595 * pre-checks and opposite directionality) and then scale and
3641 * {@code BigDecimal} until the preferred scale is reached or no
3642 * more zeros can be removed. If the preferred scale is less than
3650 * @return this {@code BigDecimal} with a scale possibly reduced
3651 * to be closed to the preferred scale.
3657 scale > preferredScale) {
3664 scale = checkScale((long)scale-1); // could Overflow
3674 * Check a scale for Underflow or Overflow. If this BigDecimal is
3675 * nonzero, throw an exception if the scale is outof range. If this
3676 * is zero, saturate the scale to the extreme value of the right
3677 * sign if the scale is out of range.
3679 * @param val The new scale.
3681 * scale is out of range.
3682 * @return validated scale as an int.
3727 this.scale = rounded.scale;
3749 int newScale = d.checkScale((long)d.scale - drop);
3804 bd.scale,