code
stringlengths
11
173k
docstring
stringlengths
2
593k
func_name
stringlengths
2
189
language
stringclasses
1 value
repo
stringclasses
844 values
path
stringlengths
11
294
url
stringlengths
60
339
license
stringclasses
4 values
public BigDecimal(char[] in, int offset, int len) { this(in,offset,len,MathContext.UNLIMITED); }
Translates a character array representation of a {@code BigDecimal} into a {@code BigDecimal}, accepting the same sequence of characters as the {@link #BigDecimal(String)} constructor, while allowing a sub-array to be specified. <p>Note that if the sequence of characters is already available within a character array, using this constructor is faster than converting the {@code char} array to string and using the {@code BigDecimal(String)} constructor . @param in {@code char} array that is the source of characters. @param offset first character in the array to inspect. @param len number of characters to consider. @throws NumberFormatException if {@code in} is not a valid representation of a {@code BigDecimal} or the defined subarray is not wholly within {@code in}. @since 1.5
BigDecimal::BigDecimal
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal(char[] in, int offset, int len, MathContext mc) { // protect against huge length. if (offset + len > in.length || offset < 0) throw new NumberFormatException("Bad offset or len arguments for char[] input."); // This is the primary string to BigDecimal constructor; all // incoming strings end up here; it uses explicit (inline) // parsing for speed and generates at most one intermediate // (temporary) object (a char[] array) for non-compact case. // Use locals for all fields values until completion int prec = 0; // record precision value int scl = 0; // record scale value long rs = 0; // the compact value in long BigInteger rb = null; // the inflated value in BigInteger // use array bounds checking to handle too-long, len == 0, // bad offset, etc. try { // handle the sign boolean isneg = false; // assume positive if (in[offset] == '-') { isneg = true; // leading minus means negative offset++; len--; } else if (in[offset] == '+') { // leading + allowed offset++; len--; } // should now be at numeric part of the significand boolean dot = false; // true when there is a '.' long exp = 0; // exponent char c; // current character boolean isCompact = (len <= MAX_COMPACT_DIGITS); // integer significand array & idx is the index to it. The array // is ONLY used when we can't use a compact representation. int idx = 0; if (isCompact) { // First compact case, we need not to preserve the character // and we can just compute the value in place. for (; len > 0; offset++, len--) { c = in[offset]; if ((c == '0')) { // have zero if (prec == 0) prec = 1; else if (rs != 0) { rs *= 10; ++prec; } // else digit is a redundant leading zero if (dot) ++scl; } else if ((c >= '1' && c <= '9')) { // have digit int digit = c - '0'; if (prec != 1 || rs != 0) ++prec; // prec unchanged if preceded by 0s rs = rs * 10 + digit; if (dot) ++scl; } else if (c == '.') { // have dot // have dot if (dot) // two dots throw new NumberFormatException(); dot = true; } else if (Character.isDigit(c)) { // slow path int digit = Character.digit(c, 10); if (digit == 0) { if (prec == 0) prec = 1; else if (rs != 0) { rs *= 10; ++prec; } // else digit is a redundant leading zero } else { if (prec != 1 || rs != 0) ++prec; // prec unchanged if preceded by 0s rs = rs * 10 + digit; } if (dot) ++scl; } else if ((c == 'e') || (c == 'E')) { exp = parseExp(in, offset, len); // Next test is required for backwards compatibility if ((int) exp != exp) // overflow throw new NumberFormatException(); break; // [saves a test] } else { throw new NumberFormatException(); } } if (prec == 0) // no digits found throw new NumberFormatException(); // Adjust scale if exp is not zero. if (exp != 0) { // had significant exponent scl = adjustScale(scl, exp); } rs = isneg ? -rs : rs; int mcp = mc.precision; int drop = prec - mcp; // prec has range [1, MAX_INT], mcp has range [0, MAX_INT]; // therefore, this subtract cannot overflow if (mcp > 0 && drop > 0) { // do rounding while (drop > 0) { scl = checkScaleNonZero((long) scl - drop); rs = divideAndRound(rs, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode); prec = longDigitLength(rs); drop = prec - mcp; } } } else { char coeff[] = new char[len]; for (; len > 0; offset++, len--) { c = in[offset]; // have digit if ((c >= '0' && c <= '9') || Character.isDigit(c)) { // First compact case, we need not to preserve the character // and we can just compute the value in place. if (c == '0' || Character.digit(c, 10) == 0) { if (prec == 0) { coeff[idx] = c; prec = 1; } else if (idx != 0) { coeff[idx++] = c; ++prec; } // else c must be a redundant leading zero } else { if (prec != 1 || idx != 0) ++prec; // prec unchanged if preceded by 0s coeff[idx++] = c; } if (dot) ++scl; continue; } // have dot if (c == '.') { // have dot if (dot) // two dots throw new NumberFormatException(); dot = true; continue; } // exponent expected if ((c != 'e') && (c != 'E')) throw new NumberFormatException(); exp = parseExp(in, offset, len); // Next test is required for backwards compatibility if ((int) exp != exp) // overflow throw new NumberFormatException(); break; // [saves a test] } // here when no characters left if (prec == 0) // no digits found throw new NumberFormatException(); // Adjust scale if exp is not zero. if (exp != 0) { // had significant exponent scl = adjustScale(scl, exp); } // Remove leading zeros from precision (digits count) rb = new BigInteger(coeff, isneg ? -1 : 1, prec); rs = compactValFor(rb); int mcp = mc.precision; if (mcp > 0 && (prec > mcp)) { if (rs == INFLATED) { int drop = prec - mcp; while (drop > 0) { scl = checkScaleNonZero((long) scl - drop); rb = divideAndRoundByTenPow(rb, drop, mc.roundingMode.oldMode); rs = compactValFor(rb); if (rs != INFLATED) { prec = longDigitLength(rs); break; } prec = bigDigitLength(rb); drop = prec - mcp; } } if (rs != INFLATED) { int drop = prec - mcp; while (drop > 0) { scl = checkScaleNonZero((long) scl - drop); rs = divideAndRound(rs, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode); prec = longDigitLength(rs); drop = prec - mcp; } rb = null; } } } } catch (ArrayIndexOutOfBoundsException e) { throw new NumberFormatException(); } catch (NegativeArraySizeException e) { throw new NumberFormatException(); } this.scale = scl; this.precision = prec; this.intCompact = rs; this.intVal = rb; }
Translates a character array representation of a {@code BigDecimal} into a {@code BigDecimal}, accepting the same sequence of characters as the {@link #BigDecimal(String)} constructor, while allowing a sub-array to be specified and with rounding according to the context settings. <p>Note that if the sequence of characters is already available within a character array, using this constructor is faster than converting the {@code char} array to string and using the {@code BigDecimal(String)} constructor . @param in {@code char} array that is the source of characters. @param offset first character in the array to inspect. @param len number of characters to consider.. @param mc the context to use. @throws ArithmeticException if the result is inexact but the rounding mode is {@code UNNECESSARY}. @throws NumberFormatException if {@code in} is not a valid representation of a {@code BigDecimal} or the defined subarray is not wholly within {@code in}. @since 1.5
BigDecimal::BigDecimal
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
private static long parseExp(char[] in, int offset, int len){ long exp = 0; offset++; char c = in[offset]; len--; boolean negexp = (c == '-'); // optional sign if (negexp || c == '+') { offset++; c = in[offset]; len--; } if (len <= 0) // no exponent digits throw new NumberFormatException(); // skip leading zeros in the exponent while (len > 10 && (c=='0' || (Character.digit(c, 10) == 0))) { offset++; c = in[offset]; len--; } if (len > 10) // too many nonzero exponent digits throw new NumberFormatException(); // c now holds first digit of exponent for (;; len--) { int v; if (c >= '0' && c <= '9') { v = c - '0'; } else { v = Character.digit(c, 10); if (v < 0) // not a digit throw new NumberFormatException(); } exp = exp * 10 + v; if (len == 1) break; // that was final character offset++; c = in[offset]; } if (negexp) // apply sign exp = -exp; return exp; }
Translates a character array representation of a {@code BigDecimal} into a {@code BigDecimal}, accepting the same sequence of characters as the {@link #BigDecimal(String)} constructor, while allowing a sub-array to be specified and with rounding according to the context settings. <p>Note that if the sequence of characters is already available within a character array, using this constructor is faster than converting the {@code char} array to string and using the {@code BigDecimal(String)} constructor . @param in {@code char} array that is the source of characters. @param offset first character in the array to inspect. @param len number of characters to consider.. @param mc the context to use. @throws ArithmeticException if the result is inexact but the rounding mode is {@code UNNECESSARY}. @throws NumberFormatException if {@code in} is not a valid representation of a {@code BigDecimal} or the defined subarray is not wholly within {@code in}. @since 1.5 public BigDecimal(char[] in, int offset, int len, MathContext mc) { // protect against huge length. if (offset + len > in.length || offset < 0) throw new NumberFormatException("Bad offset or len arguments for char[] input."); // This is the primary string to BigDecimal constructor; all // incoming strings end up here; it uses explicit (inline) // parsing for speed and generates at most one intermediate // (temporary) object (a char[] array) for non-compact case. // Use locals for all fields values until completion int prec = 0; // record precision value int scl = 0; // record scale value long rs = 0; // the compact value in long BigInteger rb = null; // the inflated value in BigInteger // use array bounds checking to handle too-long, len == 0, // bad offset, etc. try { // handle the sign boolean isneg = false; // assume positive if (in[offset] == '-') { isneg = true; // leading minus means negative offset++; len--; } else if (in[offset] == '+') { // leading + allowed offset++; len--; } // should now be at numeric part of the significand boolean dot = false; // true when there is a '.' long exp = 0; // exponent char c; // current character boolean isCompact = (len <= MAX_COMPACT_DIGITS); // integer significand array & idx is the index to it. The array // is ONLY used when we can't use a compact representation. int idx = 0; if (isCompact) { // First compact case, we need not to preserve the character // and we can just compute the value in place. for (; len > 0; offset++, len--) { c = in[offset]; if ((c == '0')) { // have zero if (prec == 0) prec = 1; else if (rs != 0) { rs *= 10; ++prec; } // else digit is a redundant leading zero if (dot) ++scl; } else if ((c >= '1' && c <= '9')) { // have digit int digit = c - '0'; if (prec != 1 || rs != 0) ++prec; // prec unchanged if preceded by 0s rs = rs * 10 + digit; if (dot) ++scl; } else if (c == '.') { // have dot // have dot if (dot) // two dots throw new NumberFormatException(); dot = true; } else if (Character.isDigit(c)) { // slow path int digit = Character.digit(c, 10); if (digit == 0) { if (prec == 0) prec = 1; else if (rs != 0) { rs *= 10; ++prec; } // else digit is a redundant leading zero } else { if (prec != 1 || rs != 0) ++prec; // prec unchanged if preceded by 0s rs = rs * 10 + digit; } if (dot) ++scl; } else if ((c == 'e') || (c == 'E')) { exp = parseExp(in, offset, len); // Next test is required for backwards compatibility if ((int) exp != exp) // overflow throw new NumberFormatException(); break; // [saves a test] } else { throw new NumberFormatException(); } } if (prec == 0) // no digits found throw new NumberFormatException(); // Adjust scale if exp is not zero. if (exp != 0) { // had significant exponent scl = adjustScale(scl, exp); } rs = isneg ? -rs : rs; int mcp = mc.precision; int drop = prec - mcp; // prec has range [1, MAX_INT], mcp has range [0, MAX_INT]; // therefore, this subtract cannot overflow if (mcp > 0 && drop > 0) { // do rounding while (drop > 0) { scl = checkScaleNonZero((long) scl - drop); rs = divideAndRound(rs, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode); prec = longDigitLength(rs); drop = prec - mcp; } } } else { char coeff[] = new char[len]; for (; len > 0; offset++, len--) { c = in[offset]; // have digit if ((c >= '0' && c <= '9') || Character.isDigit(c)) { // First compact case, we need not to preserve the character // and we can just compute the value in place. if (c == '0' || Character.digit(c, 10) == 0) { if (prec == 0) { coeff[idx] = c; prec = 1; } else if (idx != 0) { coeff[idx++] = c; ++prec; } // else c must be a redundant leading zero } else { if (prec != 1 || idx != 0) ++prec; // prec unchanged if preceded by 0s coeff[idx++] = c; } if (dot) ++scl; continue; } // have dot if (c == '.') { // have dot if (dot) // two dots throw new NumberFormatException(); dot = true; continue; } // exponent expected if ((c != 'e') && (c != 'E')) throw new NumberFormatException(); exp = parseExp(in, offset, len); // Next test is required for backwards compatibility if ((int) exp != exp) // overflow throw new NumberFormatException(); break; // [saves a test] } // here when no characters left if (prec == 0) // no digits found throw new NumberFormatException(); // Adjust scale if exp is not zero. if (exp != 0) { // had significant exponent scl = adjustScale(scl, exp); } // Remove leading zeros from precision (digits count) rb = new BigInteger(coeff, isneg ? -1 : 1, prec); rs = compactValFor(rb); int mcp = mc.precision; if (mcp > 0 && (prec > mcp)) { if (rs == INFLATED) { int drop = prec - mcp; while (drop > 0) { scl = checkScaleNonZero((long) scl - drop); rb = divideAndRoundByTenPow(rb, drop, mc.roundingMode.oldMode); rs = compactValFor(rb); if (rs != INFLATED) { prec = longDigitLength(rs); break; } prec = bigDigitLength(rb); drop = prec - mcp; } } if (rs != INFLATED) { int drop = prec - mcp; while (drop > 0) { scl = checkScaleNonZero((long) scl - drop); rs = divideAndRound(rs, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode); prec = longDigitLength(rs); drop = prec - mcp; } rb = null; } } } } catch (ArrayIndexOutOfBoundsException e) { throw new NumberFormatException(); } catch (NegativeArraySizeException e) { throw new NumberFormatException(); } this.scale = scl; this.precision = prec; this.intCompact = rs; this.intVal = rb; } private int adjustScale(int scl, long exp) { long adjustedScale = scl - exp; if (adjustedScale > Integer.MAX_VALUE || adjustedScale < Integer.MIN_VALUE) throw new NumberFormatException("Scale out of range."); scl = (int) adjustedScale; return scl; } /* parse exponent
BigDecimal::parseExp
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal(char[] in) { this(in, 0, in.length); }
Translates a character array representation of a {@code BigDecimal} into a {@code BigDecimal}, accepting the same sequence of characters as the {@link #BigDecimal(String)} constructor. <p>Note that if the sequence of characters is already available as a character array, using this constructor is faster than converting the {@code char} array to string and using the {@code BigDecimal(String)} constructor . @param in {@code char} array that is the source of characters. @throws NumberFormatException if {@code in} is not a valid representation of a {@code BigDecimal}. @since 1.5
BigDecimal::BigDecimal
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal(char[] in, MathContext mc) { this(in, 0, in.length, mc); }
Translates a character array representation of a {@code BigDecimal} into a {@code BigDecimal}, accepting the same sequence of characters as the {@link #BigDecimal(String)} constructor and with rounding according to the context settings. <p>Note that if the sequence of characters is already available as a character array, using this constructor is faster than converting the {@code char} array to string and using the {@code BigDecimal(String)} constructor . @param in {@code char} array that is the source of characters. @param mc the context to use. @throws ArithmeticException if the result is inexact but the rounding mode is {@code UNNECESSARY}. @throws NumberFormatException if {@code in} is not a valid representation of a {@code BigDecimal}. @since 1.5
BigDecimal::BigDecimal
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal(String val) { this(val.toCharArray(), 0, val.length()); }
Translates the string representation of a {@code BigDecimal} into a {@code BigDecimal}. The string representation consists of an optional sign, {@code '+'} (<tt> '&#92;u002B'</tt>) or {@code '-'} (<tt>'&#92;u002D'</tt>), followed by a sequence of zero or more decimal digits ("the integer"), optionally followed by a fraction, optionally followed by an exponent. <p>The fraction consists of a decimal point followed by zero or more decimal digits. The string must contain at least one digit in either the integer or the fraction. The number formed by the sign, the integer and the fraction is referred to as the <i>significand</i>. <p>The exponent consists of the character {@code 'e'} (<tt>'&#92;u0065'</tt>) or {@code 'E'} (<tt>'&#92;u0045'</tt>) followed by one or more decimal digits. The value of the exponent must lie between -{@link Integer#MAX_VALUE} ({@link Integer#MIN_VALUE}+1) and {@link Integer#MAX_VALUE}, inclusive. <p>More formally, the strings this constructor accepts are described by the following grammar: <blockquote> <dl> <dt><i>BigDecimalString:</i> <dd><i>Sign<sub>opt</sub> Significand Exponent<sub>opt</sub></i> <dt><i>Sign:</i> <dd>{@code +} <dd>{@code -} <dt><i>Significand:</i> <dd><i>IntegerPart</i> {@code .} <i>FractionPart<sub>opt</sub></i> <dd>{@code .} <i>FractionPart</i> <dd><i>IntegerPart</i> <dt><i>IntegerPart:</i> <dd><i>Digits</i> <dt><i>FractionPart:</i> <dd><i>Digits</i> <dt><i>Exponent:</i> <dd><i>ExponentIndicator SignedInteger</i> <dt><i>ExponentIndicator:</i> <dd>{@code e} <dd>{@code E} <dt><i>SignedInteger:</i> <dd><i>Sign<sub>opt</sub> Digits</i> <dt><i>Digits:</i> <dd><i>Digit</i> <dd><i>Digits Digit</i> <dt><i>Digit:</i> <dd>any character for which {@link Character#isDigit} returns {@code true}, including 0, 1, 2 ... </dl> </blockquote> <p>The scale of the returned {@code BigDecimal} will be the number of digits in the fraction, or zero if the string contains no decimal point, subject to adjustment for any exponent; if the string contains an exponent, the exponent is subtracted from the scale. The value of the resulting scale must lie between {@code Integer.MIN_VALUE} and {@code Integer.MAX_VALUE}, inclusive. <p>The character-to-digit mapping is provided by {@link java.lang.Character#digit} set to convert to radix 10. The String may not contain any extraneous characters (whitespace, for example). <p><b>Examples:</b><br> The value of the returned {@code BigDecimal} is equal to <i>significand</i> &times; 10<sup>&nbsp;<i>exponent</i></sup>. For each string on the left, the resulting representation [{@code BigInteger}, {@code scale}] is shown on the right. <pre> "0" [0,0] "0.00" [0,2] "123" [123,0] "-123" [-123,0] "1.23E3" [123,-1] "1.23E+3" [123,-1] "12.3E+7" [123,-6] "12.0" [120,1] "12.3" [123,1] "0.00123" [123,5] "-1.23E-12" [-123,14] "1234.5E-4" [12345,5] "0E+7" [0,-7] "-0" [0,0] </pre> <p>Note: For values other than {@code float} and {@code double} NaN and &plusmn;Infinity, this constructor is compatible with the values returned by {@link Float#toString} and {@link Double#toString}. This is generally the preferred way to convert a {@code float} or {@code double} into a BigDecimal, as it doesn't suffer from the unpredictability of the {@link #BigDecimal(double)} constructor. @param val String representation of {@code BigDecimal}. @throws NumberFormatException if {@code val} is not a valid representation of a {@code BigDecimal}.
BigDecimal::BigDecimal
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal(String val, MathContext mc) { this(val.toCharArray(), 0, val.length(), mc); }
Translates the string representation of a {@code BigDecimal} into a {@code BigDecimal}, accepting the same strings as the {@link #BigDecimal(String)} constructor, with rounding according to the context settings. @param val string representation of a {@code BigDecimal}. @param mc the context to use. @throws ArithmeticException if the result is inexact but the rounding mode is {@code UNNECESSARY}. @throws NumberFormatException if {@code val} is not a valid representation of a BigDecimal. @since 1.5
BigDecimal::BigDecimal
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal(double val) { this(val,MathContext.UNLIMITED); }
Translates a {@code double} into a {@code BigDecimal} which is the exact decimal representation of the {@code double}'s binary floating-point value. The scale of the returned {@code BigDecimal} is the smallest value such that <tt>(10<sup>scale</sup> &times; val)</tt> is an integer. <p> <b>Notes:</b> <ol> <li> The results of this constructor can be somewhat unpredictable. One might assume that writing {@code new BigDecimal(0.1)} in Java creates a {@code BigDecimal} which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be represented exactly as a {@code double} (or, for that matter, as a binary fraction of any finite length). Thus, the value that is being passed <i>in</i> to the constructor is not exactly equal to 0.1, appearances notwithstanding. <li> The {@code String} constructor, on the other hand, is perfectly predictable: writing {@code new BigDecimal("0.1")} creates a {@code BigDecimal} which is <i>exactly</i> equal to 0.1, as one would expect. Therefore, it is generally recommended that the {@linkplain #BigDecimal(String) <tt>String</tt> constructor} be used in preference to this one. <li> When a {@code double} must be used as a source for a {@code BigDecimal}, note that this constructor provides an exact conversion; it does not give the same result as converting the {@code double} to a {@code String} using the {@link Double#toString(double)} method and then using the {@link #BigDecimal(String)} constructor. To get that result, use the {@code static} {@link #valueOf(double)} method. </ol> @param val {@code double} value to be converted to {@code BigDecimal}. @throws NumberFormatException if {@code val} is infinite or NaN.
BigDecimal::BigDecimal
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
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. */ if (significand == 0) { this.intVal = BigInteger.ZERO; this.scale = 0; this.intCompact = 0; this.precision = 1; return; } // Normalize while ((significand & 1) == 0) { // i.e., significand is even significand >>= 1; exponent++; } 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) intVal = BigInteger.valueOf(2).pow(exponent).multiply(compactVal); } compactVal = compactValFor(intVal); } int prec = 0; int mcp = mc.precision; if (mcp > 0) { // do rounding int mode = mc.roundingMode.oldMode; int drop; if (compactVal == INFLATED) { prec = bigDigitLength(intVal); drop = prec - mcp; while (drop > 0) { scale = checkScaleNonZero((long) scale - drop); intVal = divideAndRoundByTenPow(intVal, drop, mode); compactVal = compactValFor(intVal); if (compactVal != INFLATED) { break; } prec = bigDigitLength(intVal); drop = prec - mcp; } } if (compactVal != INFLATED) { prec = longDigitLength(compactVal); drop = prec - mcp; while (drop > 0) { scale = checkScaleNonZero((long) scale - drop); compactVal = divideAndRound(compactVal, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode); prec = longDigitLength(compactVal); drop = prec - mcp; } intVal = null; } } this.intVal = intVal; this.intCompact = compactVal; this.scale = scale; this.precision = prec; }
Translates a {@code double} into a {@code BigDecimal}, with rounding according to the context settings. The scale of the {@code BigDecimal} is the smallest value such that <tt>(10<sup>scale</sup> &times; val)</tt> is an integer. <p>The results of this constructor can be somewhat unpredictable and its use is generally not recommended; see the notes under the {@link #BigDecimal(double)} constructor. @param val {@code double} value to be converted to {@code BigDecimal}. @param mc the context to use. @throws ArithmeticException if the result is inexact but the RoundingMode is UNNECESSARY. @throws NumberFormatException if {@code val} is infinite or NaN. @since 1.5
BigDecimal::BigDecimal
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal(BigInteger val) { scale = 0; intVal = val; intCompact = compactValFor(val); }
Translates a {@code BigInteger} into a {@code BigDecimal}. The scale of the {@code BigDecimal} is zero. @param val {@code BigInteger} value to be converted to {@code BigDecimal}.
BigDecimal::BigDecimal
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal(BigInteger val, MathContext mc) { this(val,0,mc); }
Translates a {@code BigInteger} into a {@code BigDecimal} rounding according to the context settings. The scale of the {@code BigDecimal} is zero. @param val {@code BigInteger} value to be converted to {@code BigDecimal}. @param mc the context to use. @throws ArithmeticException if the result is inexact but the rounding mode is {@code UNNECESSARY}. @since 1.5
BigDecimal::BigDecimal
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal(BigInteger unscaledVal, int scale) { // Negative scales are now allowed this.intVal = unscaledVal; this.intCompact = compactValFor(unscaledVal); this.scale = scale; }
Translates a {@code BigInteger} unscaled value and an {@code int} scale into a {@code BigDecimal}. The value of the {@code BigDecimal} is <tt>(unscaledVal &times; 10<sup>-scale</sup>)</tt>. @param unscaledVal unscaled value of the {@code BigDecimal}. @param scale scale of the {@code BigDecimal}.
BigDecimal::BigDecimal
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal(BigInteger unscaledVal, int scale, MathContext mc) { long compactVal = compactValFor(unscaledVal); int mcp = mc.precision; int prec = 0; if (mcp > 0) { // do rounding int mode = mc.roundingMode.oldMode; if (compactVal == INFLATED) { prec = bigDigitLength(unscaledVal); int drop = prec - mcp; while (drop > 0) { scale = checkScaleNonZero((long) scale - drop); unscaledVal = divideAndRoundByTenPow(unscaledVal, drop, mode); compactVal = compactValFor(unscaledVal); if (compactVal != INFLATED) { break; } prec = bigDigitLength(unscaledVal); drop = prec - mcp; } } if (compactVal != INFLATED) { prec = longDigitLength(compactVal); int drop = prec - mcp; // drop can't be more than 18 while (drop > 0) { scale = checkScaleNonZero((long) scale - drop); compactVal = divideAndRound(compactVal, LONG_TEN_POWERS_TABLE[drop], mode); prec = longDigitLength(compactVal); drop = prec - mcp; } unscaledVal = null; } } this.intVal = unscaledVal; this.intCompact = compactVal; this.scale = scale; this.precision = prec; }
Translates a {@code BigInteger} unscaled value and an {@code int} scale into a {@code BigDecimal}, with rounding according to the context settings. The value of the {@code BigDecimal} is <tt>(unscaledVal &times; 10<sup>-scale</sup>)</tt>, rounded according to the {@code precision} and rounding mode settings. @param unscaledVal unscaled value of the {@code BigDecimal}. @param scale scale of the {@code BigDecimal}. @param mc the context to use. @throws ArithmeticException if the result is inexact but the rounding mode is {@code UNNECESSARY}. @since 1.5
BigDecimal::BigDecimal
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal(int val) { this.intCompact = val; this.scale = 0; this.intVal = null; }
Translates an {@code int} into a {@code BigDecimal}. The scale of the {@code BigDecimal} is zero. @param val {@code int} value to be converted to {@code BigDecimal}. @since 1.5
BigDecimal::BigDecimal
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal(int val, MathContext mc) { int mcp = mc.precision; long compactVal = val; int scale = 0; int prec = 0; if (mcp > 0) { // do rounding prec = longDigitLength(compactVal); int drop = prec - mcp; // drop can't be more than 18 while (drop > 0) { scale = checkScaleNonZero((long) scale - drop); compactVal = divideAndRound(compactVal, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode); prec = longDigitLength(compactVal); drop = prec - mcp; } } this.intVal = null; this.intCompact = compactVal; this.scale = scale; this.precision = prec; }
Translates an {@code int} into a {@code BigDecimal}, with rounding according to the context settings. The scale of the {@code BigDecimal}, before any rounding, is zero. @param val {@code int} value to be converted to {@code BigDecimal}. @param mc the context to use. @throws ArithmeticException if the result is inexact but the rounding mode is {@code UNNECESSARY}. @since 1.5
BigDecimal::BigDecimal
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal(long val) { this.intCompact = val; this.intVal = (val == INFLATED) ? INFLATED_BIGINT : null; this.scale = 0; }
Translates a {@code long} into a {@code BigDecimal}. The scale of the {@code BigDecimal} is zero. @param val {@code long} value to be converted to {@code BigDecimal}. @since 1.5
BigDecimal::BigDecimal
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal(long val, MathContext mc) { int mcp = mc.precision; int mode = mc.roundingMode.oldMode; int prec = 0; int scale = 0; BigInteger intVal = (val == INFLATED) ? INFLATED_BIGINT : null; if (mcp > 0) { // do rounding if (val == INFLATED) { prec = 19; int drop = prec - mcp; while (drop > 0) { scale = checkScaleNonZero((long) scale - drop); intVal = divideAndRoundByTenPow(intVal, drop, mode); val = compactValFor(intVal); if (val != INFLATED) { break; } prec = bigDigitLength(intVal); drop = prec - mcp; } } if (val != INFLATED) { prec = longDigitLength(val); int drop = prec - mcp; while (drop > 0) { scale = checkScaleNonZero((long) scale - drop); val = divideAndRound(val, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode); prec = longDigitLength(val); drop = prec - mcp; } intVal = null; } } this.intVal = intVal; this.intCompact = val; this.scale = scale; this.precision = prec; }
Translates a {@code long} into a {@code BigDecimal}, with rounding according to the context settings. The scale of the {@code BigDecimal}, before any rounding, is zero. @param val {@code long} value to be converted to {@code BigDecimal}. @param mc the context to use. @throws ArithmeticException if the result is inexact but the rounding mode is {@code UNNECESSARY}. @since 1.5
BigDecimal::BigDecimal
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public static BigDecimal valueOf(long unscaledVal, int scale) { if (scale == 0) return valueOf(unscaledVal); else if (unscaledVal == 0) { return zeroValueOf(scale); } return new BigDecimal(unscaledVal == INFLATED ? INFLATED_BIGINT : null, unscaledVal, scale, 0); }
Translates a {@code long} unscaled value and an {@code int} scale into a {@code BigDecimal}. This {@literal "static factory method"} is provided in preference to a ({@code long}, {@code int}) constructor because it allows for reuse of frequently used {@code BigDecimal} values.. @param unscaledVal unscaled value of the {@code BigDecimal}. @param scale scale of the {@code BigDecimal}. @return a {@code BigDecimal} whose value is <tt>(unscaledVal &times; 10<sup>-scale</sup>)</tt>.
BigDecimal::valueOf
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public static BigDecimal valueOf(long val) { if (val >= 0 && val < zeroThroughTen.length) return zeroThroughTen[(int)val]; else if (val != INFLATED) return new BigDecimal(null, val, 0, 0); return new BigDecimal(INFLATED_BIGINT, val, 0, 0); }
Translates a {@code long} value into a {@code BigDecimal} with a scale of zero. This {@literal "static factory method"} is provided in preference to a ({@code long}) constructor because it allows for reuse of frequently used {@code BigDecimal} values. @param val value of the {@code BigDecimal}. @return a {@code BigDecimal} whose value is {@code val}.
BigDecimal::valueOf
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public static BigDecimal valueOf(double val) { // Reminder: a zero double returns '0.0', so we cannot fastpath // to use the constant ZERO. This might be important enough to // justify a factory approach, a cache, or a few private // constants, later. return new BigDecimal(Double.toString(val)); }
Translates a {@code double} into a {@code BigDecimal}, using the {@code double}'s canonical string representation provided by the {@link Double#toString(double)} method. <p><b>Note:</b> This is generally the preferred way to convert a {@code double} (or {@code float}) into a {@code BigDecimal}, as the value returned is equal to that resulting from constructing a {@code BigDecimal} from the result of using {@link Double#toString(double)}. @param val {@code double} to convert to a {@code BigDecimal}. @return a {@code BigDecimal} whose value is equal to or approximately equal to the value of {@code val}. @throws NumberFormatException if {@code val} is infinite or NaN. @since 1.5
BigDecimal::valueOf
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal add(BigDecimal augend) { if (this.intCompact != INFLATED) { if ((augend.intCompact != INFLATED)) { return add(this.intCompact, this.scale, augend.intCompact, augend.scale); } else { return add(this.intCompact, this.scale, augend.intVal, augend.scale); } } else { if ((augend.intCompact != INFLATED)) { return add(augend.intCompact, augend.scale, this.intVal, this.scale); } else { return add(this.intVal, this.scale, augend.intVal, augend.scale); } } }
Returns a {@code BigDecimal} whose value is {@code (this + augend)}, and whose scale is {@code max(this.scale(), augend.scale())}. @param augend value to be added to this {@code BigDecimal}. @return {@code this + augend}
BigDecimal::add
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal add(BigDecimal augend, MathContext mc) { if (mc.precision == 0) return add(augend); BigDecimal lhs = this; // If either number is zero then the other number, rounded and // scaled if necessary, is used as the result. { boolean lhsIsZero = lhs.signum() == 0; boolean augendIsZero = augend.signum() == 0; if (lhsIsZero || augendIsZero) { int preferredScale = Math.max(lhs.scale(), augend.scale()); BigDecimal result; if (lhsIsZero && augendIsZero) return zeroValueOf(preferredScale); result = lhsIsZero ? doRound(augend, mc) : doRound(lhs, mc); if (result.scale() == preferredScale) return result; else if (result.scale() > preferredScale) { return stripZerosToMatchScale(result.intVal, result.intCompact, result.scale, preferredScale); } else { // result.scale < preferredScale int precisionDiff = mc.precision - result.precision(); int scaleDiff = preferredScale - result.scale(); if (precisionDiff >= scaleDiff) return result.setScale(preferredScale); // can achieve target scale else return result.setScale(result.scale() + precisionDiff); } } } long padding = (long) lhs.scale - augend.scale; if (padding != 0) { // scales differ; alignment needed BigDecimal arg[] = preAlign(lhs, augend, padding, mc); matchScale(arg); lhs = arg[0]; augend = arg[1]; } return doRound(lhs.inflated().add(augend.inflated()), lhs.scale, mc); }
Returns a {@code BigDecimal} whose value is {@code (this + augend)}, with rounding according to the context settings. If either number is zero and the precision setting is nonzero then the other number, rounded if necessary, is used as the result. @param augend value to be added to this {@code BigDecimal}. @param mc the context to use. @return {@code this + augend}, rounded as necessary. @throws ArithmeticException if the result is inexact but the rounding mode is {@code UNNECESSARY}. @since 1.5
BigDecimal::add
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
private BigDecimal[] preAlign(BigDecimal lhs, BigDecimal augend, long padding, MathContext mc) { assert padding != 0; BigDecimal big; BigDecimal small; if (padding < 0) { // lhs is big; augend is small big = lhs; small = augend; } else { // lhs is small; augend is big big = augend; small = lhs; } /* * This is the estimated scale of an ulp of the result; it assumes that * the result doesn't have a carry-out on a true add (e.g. 999 + 1 => * 1000) or any subtractive cancellation on borrowing (e.g. 100 - 1.2 => * 98.8) */ long estResultUlpScale = (long) big.scale - big.precision() + mc.precision; /* * The low-order digit position of big is big.scale(). This * is true regardless of whether big has a positive or * negative scale. The high-order digit position of small is * small.scale - (small.precision() - 1). To do the full * condensation, the digit positions of big and small must be * disjoint *and* the digit positions of small should not be * directly visible in the result. */ long smallHighDigitPos = (long) small.scale - small.precision() + 1; if (smallHighDigitPos > big.scale + 2 && // big and small disjoint smallHighDigitPos > estResultUlpScale + 2) { // small digits not visible small = BigDecimal.valueOf(small.signum(), this.checkScale(Math.max(big.scale, estResultUlpScale) + 3)); } // Since addition is symmetric, preserving input order in // returned operands doesn't matter BigDecimal[] result = {big, small}; return result; }
Returns an array of length two, the sum of whose entries is equal to the rounded sum of the {@code BigDecimal} arguments. <p>If the digit positions of the arguments have a sufficient gap between them, the value smaller in magnitude can be condensed into a {@literal "sticky bit"} and the end result will round the same way <em>if</em> the precision of the final result does not include the high order digit of the small magnitude operand. <p>Note that while strictly speaking this is an optimization, it makes a much wider range of additions practical. <p>This corresponds to a pre-shift operation in a fixed precision floating-point adder; this method is complicated by variable precision of the result as determined by the MathContext. A more nuanced operation could implement a {@literal "right shift"} on the smaller magnitude operand so that the number of digits of the smaller operand could be reduced even though the significands partially overlapped.
BigDecimal::preAlign
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal subtract(BigDecimal subtrahend) { if (this.intCompact != INFLATED) { if ((subtrahend.intCompact != INFLATED)) { return add(this.intCompact, this.scale, -subtrahend.intCompact, subtrahend.scale); } else { return add(this.intCompact, this.scale, subtrahend.intVal.negate(), subtrahend.scale); } } else { if ((subtrahend.intCompact != INFLATED)) { // Pair of subtrahend values given before pair of // values from this BigDecimal to avoid need for // method overloading on the specialized add method return add(-subtrahend.intCompact, subtrahend.scale, this.intVal, this.scale); } else { return add(this.intVal, this.scale, subtrahend.intVal.negate(), subtrahend.scale); } } }
Returns a {@code BigDecimal} whose value is {@code (this - subtrahend)}, and whose scale is {@code max(this.scale(), subtrahend.scale())}. @param subtrahend value to be subtracted from this {@code BigDecimal}. @return {@code this - subtrahend}
BigDecimal::subtract
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal subtract(BigDecimal subtrahend, MathContext mc) { if (mc.precision == 0) return subtract(subtrahend); // share the special rounding code in add() return add(subtrahend.negate(), mc); }
Returns a {@code BigDecimal} whose value is {@code (this - subtrahend)}, with rounding according to the context settings. If {@code subtrahend} is zero then this, rounded if necessary, is used as the result. If this is zero then the result is {@code subtrahend.negate(mc)}. @param subtrahend value to be subtracted from this {@code BigDecimal}. @param mc the context to use. @return {@code this - subtrahend}, rounded as necessary. @throws ArithmeticException if the result is inexact but the rounding mode is {@code UNNECESSARY}. @since 1.5
BigDecimal::subtract
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal multiply(BigDecimal multiplicand) { int productScale = checkScale((long) scale + multiplicand.scale); if (this.intCompact != INFLATED) { if ((multiplicand.intCompact != INFLATED)) { return multiply(this.intCompact, multiplicand.intCompact, productScale); } else { return multiply(this.intCompact, multiplicand.intVal, productScale); } } else { if ((multiplicand.intCompact != INFLATED)) { return multiply(multiplicand.intCompact, this.intVal, productScale); } else { return multiply(this.intVal, multiplicand.intVal, productScale); } } }
Returns a {@code BigDecimal} whose value is <tt>(this &times; multiplicand)</tt>, and whose scale is {@code (this.scale() + multiplicand.scale())}. @param multiplicand value to be multiplied by this {@code BigDecimal}. @return {@code this * multiplicand}
BigDecimal::multiply
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal multiply(BigDecimal multiplicand, MathContext mc) { if (mc.precision == 0) return multiply(multiplicand); int productScale = checkScale((long) scale + multiplicand.scale); if (this.intCompact != INFLATED) { if ((multiplicand.intCompact != INFLATED)) { return multiplyAndRound(this.intCompact, multiplicand.intCompact, productScale, mc); } else { return multiplyAndRound(this.intCompact, multiplicand.intVal, productScale, mc); } } else { if ((multiplicand.intCompact != INFLATED)) { return multiplyAndRound(multiplicand.intCompact, this.intVal, productScale, mc); } else { return multiplyAndRound(this.intVal, multiplicand.intVal, productScale, mc); } } }
Returns a {@code BigDecimal} whose value is <tt>(this &times; multiplicand)</tt>, with rounding according to the context settings. @param multiplicand value to be multiplied by this {@code BigDecimal}. @param mc the context to use. @return {@code this * multiplicand}, rounded as necessary. @throws ArithmeticException if the result is inexact but the rounding mode is {@code UNNECESSARY}. @since 1.5
BigDecimal::multiply
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) { if (roundingMode < ROUND_UP || roundingMode > ROUND_UNNECESSARY) throw new IllegalArgumentException("Invalid rounding mode"); if (this.intCompact != INFLATED) { if ((divisor.intCompact != INFLATED)) { return divide(this.intCompact, this.scale, divisor.intCompact, divisor.scale, scale, roundingMode); } else { return divide(this.intCompact, this.scale, divisor.intVal, divisor.scale, scale, roundingMode); } } else { if ((divisor.intCompact != INFLATED)) { return divide(this.intVal, this.scale, divisor.intCompact, divisor.scale, scale, roundingMode); } else { return divide(this.intVal, this.scale, divisor.intVal, divisor.scale, scale, roundingMode); } } }
Returns a {@code BigDecimal} whose value is {@code (this / divisor)}, and whose scale is as specified. If rounding must be performed to generate a result with the specified scale, the specified rounding mode is applied. <p>The new {@link #divide(BigDecimal, int, RoundingMode)} method should be used in preference to this legacy method. @param divisor value by which this {@code BigDecimal} is to be divided. @param scale scale of the {@code BigDecimal} quotient to be returned. @param roundingMode rounding mode to apply. @return {@code this / divisor} @throws ArithmeticException if {@code divisor} is zero, {@code roundingMode==ROUND_UNNECESSARY} and the specified scale is insufficient to represent the result of the division exactly. @throws IllegalArgumentException if {@code roundingMode} does not represent a valid rounding mode. @see #ROUND_UP @see #ROUND_DOWN @see #ROUND_CEILING @see #ROUND_FLOOR @see #ROUND_HALF_UP @see #ROUND_HALF_DOWN @see #ROUND_HALF_EVEN @see #ROUND_UNNECESSARY
BigDecimal::divide
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode) { return divide(divisor, scale, roundingMode.oldMode); }
Returns a {@code BigDecimal} whose value is {@code (this / divisor)}, and whose scale is as specified. If rounding must be performed to generate a result with the specified scale, the specified rounding mode is applied. @param divisor value by which this {@code BigDecimal} is to be divided. @param scale scale of the {@code BigDecimal} quotient to be returned. @param roundingMode rounding mode to apply. @return {@code this / divisor} @throws ArithmeticException if {@code divisor} is zero, {@code roundingMode==RoundingMode.UNNECESSARY} and the specified scale is insufficient to represent the result of the division exactly. @since 1.5
BigDecimal::divide
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal divide(BigDecimal divisor, int roundingMode) { return this.divide(divisor, scale, roundingMode); }
Returns a {@code BigDecimal} whose value is {@code (this / divisor)}, and whose scale is {@code this.scale()}. If rounding must be performed to generate a result with the given scale, the specified rounding mode is applied. <p>The new {@link #divide(BigDecimal, RoundingMode)} method should be used in preference to this legacy method. @param divisor value by which this {@code BigDecimal} is to be divided. @param roundingMode rounding mode to apply. @return {@code this / divisor} @throws ArithmeticException if {@code divisor==0}, or {@code roundingMode==ROUND_UNNECESSARY} and {@code this.scale()} is insufficient to represent the result of the division exactly. @throws IllegalArgumentException if {@code roundingMode} does not represent a valid rounding mode. @see #ROUND_UP @see #ROUND_DOWN @see #ROUND_CEILING @see #ROUND_FLOOR @see #ROUND_HALF_UP @see #ROUND_HALF_DOWN @see #ROUND_HALF_EVEN @see #ROUND_UNNECESSARY
BigDecimal::divide
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal divide(BigDecimal divisor, RoundingMode roundingMode) { return this.divide(divisor, scale, roundingMode.oldMode); }
Returns a {@code BigDecimal} whose value is {@code (this / divisor)}, and whose scale is {@code this.scale()}. If rounding must be performed to generate a result with the given scale, the specified rounding mode is applied. @param divisor value by which this {@code BigDecimal} is to be divided. @param roundingMode rounding mode to apply. @return {@code this / divisor} @throws ArithmeticException if {@code divisor==0}, or {@code roundingMode==RoundingMode.UNNECESSARY} and {@code this.scale()} is insufficient to represent the result of the division exactly. @since 1.5
BigDecimal::divide
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal divide(BigDecimal divisor) { /* * Handle zero cases first. */ if (divisor.signum() == 0) { // x/0 if (this.signum() == 0) // 0/0 throw new ArithmeticException("Division undefined"); // NaN throw new ArithmeticException("Division by zero"); } // Calculate preferred scale int preferredScale = saturateLong((long) this.scale - divisor.scale); if (this.signum() == 0) // 0/y return zeroValueOf(preferredScale); else { /* * If the quotient this/divisor has a terminating decimal * expansion, the expansion can have no more than * (a.precision() + ceil(10*b.precision)/3) digits. * Therefore, create a MathContext object with this * precision and do a divide with the UNNECESSARY rounding * mode. */ MathContext mc = new MathContext( (int)Math.min(this.precision() + (long)Math.ceil(10.0*divisor.precision()/3.0), Integer.MAX_VALUE), RoundingMode.UNNECESSARY); BigDecimal quotient; try { quotient = this.divide(divisor, mc); } catch (ArithmeticException e) { throw new ArithmeticException("Non-terminating decimal expansion; " + "no exact representable decimal result."); } int quotientScale = quotient.scale(); // divide(BigDecimal, mc) tries to adjust the quotient to // the desired one by removing trailing zeros; since the // exact divide method does not have an explicit digit // limit, we can add zeros too. if (preferredScale > quotientScale) return quotient.setScale(preferredScale, ROUND_UNNECESSARY); return quotient; } }
Returns a {@code BigDecimal} whose value is {@code (this / divisor)}, and whose preferred scale is {@code (this.scale() - divisor.scale())}; if the exact quotient cannot be represented (because it has a non-terminating decimal expansion) an {@code ArithmeticException} is thrown. @param divisor value by which this {@code BigDecimal} is to be divided. @throws ArithmeticException if the exact quotient does not have a terminating decimal expansion @return {@code this / divisor} @since 1.5 @author Joseph D. Darcy
BigDecimal::divide
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal divide(BigDecimal divisor, MathContext mc) { int mcp = mc.precision; if (mcp == 0) return divide(divisor); BigDecimal dividend = this; long preferredScale = (long)dividend.scale - divisor.scale; // Now calculate the answer. We use the existing // divide-and-round method, but as this rounds to scale we have // to normalize the values here to achieve the desired result. // For x/y we first handle y=0 and x=0, and then normalize x and // y to give x' and y' with the following constraints: // (a) 0.1 <= x' < 1 // (b) x' <= y' < 10*x' // Dividing x'/y' with the required scale set to mc.precision then // will give a result in the range 0.1 to 1 rounded to exactly // the right number of digits (except in the case of a result of // 1.000... which can arise when x=y, or when rounding overflows // The 1.000... case will reduce properly to 1. if (divisor.signum() == 0) { // x/0 if (dividend.signum() == 0) // 0/0 throw new ArithmeticException("Division undefined"); // NaN throw new ArithmeticException("Division by zero"); } if (dividend.signum() == 0) // 0/y return zeroValueOf(saturateLong(preferredScale)); int xscale = dividend.precision(); int yscale = divisor.precision(); if(dividend.intCompact!=INFLATED) { if(divisor.intCompact!=INFLATED) { return divide(dividend.intCompact, xscale, divisor.intCompact, yscale, preferredScale, mc); } else { return divide(dividend.intCompact, xscale, divisor.intVal, yscale, preferredScale, mc); } } else { if(divisor.intCompact!=INFLATED) { return divide(dividend.intVal, xscale, divisor.intCompact, yscale, preferredScale, mc); } else { return divide(dividend.intVal, xscale, divisor.intVal, yscale, preferredScale, mc); } } }
Returns a {@code BigDecimal} whose value is {@code (this / divisor)}, with rounding according to the context settings. @param divisor value by which this {@code BigDecimal} is to be divided. @param mc the context to use. @return {@code this / divisor}, rounded as necessary. @throws ArithmeticException if the result is inexact but the rounding mode is {@code UNNECESSARY} or {@code mc.precision == 0} and the quotient has a non-terminating decimal expansion. @since 1.5
BigDecimal::divide
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal divideToIntegralValue(BigDecimal divisor) { // Calculate preferred scale int preferredScale = saturateLong((long) this.scale - divisor.scale); if (this.compareMagnitude(divisor) < 0) { // much faster when this << divisor return zeroValueOf(preferredScale); } if (this.signum() == 0 && divisor.signum() != 0) return this.setScale(preferredScale, ROUND_UNNECESSARY); // Perform a divide with enough digits to round to a correct // integer value; then remove any fractional digits int maxDigits = (int)Math.min(this.precision() + (long)Math.ceil(10.0*divisor.precision()/3.0) + Math.abs((long)this.scale() - divisor.scale()) + 2, Integer.MAX_VALUE); BigDecimal quotient = this.divide(divisor, new MathContext(maxDigits, RoundingMode.DOWN)); if (quotient.scale > 0) { quotient = quotient.setScale(0, RoundingMode.DOWN); quotient = stripZerosToMatchScale(quotient.intVal, quotient.intCompact, quotient.scale, preferredScale); } if (quotient.scale < preferredScale) { // pad with zeros if necessary quotient = quotient.setScale(preferredScale, ROUND_UNNECESSARY); } return quotient; }
Returns a {@code BigDecimal} whose value is the integer part of the quotient {@code (this / divisor)} rounded down. The preferred scale of the result is {@code (this.scale() - divisor.scale())}. @param divisor value by which this {@code BigDecimal} is to be divided. @return The integer part of {@code this / divisor}. @throws ArithmeticException if {@code divisor==0} @since 1.5
BigDecimal::divideToIntegralValue
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal divideToIntegralValue(BigDecimal divisor, MathContext mc) { if (mc.precision == 0 || // exact result (this.compareMagnitude(divisor) < 0)) // zero result return divideToIntegralValue(divisor); // Calculate preferred scale int preferredScale = saturateLong((long)this.scale - divisor.scale); /* * Perform a normal divide to mc.precision digits. If the * remainder has absolute value less than the divisor, the * integer portion of the quotient fits into mc.precision * digits. Next, remove any fractional digits from the * quotient and adjust the scale to the preferred value. */ BigDecimal result = this.divide(divisor, new MathContext(mc.precision, RoundingMode.DOWN)); if (result.scale() < 0) { /* * Result is an integer. See if quotient represents the * full integer portion of the exact quotient; if it does, * the computed remainder will be less than the divisor. */ BigDecimal product = result.multiply(divisor); // If the quotient is the full integer value, // |dividend-product| < |divisor|. if (this.subtract(product).compareMagnitude(divisor) >= 0) { throw new ArithmeticException("Division impossible"); } } else if (result.scale() > 0) { /* * Integer portion of quotient will fit into precision * digits; recompute quotient to scale 0 to avoid double * rounding and then try to adjust, if necessary. */ result = result.setScale(0, RoundingMode.DOWN); } // else result.scale() == 0; int precisionDiff; if ((preferredScale > result.scale()) && (precisionDiff = mc.precision - result.precision()) > 0) { return result.setScale(result.scale() + Math.min(precisionDiff, preferredScale - result.scale) ); } else { return stripZerosToMatchScale(result.intVal,result.intCompact,result.scale,preferredScale); } }
Returns a {@code BigDecimal} whose value is the integer part of {@code (this / divisor)}. Since the integer part of the exact quotient does not depend on the rounding mode, the rounding mode does not affect the values returned by this method. The preferred scale of the result is {@code (this.scale() - divisor.scale())}. An {@code ArithmeticException} is thrown if the integer part of the exact quotient needs more than {@code mc.precision} digits. @param divisor value by which this {@code BigDecimal} is to be divided. @param mc the context to use. @return The integer part of {@code this / divisor}. @throws ArithmeticException if {@code divisor==0} @throws ArithmeticException if {@code mc.precision} {@literal >} 0 and the result requires a precision of more than {@code mc.precision} digits. @since 1.5 @author Joseph D. Darcy
BigDecimal::divideToIntegralValue
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal remainder(BigDecimal divisor) { BigDecimal divrem[] = this.divideAndRemainder(divisor); return divrem[1]; }
Returns a {@code BigDecimal} whose value is {@code (this % divisor)}. <p>The remainder is given by {@code this.subtract(this.divideToIntegralValue(divisor).multiply(divisor))}. Note that this is not the modulo operation (the result can be negative). @param divisor value by which this {@code BigDecimal} is to be divided. @return {@code this % divisor}. @throws ArithmeticException if {@code divisor==0} @since 1.5
BigDecimal::remainder
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal remainder(BigDecimal divisor, MathContext mc) { BigDecimal divrem[] = this.divideAndRemainder(divisor, mc); return divrem[1]; }
Returns a {@code BigDecimal} whose value is {@code (this % divisor)}, with rounding according to the context settings. The {@code MathContext} settings affect the implicit divide used to compute the remainder. The remainder computation itself is by definition exact. Therefore, the remainder may contain more than {@code mc.getPrecision()} digits. <p>The remainder is given by {@code this.subtract(this.divideToIntegralValue(divisor, mc).multiply(divisor))}. Note that this is not the modulo operation (the result can be negative). @param divisor value by which this {@code BigDecimal} is to be divided. @param mc the context to use. @return {@code this % divisor}, rounded as necessary. @throws ArithmeticException if {@code divisor==0} @throws ArithmeticException if the result is inexact but the rounding mode is {@code UNNECESSARY}, or {@code mc.precision} {@literal >} 0 and the result of {@code this.divideToIntgralValue(divisor)} would require a precision of more than {@code mc.precision} digits. @see #divideToIntegralValue(java.math.BigDecimal, java.math.MathContext) @since 1.5
BigDecimal::remainder
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal[] divideAndRemainder(BigDecimal divisor) { // we use the identity x = i * y + r to determine r BigDecimal[] result = new BigDecimal[2]; result[0] = this.divideToIntegralValue(divisor); result[1] = this.subtract(result[0].multiply(divisor)); return result; }
Returns a two-element {@code BigDecimal} array containing the result of {@code divideToIntegralValue} followed by the result of {@code remainder} on the two operands. <p>Note that if both the integer quotient and remainder are needed, this method is faster than using the {@code divideToIntegralValue} and {@code remainder} methods separately because the division need only be carried out once. @param divisor value by which this {@code BigDecimal} is to be divided, and the remainder computed. @return a two element {@code BigDecimal} array: the quotient (the result of {@code divideToIntegralValue}) is the initial element and the remainder is the final element. @throws ArithmeticException if {@code divisor==0} @see #divideToIntegralValue(java.math.BigDecimal, java.math.MathContext) @see #remainder(java.math.BigDecimal, java.math.MathContext) @since 1.5
BigDecimal::divideAndRemainder
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal[] divideAndRemainder(BigDecimal divisor, MathContext mc) { if (mc.precision == 0) return divideAndRemainder(divisor); BigDecimal[] result = new BigDecimal[2]; BigDecimal lhs = this; result[0] = lhs.divideToIntegralValue(divisor, mc); result[1] = lhs.subtract(result[0].multiply(divisor)); return result; }
Returns a two-element {@code BigDecimal} array containing the result of {@code divideToIntegralValue} followed by the result of {@code remainder} on the two operands calculated with rounding according to the context settings. <p>Note that if both the integer quotient and remainder are needed, this method is faster than using the {@code divideToIntegralValue} and {@code remainder} methods separately because the division need only be carried out once. @param divisor value by which this {@code BigDecimal} is to be divided, and the remainder computed. @param mc the context to use. @return a two element {@code BigDecimal} array: the quotient (the result of {@code divideToIntegralValue}) is the initial element and the remainder is the final element. @throws ArithmeticException if {@code divisor==0} @throws ArithmeticException if the result is inexact but the rounding mode is {@code UNNECESSARY}, or {@code mc.precision} {@literal >} 0 and the result of {@code this.divideToIntgralValue(divisor)} would require a precision of more than {@code mc.precision} digits. @see #divideToIntegralValue(java.math.BigDecimal, java.math.MathContext) @see #remainder(java.math.BigDecimal, java.math.MathContext) @since 1.5
BigDecimal::divideAndRemainder
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal pow(int n) { if (n < 0 || n > 999999999) throw new ArithmeticException("Invalid operation"); // No need to calculate pow(n) if result will over/underflow. // Don't attempt to support "supernormal" numbers. int newScale = checkScale((long)scale * n); return new BigDecimal(this.inflated().pow(n), newScale); }
Returns a {@code BigDecimal} whose value is <tt>(this<sup>n</sup>)</tt>, The power is computed exactly, to unlimited precision. <p>The parameter {@code n} must be in the range 0 through 999999999, inclusive. {@code ZERO.pow(0)} returns {@link #ONE}. Note that future releases may expand the allowable exponent range of this method. @param n power to raise this {@code BigDecimal} to. @return <tt>this<sup>n</sup></tt> @throws ArithmeticException if {@code n} is out of range. @since 1.5
BigDecimal::pow
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal pow(int n, MathContext mc) { if (mc.precision == 0) return pow(n); if (n < -999999999 || n > 999999999) throw new ArithmeticException("Invalid operation"); if (n == 0) return ONE; // x**0 == 1 in X3.274 BigDecimal lhs = this; MathContext workmc = mc; // working settings int mag = Math.abs(n); // magnitude of n if (mc.precision > 0) { int elength = longDigitLength(mag); // length of n in digits if (elength > mc.precision) // X3.274 rule throw new ArithmeticException("Invalid operation"); workmc = new MathContext(mc.precision + elength + 1, mc.roundingMode); } // ready to carry out power calculation... BigDecimal acc = ONE; // accumulator boolean seenbit = false; // set once we've seen a 1-bit for (int i=1;;i++) { // for each bit [top bit ignored] mag += mag; // shift left 1 bit if (mag < 0) { // top bit is set seenbit = true; // OK, we're off acc = acc.multiply(lhs, workmc); // acc=acc*x } if (i == 31) break; // that was the last bit if (seenbit) acc=acc.multiply(acc, workmc); // acc=acc*acc [square] // else (!seenbit) no point in squaring ONE } // if negative n, calculate the reciprocal using working precision if (n < 0) // [hence mc.precision>0] acc=ONE.divide(acc, workmc); // round to final precision and strip zeros return doRound(acc, mc); }
Returns a {@code BigDecimal} whose value is <tt>(this<sup>n</sup>)</tt>. The current implementation uses the core algorithm defined in ANSI standard X3.274-1996 with rounding according to the context settings. In general, the returned numerical value is within two ulps of the exact numerical value for the chosen precision. Note that future releases may use a different algorithm with a decreased allowable error bound and increased allowable exponent range. <p>The X3.274-1996 algorithm is: <ul> <li> An {@code ArithmeticException} exception is thrown if <ul> <li>{@code abs(n) > 999999999} <li>{@code mc.precision == 0} and {@code n < 0} <li>{@code mc.precision > 0} and {@code n} has more than {@code mc.precision} decimal digits </ul> <li> if {@code n} is zero, {@link #ONE} is returned even if {@code this} is zero, otherwise <ul> <li> if {@code n} is positive, the result is calculated via the repeated squaring technique into a single accumulator. The individual multiplications with the accumulator use the same math context settings as in {@code mc} except for a precision increased to {@code mc.precision + elength + 1} where {@code elength} is the number of decimal digits in {@code n}. <li> if {@code n} is negative, the result is calculated as if {@code n} were positive; this value is then divided into one using the working precision specified above. <li> The final value from either the positive or negative case is then rounded to the destination precision. </ul> </ul> @param n power to raise this {@code BigDecimal} to. @param mc the context to use. @return <tt>this<sup>n</sup></tt> using the ANSI standard X3.274-1996 algorithm @throws ArithmeticException if the result is inexact but the rounding mode is {@code UNNECESSARY}, or {@code n} is out of range. @since 1.5
BigDecimal::pow
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal abs() { return (signum() < 0 ? negate() : this); }
Returns a {@code BigDecimal} whose value is the absolute value of this {@code BigDecimal}, and whose scale is {@code this.scale()}. @return {@code abs(this)}
BigDecimal::abs
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal abs(MathContext mc) { return (signum() < 0 ? negate(mc) : plus(mc)); }
Returns a {@code BigDecimal} whose value is the absolute value of this {@code BigDecimal}, with rounding according to the context settings. @param mc the context to use. @return {@code abs(this)}, rounded as necessary. @throws ArithmeticException if the result is inexact but the rounding mode is {@code UNNECESSARY}. @since 1.5
BigDecimal::abs
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal negate() { if (intCompact == INFLATED) { return new BigDecimal(intVal.negate(), INFLATED, scale, precision); } else { return valueOf(-intCompact, scale, precision); } }
Returns a {@code BigDecimal} whose value is {@code (-this)}, and whose scale is {@code this.scale()}. @return {@code -this}.
BigDecimal::negate
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal negate(MathContext mc) { return negate().plus(mc); }
Returns a {@code BigDecimal} whose value is {@code (-this)}, with rounding according to the context settings. @param mc the context to use. @return {@code -this}, rounded as necessary. @throws ArithmeticException if the result is inexact but the rounding mode is {@code UNNECESSARY}. @since 1.5
BigDecimal::negate
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal plus() { return this; }
Returns a {@code BigDecimal} whose value is {@code (+this)}, and whose scale is {@code this.scale()}. <p>This method, which simply returns this {@code BigDecimal} is included for symmetry with the unary minus method {@link #negate()}. @return {@code this}. @see #negate() @since 1.5
BigDecimal::plus
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal plus(MathContext mc) { if (mc.precision == 0) // no rounding please return this; return doRound(this, mc); }
Returns a {@code BigDecimal} whose value is {@code (+this)}, with rounding according to the context settings. <p>The effect of this method is identical to that of the {@link #round(MathContext)} method. @param mc the context to use. @return {@code this}, rounded as necessary. A zero result will have a scale of 0. @throws ArithmeticException if the result is inexact but the rounding mode is {@code UNNECESSARY}. @see #round(MathContext) @since 1.5
BigDecimal::plus
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public int signum() { return (intCompact != INFLATED)? Long.signum(intCompact): intVal.signum(); }
Returns the signum function of this {@code BigDecimal}. @return -1, 0, or 1 as the value of this {@code BigDecimal} is negative, zero, or positive.
BigDecimal::signum
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public int scale() { return scale; }
Returns the <i>scale</i> of this {@code BigDecimal}. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. For example, a scale of {@code -3} means the unscaled value is multiplied by 1000. @return the scale of this {@code BigDecimal}.
BigDecimal::scale
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public int precision() { int result = precision; if (result == 0) { long s = intCompact; if (s != INFLATED) result = longDigitLength(s); else result = bigDigitLength(intVal); precision = result; } return result; }
Returns the <i>precision</i> of this {@code BigDecimal}. (The precision is the number of digits in the unscaled value.) <p>The precision of a zero value is 1. @return the precision of this {@code BigDecimal}. @since 1.5
BigDecimal::precision
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigInteger unscaledValue() { return this.inflated(); }
Returns a {@code BigInteger} whose value is the <i>unscaled value</i> of this {@code BigDecimal}. (Computes <tt>(this * 10<sup>this.scale()</sup>)</tt>.) @return the unscaled value of this {@code BigDecimal}. @since 1.2
BigDecimal::unscaledValue
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal round(MathContext mc) { return plus(mc); }
Returns a {@code BigDecimal} rounded according to the {@code MathContext} settings. If the precision setting is 0 then no rounding takes place. <p>The effect of this method is identical to that of the {@link #plus(MathContext)} method. @param mc the context to use. @return a {@code BigDecimal} rounded according to the {@code MathContext} settings. @throws ArithmeticException if the rounding mode is {@code UNNECESSARY} and the {@code BigDecimal} operation would require rounding. @see #plus(MathContext) @since 1.5
BigDecimal::round
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal setScale(int newScale, RoundingMode roundingMode) { return setScale(newScale, roundingMode.oldMode); }
Returns a {@code BigDecimal} whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this {@code BigDecimal}'s unscaled value by the appropriate power of ten to maintain its overall value. If the scale is reduced by the operation, the unscaled value must be divided (rather than multiplied), and the value may be changed; in this case, the specified rounding mode is applied to the division. <p>Note that since BigDecimal objects are immutable, calls of this method do <i>not</i> result in the original object being modified, contrary to the usual convention of having methods named <tt>set<i>X</i></tt> mutate field <i>{@code X}</i>. Instead, {@code setScale} returns an object with the proper scale; the returned object may or may not be newly allocated. @param newScale scale of the {@code BigDecimal} value to be returned. @param roundingMode The rounding mode to apply. @return a {@code BigDecimal} whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this {@code BigDecimal}'s unscaled value by the appropriate power of ten to maintain its overall value. @throws ArithmeticException if {@code roundingMode==UNNECESSARY} and the specified scaling operation would require rounding. @see RoundingMode @since 1.5
BigDecimal::setScale
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal setScale(int newScale, int roundingMode) { if (roundingMode < ROUND_UP || roundingMode > ROUND_UNNECESSARY) throw new IllegalArgumentException("Invalid rounding mode"); int oldScale = this.scale; if (newScale == oldScale) // easy case return this; if (this.signum() == 0) // zero can have any scale return zeroValueOf(newScale); if(this.intCompact!=INFLATED) { long rs = this.intCompact; if (newScale > oldScale) { int raise = checkScale((long) newScale - oldScale); if ((rs = longMultiplyPowerTen(rs, raise)) != INFLATED) { return valueOf(rs,newScale); } BigInteger rb = bigMultiplyPowerTen(raise); return new BigDecimal(rb, INFLATED, newScale, (precision > 0) ? precision + raise : 0); } else { // newScale < oldScale -- drop some digits // Can't predict the precision due to the effect of rounding. int drop = checkScale((long) oldScale - newScale); if (drop < LONG_TEN_POWERS_TABLE.length) { return divideAndRound(rs, LONG_TEN_POWERS_TABLE[drop], newScale, roundingMode, newScale); } else { return divideAndRound(this.inflated(), bigTenToThe(drop), newScale, roundingMode, newScale); } } } else { if (newScale > oldScale) { int raise = checkScale((long) newScale - oldScale); BigInteger rb = bigMultiplyPowerTen(this.intVal,raise); return new BigDecimal(rb, INFLATED, newScale, (precision > 0) ? precision + raise : 0); } else { // newScale < oldScale -- drop some digits // Can't predict the precision due to the effect of rounding. int drop = checkScale((long) oldScale - newScale); if (drop < LONG_TEN_POWERS_TABLE.length) return divideAndRound(this.intVal, LONG_TEN_POWERS_TABLE[drop], newScale, roundingMode, newScale); else return divideAndRound(this.intVal, bigTenToThe(drop), newScale, roundingMode, newScale); } } }
Returns a {@code BigDecimal} whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this {@code BigDecimal}'s unscaled value by the appropriate power of ten to maintain its overall value. If the scale is reduced by the operation, the unscaled value must be divided (rather than multiplied), and the value may be changed; in this case, the specified rounding mode is applied to the division. <p>Note that since BigDecimal objects are immutable, calls of this method do <i>not</i> result in the original object being modified, contrary to the usual convention of having methods named <tt>set<i>X</i></tt> mutate field <i>{@code X}</i>. Instead, {@code setScale} returns an object with the proper scale; the returned object may or may not be newly allocated. <p>The new {@link #setScale(int, RoundingMode)} method should be used in preference to this legacy method. @param newScale scale of the {@code BigDecimal} value to be returned. @param roundingMode The rounding mode to apply. @return a {@code BigDecimal} whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this {@code BigDecimal}'s unscaled value by the appropriate power of ten to maintain its overall value. @throws ArithmeticException if {@code roundingMode==ROUND_UNNECESSARY} and the specified scaling operation would require rounding. @throws IllegalArgumentException if {@code roundingMode} does not represent a valid rounding mode. @see #ROUND_UP @see #ROUND_DOWN @see #ROUND_CEILING @see #ROUND_FLOOR @see #ROUND_HALF_UP @see #ROUND_HALF_DOWN @see #ROUND_HALF_EVEN @see #ROUND_UNNECESSARY
BigDecimal::setScale
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal setScale(int newScale) { return setScale(newScale, ROUND_UNNECESSARY); }
Returns a {@code BigDecimal} whose scale is the specified value, and whose value is numerically equal to this {@code BigDecimal}'s. Throws an {@code ArithmeticException} if this is not possible. <p>This call is typically used to increase the scale, in which case it is guaranteed that there exists a {@code BigDecimal} of the specified scale and the correct value. The call can also be used to reduce the scale if the caller knows that the {@code BigDecimal} has sufficiently many zeros at the end of its fractional part (i.e., factors of ten in its integer value) to allow for the rescaling without changing its value. <p>This method returns the same result as the two-argument versions of {@code setScale}, but saves the caller the trouble of specifying a rounding mode in cases where it is irrelevant. <p>Note that since {@code BigDecimal} objects are immutable, calls of this method do <i>not</i> result in the original object being modified, contrary to the usual convention of having methods named <tt>set<i>X</i></tt> mutate field <i>{@code X}</i>. Instead, {@code setScale} returns an object with the proper scale; the returned object may or may not be newly allocated. @param newScale scale of the {@code BigDecimal} value to be returned. @return a {@code BigDecimal} whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this {@code BigDecimal}'s unscaled value by the appropriate power of ten to maintain its overall value. @throws ArithmeticException if the specified scaling operation would require rounding. @see #setScale(int, int) @see #setScale(int, RoundingMode)
BigDecimal::setScale
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal movePointLeft(int n) { // Cannot use movePointRight(-n) in case of n==Integer.MIN_VALUE int newScale = checkScale((long)scale + n); BigDecimal num = new BigDecimal(intVal, intCompact, newScale, 0); return num.scale < 0 ? num.setScale(0, ROUND_UNNECESSARY) : num; }
Returns a {@code BigDecimal} which is equivalent to this one with the decimal point moved {@code n} places to the left. If {@code n} is non-negative, the call merely adds {@code n} to the scale. If {@code n} is negative, the call is equivalent to {@code movePointRight(-n)}. The {@code BigDecimal} returned by this call has value <tt>(this &times; 10<sup>-n</sup>)</tt> and scale {@code max(this.scale()+n, 0)}. @param n number of places to move the decimal point to the left. @return a {@code BigDecimal} which is equivalent to this one with the decimal point moved {@code n} places to the left. @throws ArithmeticException if scale overflows.
BigDecimal::movePointLeft
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal movePointRight(int n) { // Cannot use movePointLeft(-n) in case of n==Integer.MIN_VALUE int newScale = checkScale((long)scale - n); BigDecimal num = new BigDecimal(intVal, intCompact, newScale, 0); return num.scale < 0 ? num.setScale(0, ROUND_UNNECESSARY) : num; }
Returns a {@code BigDecimal} which is equivalent to this one with the decimal point moved {@code n} places to the right. If {@code n} is non-negative, the call merely subtracts {@code n} from the scale. If {@code n} is negative, the call is equivalent to {@code movePointLeft(-n)}. The {@code BigDecimal} returned by this call has value <tt>(this &times; 10<sup>n</sup>)</tt> and scale {@code max(this.scale()-n, 0)}. @param n number of places to move the decimal point to the right. @return a {@code BigDecimal} which is equivalent to this one with the decimal point moved {@code n} places to the right. @throws ArithmeticException if scale overflows.
BigDecimal::movePointRight
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal scaleByPowerOfTen(int n) { return new BigDecimal(intVal, intCompact, checkScale((long)scale - n), precision); }
Returns a BigDecimal whose numerical value is equal to ({@code this} * 10<sup>n</sup>). The scale of the result is {@code (this.scale() - n)}. @param n the exponent power of ten to scale by @return a BigDecimal whose numerical value is equal to ({@code this} * 10<sup>n</sup>) @throws ArithmeticException if the scale would be outside the range of a 32-bit integer. @since 1.5
BigDecimal::scaleByPowerOfTen
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal stripTrailingZeros() { if (intCompact == 0 || (intVal != null && intVal.signum() == 0)) { return BigDecimal.ZERO; } else if (intCompact != INFLATED) { return createAndStripZerosToMatchScale(intCompact, scale, Long.MIN_VALUE); } else { return createAndStripZerosToMatchScale(intVal, scale, Long.MIN_VALUE); } }
Returns a {@code BigDecimal} which is numerically equal to this one but with any trailing zeros removed from the representation. For example, stripping the trailing zeros from the {@code BigDecimal} value {@code 600.0}, which has [{@code BigInteger}, {@code scale}] components equals to [6000, 1], yields {@code 6E2} with [{@code BigInteger}, {@code scale}] components equals to [6, -2]. If this BigDecimal is numerically equal to zero, then {@code BigDecimal.ZERO} is returned. @return a numerically equal {@code BigDecimal} with any trailing zeros removed. @since 1.5
BigDecimal::stripTrailingZeros
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public int compareTo(BigDecimal val) { // Quick path for equal scale and non-inflated case. if (scale == val.scale) { long xs = intCompact; long ys = val.intCompact; if (xs != INFLATED && ys != INFLATED) return xs != ys ? ((xs > ys) ? 1 : -1) : 0; } int xsign = this.signum(); int ysign = val.signum(); if (xsign != ysign) return (xsign > ysign) ? 1 : -1; if (xsign == 0) return 0; int cmp = compareMagnitude(val); return (xsign > 0) ? cmp : -cmp; }
Compares this {@code BigDecimal} with the specified {@code BigDecimal}. Two {@code BigDecimal} objects that are equal in value but have a different scale (like 2.0 and 2.00) are considered equal by this method. This method is provided in preference to individual methods for each of the six boolean comparison operators ({@literal <}, ==, {@literal >}, {@literal >=}, !=, {@literal <=}). The suggested idiom for performing these comparisons is: {@code (x.compareTo(y)} &lt;<i>op</i>&gt; {@code 0)}, where &lt;<i>op</i>&gt; is one of the six comparison operators. @param val {@code BigDecimal} to which this {@code BigDecimal} is to be compared. @return -1, 0, or 1 as this {@code BigDecimal} is numerically less than, equal to, or greater than {@code val}.
BigDecimal::compareTo
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
private int compareMagnitude(BigDecimal val) { // Match scales, avoid unnecessary inflation long ys = val.intCompact; long xs = this.intCompact; if (xs == 0) return (ys == 0) ? 0 : -1; if (ys == 0) return 1; long sdiff = (long)this.scale - val.scale; if (sdiff != 0) { // Avoid matching scales if the (adjusted) exponents differ long xae = (long)this.precision() - this.scale; // [-1] long yae = (long)val.precision() - val.scale; // [-1] if (xae < yae) return -1; if (xae > yae) return 1; BigInteger rb = null; if (sdiff < 0) { // The cases sdiff <= Integer.MIN_VALUE intentionally fall through. if ( sdiff > Integer.MIN_VALUE && (xs == INFLATED || (xs = longMultiplyPowerTen(xs, (int)-sdiff)) == INFLATED) && ys == INFLATED) { rb = bigMultiplyPowerTen((int)-sdiff); return rb.compareMagnitude(val.intVal); } } else { // sdiff > 0 // The cases sdiff > Integer.MAX_VALUE intentionally fall through. if ( sdiff <= Integer.MAX_VALUE && (ys == INFLATED || (ys = longMultiplyPowerTen(ys, (int)sdiff)) == INFLATED) && xs == INFLATED) { rb = val.bigMultiplyPowerTen((int)sdiff); return this.intVal.compareMagnitude(rb); } } } if (xs != INFLATED) return (ys != INFLATED) ? longCompareMagnitude(xs, ys) : -1; else if (ys != INFLATED) return 1; else return this.intVal.compareMagnitude(val.intVal); }
Version of compareTo that ignores sign.
BigDecimal::compareMagnitude
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal min(BigDecimal val) { return (compareTo(val) <= 0 ? this : val); }
Returns the minimum of this {@code BigDecimal} and {@code val}. @param val value with which the minimum is to be computed. @return the {@code BigDecimal} whose value is the lesser of this {@code BigDecimal} and {@code val}. If they are equal, as defined by the {@link #compareTo(BigDecimal) compareTo} method, {@code this} is returned. @see #compareTo(java.math.BigDecimal)
BigDecimal::min
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal max(BigDecimal val) { return (compareTo(val) >= 0 ? this : val); }
Returns the maximum of this {@code BigDecimal} and {@code val}. @param val value with which the maximum is to be computed. @return the {@code BigDecimal} whose value is the greater of this {@code BigDecimal} and {@code val}. If they are equal, as defined by the {@link #compareTo(BigDecimal) compareTo} method, {@code this} is returned. @see #compareTo(java.math.BigDecimal)
BigDecimal::max
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public String toEngineeringString() { return layoutChars(false); }
Returns a string representation of this {@code BigDecimal}, using engineering notation if an exponent is needed. <p>Returns a string that represents the {@code BigDecimal} as described in the {@link #toString()} method, except that if exponential notation is used, the power of ten is adjusted to be a multiple of three (engineering notation) such that the integer part of nonzero values will be in the range 1 through 999. If exponential notation is used for zero values, a decimal point and one or two fractional zero digits are used so that the scale of the zero value is preserved. Note that unlike the output of {@link #toString()}, the output of this method is <em>not</em> guaranteed to recover the same [integer, scale] pair of this {@code BigDecimal} if the output string is converting back to a {@code BigDecimal} using the {@linkplain #BigDecimal(String) string constructor}. The result of this method meets the weaker constraint of always producing a numerically equal result from applying the string constructor to the method's output. @return string representation of this {@code BigDecimal}, using engineering notation if an exponent is needed. @since 1.5
BigDecimal::toEngineeringString
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public String toPlainString() { if(scale==0) { if(intCompact!=INFLATED) { return Long.toString(intCompact); } else { return intVal.toString(); } } if(this.scale<0) { // No decimal point if(signum()==0) { return "0"; } int tailingZeros = checkScaleNonZero((-(long)scale)); StringBuilder buf; if(intCompact!=INFLATED) { buf = new StringBuilder(20+tailingZeros); buf.append(intCompact); } else { String str = intVal.toString(); buf = new StringBuilder(str.length()+tailingZeros); buf.append(str); } for (int i = 0; i < tailingZeros; i++) buf.append('0'); return buf.toString(); } String str ; if(intCompact!=INFLATED) { str = Long.toString(Math.abs(intCompact)); } else { str = intVal.abs().toString(); } return getValueString(signum(), str, scale); }
Returns a string representation of this {@code BigDecimal} without an exponent field. For values with a positive scale, the number of digits to the right of the decimal point is used to indicate scale. For values with a zero or negative scale, the resulting string is generated as if the value were converted to a numerically equal value with zero scale and as if all the trailing zeros of the zero scale value were present in the result. The entire string is prefixed by a minus sign character '-' (<tt>'&#92;u002D'</tt>) if the unscaled value is less than zero. No sign character is prefixed if the unscaled value is zero or positive. Note that if the result of this method is passed to the {@linkplain #BigDecimal(String) string constructor}, only the numerical value of this {@code BigDecimal} will necessarily be recovered; the representation of the new {@code BigDecimal} may have a different scale. In particular, if this {@code BigDecimal} has a negative scale, the string resulting from this method will have a scale of zero when processed by the string constructor. (This method behaves analogously to the {@code toString} method in 1.4 and earlier releases.) @return a string representation of this {@code BigDecimal} without an exponent field. @since 1.5 @see #toString() @see #toEngineeringString()
BigDecimal::toPlainString
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
private String getValueString(int signum, String intString, int scale) { /* Insert decimal point */ StringBuilder buf; int insertionPoint = intString.length() - scale; if (insertionPoint == 0) { /* Point goes right before intVal */ return (signum<0 ? "-0." : "0.") + intString; } else if (insertionPoint > 0) { /* Point goes inside intVal */ buf = new StringBuilder(intString); buf.insert(insertionPoint, '.'); if (signum < 0) buf.insert(0, '-'); } else { /* We must insert zeros between point and intVal */ buf = new StringBuilder(3-insertionPoint + intString.length()); buf.append(signum<0 ? "-0." : "0."); for (int i=0; i<-insertionPoint; i++) buf.append('0'); buf.append(intString); } return buf.toString(); }
Returns a string representation of this {@code BigDecimal} without an exponent field. For values with a positive scale, the number of digits to the right of the decimal point is used to indicate scale. For values with a zero or negative scale, the resulting string is generated as if the value were converted to a numerically equal value with zero scale and as if all the trailing zeros of the zero scale value were present in the result. The entire string is prefixed by a minus sign character '-' (<tt>'&#92;u002D'</tt>) if the unscaled value is less than zero. No sign character is prefixed if the unscaled value is zero or positive. Note that if the result of this method is passed to the {@linkplain #BigDecimal(String) string constructor}, only the numerical value of this {@code BigDecimal} will necessarily be recovered; the representation of the new {@code BigDecimal} may have a different scale. In particular, if this {@code BigDecimal} has a negative scale, the string resulting from this method will have a scale of zero when processed by the string constructor. (This method behaves analogously to the {@code toString} method in 1.4 and earlier releases.) @return a string representation of this {@code BigDecimal} without an exponent field. @since 1.5 @see #toString() @see #toEngineeringString() public String toPlainString() { if(scale==0) { if(intCompact!=INFLATED) { return Long.toString(intCompact); } else { return intVal.toString(); } } if(this.scale<0) { // No decimal point if(signum()==0) { return "0"; } int tailingZeros = checkScaleNonZero((-(long)scale)); StringBuilder buf; if(intCompact!=INFLATED) { buf = new StringBuilder(20+tailingZeros); buf.append(intCompact); } else { String str = intVal.toString(); buf = new StringBuilder(str.length()+tailingZeros); buf.append(str); } for (int i = 0; i < tailingZeros; i++) buf.append('0'); return buf.toString(); } String str ; if(intCompact!=INFLATED) { str = Long.toString(Math.abs(intCompact)); } else { str = intVal.abs().toString(); } return getValueString(signum(), str, scale); } /* Returns a digit.digit string
BigDecimal::getValueString
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigInteger toBigInteger() { // force to an integer, quietly return this.setScale(0, ROUND_DOWN).inflated(); }
Converts this {@code BigDecimal} to a {@code BigInteger}. This conversion is analogous to the <i>narrowing primitive conversion</i> from {@code double} to {@code long} as defined in section 5.1.3 of <cite>The Java&trade; Language Specification</cite>: any fractional part of this {@code BigDecimal} will be discarded. Note that this conversion can lose information about the precision of the {@code BigDecimal} value. <p> To have an exception thrown if the conversion is inexact (in other words if a nonzero fractional part is discarded), use the {@link #toBigIntegerExact()} method. @return this {@code BigDecimal} converted to a {@code BigInteger}.
BigDecimal::toBigInteger
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigInteger toBigIntegerExact() { // round to an integer, with Exception if decimal part non-0 return this.setScale(0, ROUND_UNNECESSARY).inflated(); }
Converts this {@code BigDecimal} to a {@code BigInteger}, checking for lost information. An exception is thrown if this {@code BigDecimal} has a nonzero fractional part. @return this {@code BigDecimal} converted to a {@code BigInteger}. @throws ArithmeticException if {@code this} has a nonzero fractional part. @since 1.5
BigDecimal::toBigIntegerExact
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public long longValue(){ return (intCompact != INFLATED && scale == 0) ? intCompact: toBigInteger().longValue(); }
Converts this {@code BigDecimal} to a {@code long}. This conversion is analogous to the <i>narrowing primitive conversion</i> from {@code double} to {@code short} as defined in section 5.1.3 of <cite>The Java&trade; Language Specification</cite>: any fractional part of this {@code BigDecimal} will be discarded, and if the resulting "{@code BigInteger}" is too big to fit in a {@code long}, only the low-order 64 bits are returned. Note that this conversion can lose information about the overall magnitude and precision of this {@code BigDecimal} value as well as return a result with the opposite sign. @return this {@code BigDecimal} converted to a {@code long}.
BigDecimal::longValue
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public long longValueExact() { if (intCompact != INFLATED && scale == 0) return intCompact; // If more than 19 digits in integer part it cannot possibly fit if ((precision() - scale) > 19) // [OK for negative scale too] throw new java.lang.ArithmeticException("Overflow"); // Fastpath zero and < 1.0 numbers (the latter can be very slow // to round if very small) if (this.signum() == 0) return 0; if ((this.precision() - this.scale) <= 0) throw new ArithmeticException("Rounding necessary"); // round to an integer, with Exception if decimal part non-0 BigDecimal num = this.setScale(0, ROUND_UNNECESSARY); if (num.precision() >= 19) // need to check carefully LongOverflow.check(num); return num.inflated().longValue(); }
Converts this {@code BigDecimal} to a {@code long}, checking for lost information. If this {@code BigDecimal} has a nonzero fractional part or is out of the possible range for a {@code long} result then an {@code ArithmeticException} is thrown. @return this {@code BigDecimal} converted to a {@code long}. @throws ArithmeticException if {@code this} has a nonzero fractional part, or will not fit in a {@code long}. @since 1.5
BigDecimal::longValueExact
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public int intValue() { return (intCompact != INFLATED && scale == 0) ? (int)intCompact : toBigInteger().intValue(); }
Converts this {@code BigDecimal} to an {@code int}. This conversion is analogous to the <i>narrowing primitive conversion</i> from {@code double} to {@code short} as defined in section 5.1.3 of <cite>The Java&trade; Language Specification</cite>: any fractional part of this {@code BigDecimal} will be discarded, and if the resulting "{@code BigInteger}" is too big to fit in an {@code int}, only the low-order 32 bits are returned. Note that this conversion can lose information about the overall magnitude and precision of this {@code BigDecimal} value as well as return a result with the opposite sign. @return this {@code BigDecimal} converted to an {@code int}.
LongOverflow::intValue
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public int intValueExact() { long num; num = this.longValueExact(); // will check decimal part if ((int)num != num) throw new java.lang.ArithmeticException("Overflow"); return (int)num; }
Converts this {@code BigDecimal} to an {@code int}, checking for lost information. If this {@code BigDecimal} has a nonzero fractional part or is out of the possible range for an {@code int} result then an {@code ArithmeticException} is thrown. @return this {@code BigDecimal} converted to an {@code int}. @throws ArithmeticException if {@code this} has a nonzero fractional part, or will not fit in an {@code int}. @since 1.5
LongOverflow::intValueExact
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public short shortValueExact() { long num; num = this.longValueExact(); // will check decimal part if ((short)num != num) throw new java.lang.ArithmeticException("Overflow"); return (short)num; }
Converts this {@code BigDecimal} to a {@code short}, checking for lost information. If this {@code BigDecimal} has a nonzero fractional part or is out of the possible range for a {@code short} result then an {@code ArithmeticException} is thrown. @return this {@code BigDecimal} converted to a {@code short}. @throws ArithmeticException if {@code this} has a nonzero fractional part, or will not fit in a {@code short}. @since 1.5
LongOverflow::shortValueExact
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public byte byteValueExact() { long num; num = this.longValueExact(); // will check decimal part if ((byte)num != num) throw new java.lang.ArithmeticException("Overflow"); return (byte)num; }
Converts this {@code BigDecimal} to a {@code byte}, checking for lost information. If this {@code BigDecimal} has a nonzero fractional part or is out of the possible range for a {@code byte} result then an {@code ArithmeticException} is thrown. @return this {@code BigDecimal} converted to a {@code byte}. @throws ArithmeticException if {@code this} has a nonzero fractional part, or will not fit in a {@code byte}. @since 1.5
LongOverflow::byteValueExact
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public float floatValue(){ if(intCompact != INFLATED) { if (scale == 0) { return (float)intCompact; } else { /* * If both intCompact and the scale can be exactly * represented as float values, perform a single float * multiply or divide to compute the (properly * rounded) result. */ if (Math.abs(intCompact) < 1L<<22 ) { // Don't have too guard against // Math.abs(MIN_VALUE) because of outer check // against INFLATED. if (scale > 0 && scale < float10pow.length) { return (float)intCompact / float10pow[scale]; } else if (scale < 0 && scale > -float10pow.length) { return (float)intCompact * float10pow[-scale]; } } } } // Somewhat inefficient, but guaranteed to work. return Float.parseFloat(this.toString()); }
Converts this {@code BigDecimal} to a {@code float}. This conversion is similar to the <i>narrowing primitive conversion</i> from {@code double} to {@code float} as defined in section 5.1.3 of <cite>The Java&trade; Language Specification</cite>: if this {@code BigDecimal} has too great a magnitude to represent as a {@code float}, it will be converted to {@link Float#NEGATIVE_INFINITY} or {@link Float#POSITIVE_INFINITY} as appropriate. Note that even when the return value is finite, this conversion can lose information about the precision of the {@code BigDecimal} value. @return this {@code BigDecimal} converted to a {@code float}.
LongOverflow::floatValue
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public double doubleValue(){ if(intCompact != INFLATED) { if (scale == 0) { return (double)intCompact; } else { /* * If both intCompact and the scale can be exactly * represented as double values, perform a single * double multiply or divide to compute the (properly * rounded) result. */ if (Math.abs(intCompact) < 1L<<52 ) { // Don't have too guard against // Math.abs(MIN_VALUE) because of outer check // against INFLATED. if (scale > 0 && scale < double10pow.length) { return (double)intCompact / double10pow[scale]; } else if (scale < 0 && scale > -double10pow.length) { return (double)intCompact * double10pow[-scale]; } } } } // Somewhat inefficient, but guaranteed to work. return Double.parseDouble(this.toString()); }
Converts this {@code BigDecimal} to a {@code double}. This conversion is similar to the <i>narrowing primitive conversion</i> from {@code double} to {@code float} as defined in section 5.1.3 of <cite>The Java&trade; Language Specification</cite>: if this {@code BigDecimal} has too great a magnitude represent as a {@code double}, it will be converted to {@link Double#NEGATIVE_INFINITY} or {@link Double#POSITIVE_INFINITY} as appropriate. Note that even when the return value is finite, this conversion can lose information about the precision of the {@code BigDecimal} value. @return this {@code BigDecimal} converted to a {@code double}.
LongOverflow::doubleValue
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
public BigDecimal ulp() { return BigDecimal.valueOf(1, this.scale(), 1); }
Returns the size of an ulp, a unit in the last place, of this {@code BigDecimal}. An ulp of a nonzero {@code BigDecimal} value is the positive distance between this value and the {@code BigDecimal} value next larger in magnitude with the same number of digits. An ulp of a zero value is numerically equal to 1 with the scale of {@code this}. The result is stored with the same scale as {@code this} so the result for zero and nonzero values is equal to {@code [1, this.scale()]}. @return the size of an ulp of {@code this} @since 1.5
LongOverflow::ulp
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
int putIntCompact(long intCompact) { assert intCompact >= 0; long q; int r; // since we start from the least significant digit, charPos points to // the last character in cmpCharArray. int charPos = cmpCharArray.length; // Get 2 digits/iteration using longs until quotient fits into an int while (intCompact > Integer.MAX_VALUE) { q = intCompact / 100; r = (int)(intCompact - q * 100); intCompact = q; cmpCharArray[--charPos] = DIGIT_ONES[r]; cmpCharArray[--charPos] = DIGIT_TENS[r]; } // Get 2 digits/iteration using ints when i2 >= 100 int q2; int i2 = (int)intCompact; while (i2 >= 100) { q2 = i2 / 100; r = i2 - q2 * 100; i2 = q2; cmpCharArray[--charPos] = DIGIT_ONES[r]; cmpCharArray[--charPos] = DIGIT_TENS[r]; } cmpCharArray[--charPos] = DIGIT_ONES[i2]; if (i2 >= 10) cmpCharArray[--charPos] = DIGIT_TENS[i2]; return charPos; }
Places characters representing the intCompact in {@code long} into cmpCharArray and returns the offset to the array where the representation starts. @param intCompact the number to put into the cmpCharArray. @return offset to the array where the representation starts. Note: intCompact must be greater or equal to zero.
StringBuilderHelper::putIntCompact
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
private String layoutChars(boolean sci) { if (scale == 0) // zero scale is trivial return (intCompact != INFLATED) ? Long.toString(intCompact): intVal.toString(); if (scale == 2 && intCompact >= 0 && intCompact < Integer.MAX_VALUE) { // currency fast path int lowInt = (int)intCompact % 100; int highInt = (int)intCompact / 100; return (Integer.toString(highInt) + '.' + StringBuilderHelper.DIGIT_TENS[lowInt] + StringBuilderHelper.DIGIT_ONES[lowInt]) ; } StringBuilderHelper sbHelper = threadLocalStringBuilderHelper.get(); char[] coeff; int offset; // offset is the starting index for coeff array // Get the significand as an absolute value if (intCompact != INFLATED) { offset = sbHelper.putIntCompact(Math.abs(intCompact)); coeff = sbHelper.getCompactCharArray(); } else { offset = 0; coeff = intVal.abs().toString().toCharArray(); } // Construct a buffer, with sufficient capacity for all cases. // If E-notation is needed, length will be: +1 if negative, +1 // if '.' needed, +2 for "E+", + up to 10 for adjusted exponent. // Otherwise it could have +1 if negative, plus leading "0.00000" StringBuilder buf = sbHelper.getStringBuilder(); if (signum() < 0) // prefix '-' if negative buf.append('-'); int coeffLen = coeff.length - offset; long adjusted = -(long)scale + (coeffLen -1); if ((scale >= 0) && (adjusted >= -6)) { // plain number int pad = scale - coeffLen; // count of padding zeros if (pad >= 0) { // 0.xxx form buf.append('0'); buf.append('.'); for (; pad>0; pad--) { buf.append('0'); } buf.append(coeff, offset, coeffLen); } else { // xx.xx form buf.append(coeff, offset, -pad); buf.append('.'); buf.append(coeff, -pad + offset, scale); } } else { // E-notation is needed if (sci) { // Scientific notation buf.append(coeff[offset]); // first character if (coeffLen > 1) { // more to come buf.append('.'); buf.append(coeff, offset + 1, coeffLen - 1); } } else { // Engineering notation int sig = (int)(adjusted % 3); if (sig < 0) sig += 3; // [adjusted was negative] adjusted -= sig; // now a multiple of 3 sig++; if (signum() == 0) { switch (sig) { case 1: buf.append('0'); // exponent is a multiple of three break; case 2: buf.append("0.00"); adjusted += 3; break; case 3: buf.append("0.0"); adjusted += 3; break; default: throw new AssertionError("Unexpected sig value " + sig); } } else if (sig >= coeffLen) { // significand all in integer buf.append(coeff, offset, coeffLen); // may need some zeros, too for (int i = sig - coeffLen; i > 0; i--) buf.append('0'); } else { // xx.xxE form buf.append(coeff, offset, sig); buf.append('.'); buf.append(coeff, offset + sig, coeffLen - sig); } } if (adjusted != 0) { // [!sci could have made 0] buf.append('E'); if (adjusted > 0) // force sign for positive buf.append('+'); buf.append(adjusted); } } return buf.toString(); }
Lay out this {@code BigDecimal} into a {@code char[]} array. The Java 1.2 equivalent to this was called {@code getValueString}. @param sci {@code true} for Scientific exponential notation; {@code false} for Engineering @return string with canonical string representation of this {@code BigDecimal}
StringBuilderHelper::layoutChars
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
private static BigInteger bigTenToThe(int n) { if (n < 0) return BigInteger.ZERO; if (n < BIG_TEN_POWERS_TABLE_MAX) { BigInteger[] pows = BIG_TEN_POWERS_TABLE; if (n < pows.length) return pows[n]; else return expandBigIntegerTenPowers(n); } return BigInteger.TEN.pow(n); }
Return 10 to the power n, as a {@code BigInteger}. @param n the power of ten to be returned (>=0) @return a {@code BigInteger} with the value (10<sup>n</sup>)
StringBuilderHelper::bigTenToThe
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { // Read in all fields s.defaultReadObject(); // validate possibly bad fields if (intVal == null) { String message = "BigDecimal: null intVal in stream"; throw new java.io.StreamCorruptedException(message); // [all values of scale are now allowed] } UnsafeHolder.setIntCompactVolatile(this, compactValFor(intVal)); }
Reconstitute the {@code BigDecimal} instance from a stream (that is, deserialize it). @param s the stream being read.
StringBuilderHelper::readObject
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { // Must inflate to maintain compatible serial form. if (this.intVal == null) UnsafeHolder.setIntValVolatile(this, BigInteger.valueOf(this.intCompact)); // Could reset intVal back to null if it has to be set. s.defaultWriteObject(); }
Serialize this {@code BigDecimal} to the stream in question @param s the stream to serialize to.
StringBuilderHelper::writeObject
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
static int longDigitLength(long x) { /* * As described in "Bit Twiddling Hacks" by Sean Anderson, * (http://graphics.stanford.edu/~seander/bithacks.html) * integer log 10 of x is within 1 of (1233/4096)* (1 + * integer log 2 of x). The fraction 1233/4096 approximates * log10(2). So we first do a version of log2 (a variant of * Long class with pre-checks and opposite directionality) and * then scale and check against powers table. This is a little * simpler in present context than the version in Hacker's * Delight sec 11-4. Adding one to bit length allows comparing * downward from the LONG_TEN_POWERS_TABLE that we need * anyway. */ assert x != BigDecimal.INFLATED; if (x < 0) x = -x; if (x < 10) // must screen for 0, might as well 10 return 1; int r = ((64 - Long.numberOfLeadingZeros(x) + 1) * 1233) >>> 12; long[] tab = LONG_TEN_POWERS_TABLE; // if r >= length, must have max possible digits for long return (r >= tab.length || x < tab[r]) ? r : r + 1; }
Returns the length of the absolute value of a {@code long}, in decimal digits. @param x the {@code long} @return the length of the unscaled value, in deciaml digits.
StringBuilderHelper::longDigitLength
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
private static int bigDigitLength(BigInteger b) { /* * Same idea as the long version, but we need a better * approximation of log10(2). Using 646456993/2^31 * is accurate up to max possible reported bitLength. */ if (b.signum == 0) return 1; int r = (int)((((long)b.bitLength() + 1) * 646456993) >>> 31); return b.compareMagnitude(bigTenToThe(r)) < 0? r : r+1; }
Returns the length of the absolute value of a BigInteger, in decimal digits. @param b the BigInteger @return the length of the unscaled value, in decimal digits
StringBuilderHelper::bigDigitLength
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
private int checkScale(long val) { int asInt = (int)val; if (asInt != val) { asInt = val>Integer.MAX_VALUE ? Integer.MAX_VALUE : Integer.MIN_VALUE; BigInteger b; if (intCompact != 0 && ((b = intVal) == null || b.signum() != 0)) throw new ArithmeticException(asInt>0 ? "Underflow":"Overflow"); } return asInt; }
Check a scale for Underflow or Overflow. If this BigDecimal is nonzero, throw an exception if the scale is outof range. If this is zero, saturate the scale to the extreme value of the right sign if the scale is out of range. @param val The new scale. @throws ArithmeticException (overflow or underflow) if the new scale is out of range. @return validated scale as an int.
StringBuilderHelper::checkScale
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
private static long compactValFor(BigInteger b) { int[] m = b.mag; int len = m.length; if (len == 0) return 0; int d = m[0]; if (len > 2 || (len == 2 && d < 0)) return INFLATED; long u = (len == 2)? (((long) m[1] & LONG_MASK) + (((long)d) << 32)) : (((long)d) & LONG_MASK); return (b.signum < 0)? -u : u; }
Returns the compact value for given {@code BigInteger}, or INFLATED if too big. Relies on internal representation of {@code BigInteger}.
StringBuilderHelper::compactValFor
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
private static void print(String name, BigDecimal bd) { System.err.format("%s:\tintCompact %d\tintVal %d\tscale %d\tprecision %d%n", name, bd.intCompact, bd.intVal, bd.scale, bd.precision); }
Returns the compact value for given {@code BigInteger}, or INFLATED if too big. Relies on internal representation of {@code BigInteger}. private static long compactValFor(BigInteger b) { int[] m = b.mag; int len = m.length; if (len == 0) return 0; int d = m[0]; if (len > 2 || (len == 2 && d < 0)) return INFLATED; long u = (len == 2)? (((long) m[1] & LONG_MASK) + (((long)d) << 32)) : (((long)d) & LONG_MASK); return (b.signum < 0)? -u : u; } private static int longCompareMagnitude(long x, long y) { if (x < 0) x = -x; if (y < 0) y = -y; return (x < y) ? -1 : ((x == y) ? 0 : 1); } private static int saturateLong(long s) { int i = (int)s; return (s == i) ? i : (s < 0 ? Integer.MIN_VALUE : Integer.MAX_VALUE); } /* Internal printing routine
StringBuilderHelper::print
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
private BigDecimal audit() { if (intCompact == INFLATED) { if (intVal == null) { print("audit", this); throw new AssertionError("null intVal"); } // Check precision if (precision > 0 && precision != bigDigitLength(intVal)) { print("audit", this); throw new AssertionError("precision mismatch"); } } else { if (intVal != null) { long val = intVal.longValue(); if (val != intCompact) { print("audit", this); throw new AssertionError("Inconsistent state, intCompact=" + intCompact + "\t intVal=" + val); } } // Check precision if (precision > 0 && precision != longDigitLength(intCompact)) { print("audit", this); throw new AssertionError("precision mismatch"); } } return this; }
Check internal invariants of this BigDecimal. These invariants include: <ul> <li>The object must be initialized; either intCompact must not be INFLATED or intVal is non-null. Both of these conditions may be true. <li>If both intCompact and intVal and set, their values must be consistent. <li>If precision is nonzero, it must have the right value. </ul> Note: Since this is an audit method, we are not supposed to change the state of this BigDecimal object.
StringBuilderHelper::audit
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
private static int checkScaleNonZero(long val) { int asInt = (int)val; if (asInt != val) { throw new ArithmeticException(asInt>0 ? "Underflow":"Overflow"); } return asInt; }
Check internal invariants of this BigDecimal. These invariants include: <ul> <li>The object must be initialized; either intCompact must not be INFLATED or intVal is non-null. Both of these conditions may be true. <li>If both intCompact and intVal and set, their values must be consistent. <li>If precision is nonzero, it must have the right value. </ul> Note: Since this is an audit method, we are not supposed to change the state of this BigDecimal object. private BigDecimal audit() { if (intCompact == INFLATED) { if (intVal == null) { print("audit", this); throw new AssertionError("null intVal"); } // Check precision if (precision > 0 && precision != bigDigitLength(intVal)) { print("audit", this); throw new AssertionError("precision mismatch"); } } else { if (intVal != null) { long val = intVal.longValue(); if (val != intCompact) { print("audit", this); throw new AssertionError("Inconsistent state, intCompact=" + intCompact + "\t intVal=" + val); } } // Check precision if (precision > 0 && precision != longDigitLength(intCompact)) { print("audit", this); throw new AssertionError("precision mismatch"); } } return this; } /* the same as checkScale where value!=0
StringBuilderHelper::checkScaleNonZero
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
private static BigDecimal doRound(BigDecimal val, MathContext mc) { int mcp = mc.precision; boolean wasDivided = false; if (mcp > 0) { BigInteger intVal = val.intVal; long compactVal = val.intCompact; int scale = val.scale; int prec = val.precision(); int mode = mc.roundingMode.oldMode; int drop; if (compactVal == INFLATED) { drop = prec - mcp; while (drop > 0) { scale = checkScaleNonZero((long) scale - drop); intVal = divideAndRoundByTenPow(intVal, drop, mode); wasDivided = true; compactVal = compactValFor(intVal); if (compactVal != INFLATED) { prec = longDigitLength(compactVal); break; } prec = bigDigitLength(intVal); drop = prec - mcp; } } if (compactVal != INFLATED) { drop = prec - mcp; // drop can't be more than 18 while (drop > 0) { scale = checkScaleNonZero((long) scale - drop); compactVal = divideAndRound(compactVal, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode); wasDivided = true; prec = longDigitLength(compactVal); drop = prec - mcp; intVal = null; } } return wasDivided ? new BigDecimal(intVal,compactVal,scale,prec) : val; } return val; }
Returns a {@code BigDecimal} rounded according to the MathContext settings; If rounding is needed a new {@code BigDecimal} is created and returned. @param val the value to be rounded @param mc the context to use. @return a {@code BigDecimal} rounded according to the MathContext settings. May return {@code value}, if no rounding needed. @throws ArithmeticException if the rounding mode is {@code RoundingMode.UNNECESSARY} and the result is inexact.
StringBuilderHelper::doRound
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
private static BigDecimal doRound(long compactVal, int scale, MathContext mc) { int mcp = mc.precision; if (mcp > 0 && mcp < 19) { int prec = longDigitLength(compactVal); int drop = prec - mcp; // drop can't be more than 18 while (drop > 0) { scale = checkScaleNonZero((long) scale - drop); compactVal = divideAndRound(compactVal, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode); prec = longDigitLength(compactVal); drop = prec - mcp; } return valueOf(compactVal, scale, prec); } return valueOf(compactVal, scale); }
Returns a {@code BigDecimal} rounded according to the MathContext settings; If rounding is needed a new {@code BigDecimal} is created and returned. @param val the value to be rounded @param mc the context to use. @return a {@code BigDecimal} rounded according to the MathContext settings. May return {@code value}, if no rounding needed. @throws ArithmeticException if the rounding mode is {@code RoundingMode.UNNECESSARY} and the result is inexact. private static BigDecimal doRound(BigDecimal val, MathContext mc) { int mcp = mc.precision; boolean wasDivided = false; if (mcp > 0) { BigInteger intVal = val.intVal; long compactVal = val.intCompact; int scale = val.scale; int prec = val.precision(); int mode = mc.roundingMode.oldMode; int drop; if (compactVal == INFLATED) { drop = prec - mcp; while (drop > 0) { scale = checkScaleNonZero((long) scale - drop); intVal = divideAndRoundByTenPow(intVal, drop, mode); wasDivided = true; compactVal = compactValFor(intVal); if (compactVal != INFLATED) { prec = longDigitLength(compactVal); break; } prec = bigDigitLength(intVal); drop = prec - mcp; } } if (compactVal != INFLATED) { drop = prec - mcp; // drop can't be more than 18 while (drop > 0) { scale = checkScaleNonZero((long) scale - drop); compactVal = divideAndRound(compactVal, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode); wasDivided = true; prec = longDigitLength(compactVal); drop = prec - mcp; intVal = null; } } return wasDivided ? new BigDecimal(intVal,compactVal,scale,prec) : val; } return val; } /* Returns a {@code BigDecimal} created from {@code long} value with given scale rounded according to the MathContext settings
StringBuilderHelper::doRound
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
private static BigDecimal doRound(BigInteger intVal, int scale, MathContext mc) { int mcp = mc.precision; int prec = 0; if (mcp > 0) { long compactVal = compactValFor(intVal); int mode = mc.roundingMode.oldMode; int drop; if (compactVal == INFLATED) { prec = bigDigitLength(intVal); drop = prec - mcp; while (drop > 0) { scale = checkScaleNonZero((long) scale - drop); intVal = divideAndRoundByTenPow(intVal, drop, mode); compactVal = compactValFor(intVal); if (compactVal != INFLATED) { break; } prec = bigDigitLength(intVal); drop = prec - mcp; } } if (compactVal != INFLATED) { prec = longDigitLength(compactVal); drop = prec - mcp; // drop can't be more than 18 while (drop > 0) { scale = checkScaleNonZero((long) scale - drop); compactVal = divideAndRound(compactVal, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode); prec = longDigitLength(compactVal); drop = prec - mcp; } return valueOf(compactVal,scale,prec); } } return new BigDecimal(intVal,INFLATED,scale,prec); }
Returns a {@code BigDecimal} rounded according to the MathContext settings; If rounding is needed a new {@code BigDecimal} is created and returned. @param val the value to be rounded @param mc the context to use. @return a {@code BigDecimal} rounded according to the MathContext settings. May return {@code value}, if no rounding needed. @throws ArithmeticException if the rounding mode is {@code RoundingMode.UNNECESSARY} and the result is inexact. private static BigDecimal doRound(BigDecimal val, MathContext mc) { int mcp = mc.precision; boolean wasDivided = false; if (mcp > 0) { BigInteger intVal = val.intVal; long compactVal = val.intCompact; int scale = val.scale; int prec = val.precision(); int mode = mc.roundingMode.oldMode; int drop; if (compactVal == INFLATED) { drop = prec - mcp; while (drop > 0) { scale = checkScaleNonZero((long) scale - drop); intVal = divideAndRoundByTenPow(intVal, drop, mode); wasDivided = true; compactVal = compactValFor(intVal); if (compactVal != INFLATED) { prec = longDigitLength(compactVal); break; } prec = bigDigitLength(intVal); drop = prec - mcp; } } if (compactVal != INFLATED) { drop = prec - mcp; // drop can't be more than 18 while (drop > 0) { scale = checkScaleNonZero((long) scale - drop); compactVal = divideAndRound(compactVal, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode); wasDivided = true; prec = longDigitLength(compactVal); drop = prec - mcp; intVal = null; } } return wasDivided ? new BigDecimal(intVal,compactVal,scale,prec) : val; } return val; } /* Returns a {@code BigDecimal} created from {@code long} value with given scale rounded according to the MathContext settings private static BigDecimal doRound(long compactVal, int scale, MathContext mc) { int mcp = mc.precision; if (mcp > 0 && mcp < 19) { int prec = longDigitLength(compactVal); int drop = prec - mcp; // drop can't be more than 18 while (drop > 0) { scale = checkScaleNonZero((long) scale - drop); compactVal = divideAndRound(compactVal, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode); prec = longDigitLength(compactVal); drop = prec - mcp; } return valueOf(compactVal, scale, prec); } return valueOf(compactVal, scale); } /* Returns a {@code BigDecimal} created from {@code BigInteger} value with given scale rounded according to the MathContext settings
StringBuilderHelper::doRound
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
private static BigInteger divideAndRoundByTenPow(BigInteger intVal, int tenPow, int roundingMode) { if (tenPow < LONG_TEN_POWERS_TABLE.length) intVal = divideAndRound(intVal, LONG_TEN_POWERS_TABLE[tenPow], roundingMode); else intVal = divideAndRound(intVal, bigTenToThe(tenPow), roundingMode); return intVal; }
Returns a {@code BigDecimal} rounded according to the MathContext settings; If rounding is needed a new {@code BigDecimal} is created and returned. @param val the value to be rounded @param mc the context to use. @return a {@code BigDecimal} rounded according to the MathContext settings. May return {@code value}, if no rounding needed. @throws ArithmeticException if the rounding mode is {@code RoundingMode.UNNECESSARY} and the result is inexact. private static BigDecimal doRound(BigDecimal val, MathContext mc) { int mcp = mc.precision; boolean wasDivided = false; if (mcp > 0) { BigInteger intVal = val.intVal; long compactVal = val.intCompact; int scale = val.scale; int prec = val.precision(); int mode = mc.roundingMode.oldMode; int drop; if (compactVal == INFLATED) { drop = prec - mcp; while (drop > 0) { scale = checkScaleNonZero((long) scale - drop); intVal = divideAndRoundByTenPow(intVal, drop, mode); wasDivided = true; compactVal = compactValFor(intVal); if (compactVal != INFLATED) { prec = longDigitLength(compactVal); break; } prec = bigDigitLength(intVal); drop = prec - mcp; } } if (compactVal != INFLATED) { drop = prec - mcp; // drop can't be more than 18 while (drop > 0) { scale = checkScaleNonZero((long) scale - drop); compactVal = divideAndRound(compactVal, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode); wasDivided = true; prec = longDigitLength(compactVal); drop = prec - mcp; intVal = null; } } return wasDivided ? new BigDecimal(intVal,compactVal,scale,prec) : val; } return val; } /* Returns a {@code BigDecimal} created from {@code long} value with given scale rounded according to the MathContext settings private static BigDecimal doRound(long compactVal, int scale, MathContext mc) { int mcp = mc.precision; if (mcp > 0 && mcp < 19) { int prec = longDigitLength(compactVal); int drop = prec - mcp; // drop can't be more than 18 while (drop > 0) { scale = checkScaleNonZero((long) scale - drop); compactVal = divideAndRound(compactVal, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode); prec = longDigitLength(compactVal); drop = prec - mcp; } return valueOf(compactVal, scale, prec); } return valueOf(compactVal, scale); } /* Returns a {@code BigDecimal} created from {@code BigInteger} value with given scale rounded according to the MathContext settings private static BigDecimal doRound(BigInteger intVal, int scale, MathContext mc) { int mcp = mc.precision; int prec = 0; if (mcp > 0) { long compactVal = compactValFor(intVal); int mode = mc.roundingMode.oldMode; int drop; if (compactVal == INFLATED) { prec = bigDigitLength(intVal); drop = prec - mcp; while (drop > 0) { scale = checkScaleNonZero((long) scale - drop); intVal = divideAndRoundByTenPow(intVal, drop, mode); compactVal = compactValFor(intVal); if (compactVal != INFLATED) { break; } prec = bigDigitLength(intVal); drop = prec - mcp; } } if (compactVal != INFLATED) { prec = longDigitLength(compactVal); drop = prec - mcp; // drop can't be more than 18 while (drop > 0) { scale = checkScaleNonZero((long) scale - drop); compactVal = divideAndRound(compactVal, LONG_TEN_POWERS_TABLE[drop], mc.roundingMode.oldMode); prec = longDigitLength(compactVal); drop = prec - mcp; } return valueOf(compactVal,scale,prec); } } return new BigDecimal(intVal,INFLATED,scale,prec); } /* Divides {@code BigInteger} value by ten power.
StringBuilderHelper::divideAndRoundByTenPow
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
private static BigDecimal divideAndRound(long ldividend, long ldivisor, int scale, int roundingMode, int preferredScale) { int qsign; // quotient sign long q = ldividend / ldivisor; // store quotient in long if (roundingMode == ROUND_DOWN && scale == preferredScale) return valueOf(q, scale); long r = ldividend % ldivisor; // store remainder in long qsign = ((ldividend < 0) == (ldivisor < 0)) ? 1 : -1; if (r != 0) { boolean increment = needIncrement(ldivisor, roundingMode, qsign, q, r); return valueOf((increment ? q + qsign : q), scale); } else { if (preferredScale != scale) return createAndStripZerosToMatchScale(q, scale, preferredScale); else return valueOf(q, scale); } }
Internally used for division operation for division {@code long} by {@code long}. The returned {@code BigDecimal} object is the quotient whose scale is set to the passed in scale. If the remainder is not zero, it will be rounded based on the passed in roundingMode. Also, if the remainder is zero and the last parameter, i.e. preferredScale is NOT equal to scale, the trailing zeros of the result is stripped to match the preferredScale.
StringBuilderHelper::divideAndRound
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
private static long divideAndRound(long ldividend, long ldivisor, int roundingMode) { int qsign; // quotient sign long q = ldividend / ldivisor; // store quotient in long if (roundingMode == ROUND_DOWN) return q; long r = ldividend % ldivisor; // store remainder in long qsign = ((ldividend < 0) == (ldivisor < 0)) ? 1 : -1; if (r != 0) { boolean increment = needIncrement(ldivisor, roundingMode, qsign, q, r); return increment ? q + qsign : q; } else { return q; } }
Divides {@code long} by {@code long} and do rounding based on the passed in roundingMode.
StringBuilderHelper::divideAndRound
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
private static boolean commonNeedIncrement(int roundingMode, int qsign, int cmpFracHalf, boolean oddQuot) { switch(roundingMode) { case ROUND_UNNECESSARY: throw new ArithmeticException("Rounding necessary"); case ROUND_UP: // Away from zero return true; case ROUND_DOWN: // Towards zero return false; case ROUND_CEILING: // Towards +infinity return qsign > 0; 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); } } } }
Shared logic of need increment computation.
StringBuilderHelper::commonNeedIncrement
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
private static boolean needIncrement(long ldivisor, int roundingMode, int qsign, long q, long r) { assert r != 0L; int cmpFracHalf; if (r <= HALF_LONG_MIN_VALUE || r > HALF_LONG_MAX_VALUE) { cmpFracHalf = 1; // 2 * r can't fit into long } else { cmpFracHalf = longCompareMagnitude(2 * r, ldivisor); } return commonNeedIncrement(roundingMode, qsign, cmpFracHalf, (q & 1L) != 0L); }
Tests if quotient has to be incremented according the roundingMode
StringBuilderHelper::needIncrement
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
private static BigInteger divideAndRound(BigInteger bdividend, long ldivisor, int roundingMode) { boolean isRemainderZero; // record remainder is zero or not int qsign; // quotient sign long r = 0; // store quotient & remainder in long MutableBigInteger mq = null; // store quotient // Descend into mutables for faster remainder checks MutableBigInteger mdividend = new MutableBigInteger(bdividend.mag); mq = new MutableBigInteger(); r = mdividend.divide(ldivisor, mq); isRemainderZero = (r == 0); qsign = (ldivisor < 0) ? -bdividend.signum : bdividend.signum; if (!isRemainderZero) { if(needIncrement(ldivisor, roundingMode, qsign, mq, r)) { mq.add(MutableBigInteger.ONE); } } return mq.toBigInteger(qsign); }
Divides {@code BigInteger} value by {@code long} value and do rounding based on the passed in roundingMode.
StringBuilderHelper::divideAndRound
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
private static BigDecimal divideAndRound(BigInteger bdividend, long ldivisor, int scale, int roundingMode, int preferredScale) { boolean isRemainderZero; // record remainder is zero or not int qsign; // quotient sign long r = 0; // store quotient & remainder in long MutableBigInteger mq = null; // store quotient // Descend into mutables for faster remainder checks MutableBigInteger mdividend = new MutableBigInteger(bdividend.mag); mq = new MutableBigInteger(); r = mdividend.divide(ldivisor, mq); isRemainderZero = (r == 0); qsign = (ldivisor < 0) ? -bdividend.signum : bdividend.signum; if (!isRemainderZero) { if(needIncrement(ldivisor, roundingMode, qsign, mq, r)) { mq.add(MutableBigInteger.ONE); } return mq.toBigDecimal(qsign, scale); } else { if (preferredScale != scale) { long compactVal = mq.toCompactValue(qsign); if(compactVal!=INFLATED) { return createAndStripZerosToMatchScale(compactVal, scale, preferredScale); } BigInteger intVal = mq.toBigInteger(qsign); return createAndStripZerosToMatchScale(intVal,scale, preferredScale); } else { return mq.toBigDecimal(qsign, scale); } } }
Internally used for division operation for division {@code BigInteger} by {@code long}. The returned {@code BigDecimal} object is the quotient whose scale is set to the passed in scale. If the remainder is not zero, it will be rounded based on the passed in roundingMode. Also, if the remainder is zero and the last parameter, i.e. preferredScale is NOT equal to scale, the trailing zeros of the result is stripped to match the preferredScale.
StringBuilderHelper::divideAndRound
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0
private static boolean needIncrement(long ldivisor, int roundingMode, int qsign, MutableBigInteger mq, long r) { assert r != 0L; int cmpFracHalf; if (r <= HALF_LONG_MIN_VALUE || r > HALF_LONG_MAX_VALUE) { cmpFracHalf = 1; // 2 * r can't fit into long } else { cmpFracHalf = longCompareMagnitude(2 * r, ldivisor); } return commonNeedIncrement(roundingMode, qsign, cmpFracHalf, mq.isOdd()); }
Tests if quotient has to be incremented according the roundingMode
StringBuilderHelper::needIncrement
java
google/j2objc
jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
https://github.com/google/j2objc/blob/master/jre_emul/openjdk/src/share/classes/java/math/BigDecimal.java
Apache-2.0