/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* Portions Copyright IBM Corporation, 2001. All Rights Reserved.
*/
/**
* Immutable, arbitrary-precision signed decimal numbers. A
* {@code BigDecimal} consists of an arbitrary precision integer
* <i>unscaled value</i> and a 32-bit integer <i>scale</i>. 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. The
* value of the number represented by the {@code BigDecimal} is
* therefore <tt>(unscaledValue × 10<sup>-scale</sup>)</tt>.
*
* <p>The {@code BigDecimal} class provides operations for
* arithmetic, scale manipulation, rounding, comparison, hashing, and
* format conversion. The {@link #toString} method provides a
* canonical representation of a {@code BigDecimal}.
*
* <p>The {@code BigDecimal} class gives its user complete control
* over rounding behavior. If no rounding mode is specified and the
* exact result cannot be represented, an exception is thrown;
* otherwise, calculations can be carried out to a chosen precision
* and rounding mode by supplying an appropriate {@link MathContext}
* object to the operation. In either case, eight <em>rounding
* modes</em> are provided for the control of rounding. Using the
* integer fields in this class (such as {@link #ROUND_HALF_UP}) to
* represent rounding mode is largely obsolete; the enumeration values
* of the {@code RoundingMode} {@code enum}, (such as {@link
* RoundingMode#HALF_UP}) should be used instead.
*
* <p>When a {@code MathContext} object is supplied with a precision
* setting of 0 (for example, {@link MathContext#UNLIMITED}),
* arithmetic operations are exact, as are the arithmetic methods
* which take no {@code MathContext} object. (This is the only
* behavior that was supported in releases prior to 5.) As a
* corollary of computing the exact result, the rounding mode setting
* of a {@code MathContext} object with a precision setting of 0 is
* not used and thus irrelevant. In the case of divide, the exact
* quotient could have an infinitely long decimal expansion; for
* example, 1 divided by 3. If the quotient has a nonterminating
* decimal expansion and the operation is specified to return an exact
* result, an {@code ArithmeticException} is thrown. Otherwise, the
* exact result of the division is returned, as done for other
* operations.
*
* <p>When the precision setting is not 0, the rules of
* {@code BigDecimal} arithmetic are broadly compatible with selected
* modes of operation of the arithmetic defined in ANSI X3.274-1996
* and ANSI X3.274-1996/AM 1-2000 (section 7.4). Unlike those
* standards, {@code BigDecimal} includes many rounding modes, which
* were mandatory for division in {@code BigDecimal} releases prior
* to 5. Any conflicts between these ANSI standards and the
* {@code BigDecimal} specification are resolved in favor of
* {@code BigDecimal}.
*
* <p>Since the same numerical value can have different
* representations (with different scales), the rules of arithmetic
* and rounding must specify both the numerical result and the scale
* used in the result's representation.
*
*
* <p>In general the rounding modes and precision setting determine
* how operations return results with a limited number of digits when
* the exact result has more digits (perhaps infinitely many in the
* case of division) than the number of digits returned.
*
* First, the
* total number of digits to return is specified by the
* {@code MathContext}'s {@code precision} setting; this determines
* the result's <i>precision</i>. The digit count starts from the
* leftmost nonzero digit of the exact result. The rounding mode
* determines how any discarded trailing digits affect the returned
* result.
*
* <p>For all arithmetic operators , the operation is carried out as
* though an exact intermediate result were first calculated and then
* rounded to the number of digits specified by the precision setting
* (if necessary), using the selected rounding mode. If the exact
* result is not returned, some digit positions of the exact result
* are discarded. When rounding increases the magnitude of the
* returned result, it is possible for a new digit position to be
* created by a carry propagating to a leading {@literal "9"} digit.
* For example, rounding the value 999.9 to three digits rounding up
* would be numerically equal to one thousand, represented as
* 100×10<sup>1</sup>. In such cases, the new {@literal "1"} is
* the leading digit position of the returned result.
*
* <p>Besides a logical exact result, each arithmetic operation has a
* preferred scale for representing a result. The preferred
* scale for each operation is listed in the table below.
*
* <table border>
* <caption><b>Preferred Scales for Results of Arithmetic Operations
* </b></caption>
* <tr><th>Operation</th><th>Preferred Scale of Result</th></tr>
* <tr><td>Add</td><td>max(addend.scale(), augend.scale())</td>
* <tr><td>Subtract</td><td>max(minuend.scale(), subtrahend.scale())</td>
* <tr><td>Multiply</td><td>multiplier.scale() + multiplicand.scale()</td>
* <tr><td>Divide</td><td>dividend.scale() - divisor.scale()</td>
* </table>
*
* These scales are the ones used by the methods which return exact
* arithmetic results; except that an exact divide may have to use a
* larger scale since the exact result may have more digits. For
* example, {@code 1/32} is {@code 0.03125}.
*
* <p>Before rounding, the scale of the logical exact intermediate
* result is the preferred scale for that operation. If the exact
* numerical result cannot be represented in {@code precision}
* digits, rounding selects the set of digits to return and the scale
* of the result is reduced from the scale of the intermediate result
* to the least scale which can represent the {@code precision}
* digits actually returned. If the exact result can be represented
* with at most {@code precision} digits, the representation
* of the result with the scale closest to the preferred scale is
* returned. In particular, an exactly representable quotient may be
* represented in fewer than {@code precision} digits by removing
* trailing zeros and decreasing the scale. For example, rounding to
* three digits using the {@linkplain RoundingMode#FLOOR floor}
* rounding mode, <br>
*
* {@code 19/100 = 0.19 // integer=19, scale=2} <br>
*
* but<br>
*
* {@code 21/110 = 0.190 // integer=190, scale=3} <br>
*
* <p>Note that for add, subtract, and multiply, the reduction in
* scale will equal the number of digit positions of the exact result
* which are discarded. If the rounding causes a carry propagation to
* create a new high-order digit position, an additional digit of the
* result is discarded than when no new digit position is created.
*
* <p>Other methods may have slightly different rounding semantics.
* For example, the result of the {@code pow} method using the
* {@linkplain #pow(int, MathContext) specified algorithm} can
* occasionally differ from the rounded mathematical result by more
* than one unit in the last place, one <i>{@linkplain #ulp() ulp}</i>.
*
* <p>Two types of operations are provided for manipulating the scale
* #setScale setScale} and {@link #round round}) return a
* {@code BigDecimal} whose value is approximately (or exactly) equal
* to that of the operand, but whose scale or precision is the
* specified value; that is, they increase or decrease the precision
* of the stored number with minimal effect on its value. Decimal
* point motion operations ({@link #movePointLeft movePointLeft} and
* {@link #movePointRight movePointRight}) return a
* {@code BigDecimal} created from the operand by moving the decimal
* point a specified distance in the specified direction.
*
* <p>For the sake of brevity and clarity, pseudo-code is used
* throughout the descriptions of {@code BigDecimal} methods. The
* pseudo-code expression {@code (i + j)} is shorthand for "a
* {@code BigDecimal} whose value is that of the {@code BigDecimal}
* {@code i} added to that of the {@code BigDecimal}
* {@code j}." The pseudo-code expression {@code (i == j)} is
* shorthand for "{@code true} if and only if the
* {@code BigDecimal} {@code i} represents the same value as the
* {@code BigDecimal} {@code j}." Other pseudo-code expressions
* are interpreted similarly. Square brackets are used to represent
* the particular {@code BigInteger} and scale pair defining a
* {@code BigDecimal} value; for example [19, 2] is the
* {@code BigDecimal} numerically equal to 0.19 having a scale of 2.
*
* <p>Note: care should be exercised if {@code BigDecimal} objects
* are used as keys in a {@link java.util.SortedMap SortedMap} or
* elements in a {@link java.util.SortedSet SortedSet} since
* {@code BigDecimal}'s <i>natural ordering</i> is <i>inconsistent
* with equals</i>. See {@link Comparable}, {@link
* java.util.SortedMap} or {@link java.util.SortedSet} for more
* information.
*
* <p>All methods and constructors for this class throw
* {@code NullPointerException} when passed a {@code null} object
* reference for any input parameter.
*
* @see BigInteger
* @see MathContext
* @see RoundingMode
* @see java.util.SortedMap
* @see java.util.SortedSet
* @author Josh Bloch
* @author Mike Cowlishaw
* @author Joseph D. Darcy
*/
/**
* The unscaled value of this BigDecimal, as returned by {@link
* #unscaledValue}.
*
* @serial
* @see #unscaledValue
*/
/**
* The scale of this BigDecimal, as returned by {@link #scale}.
*
* @serial
* @see #scale
*/
// calculations must be done in longs
/**
* The number of decimal digits in this BigDecimal, or 0 if the
* number of digits are not known (lookaside information). If
* nonzero, the value is guaranteed correct. Use the precision()
* method to obtain and set the value if it might be 0. This
* field is mutable until set nonzero.
*
* @since 1.5
*/
private transient int precision;
/**
* Used to store the canonical string representation, if computed.
*/
/**
* Sentinel value for {@link #intCompact} indicating the
* significand information is only available from {@code intVal}.
*/
/**
* If the absolute value of the significand of this BigDecimal is
* less than or equal to {@code Long.MAX_VALUE}, the value can be
* compactly stored in this field and used in computations.
*/
private transient long intCompact;
// All 18-digit base ten strings fit into a long; not all 19-digit
// strings will
/* Appease the serialization gods */
private static final ThreadLocal<StringBuilderHelper>
protected StringBuilderHelper initialValue() {
return new StringBuilderHelper();
}
};
// Cache of common small BigDecimal values.
};
// Cache of zero scaled by 0 - 15
zeroThroughTen[0],
};
// Half of Long.MIN_VALUE & Long.MAX_VALUE.
// Constants
/**
* The value 0, with a scale of 0.
*
* @since 1.5
*/
zeroThroughTen[0];
/**
* The value 1, with a scale of 0.
*
* @since 1.5
*/
zeroThroughTen[1];
/**
* The value 10, with a scale of 0.
*
* @since 1.5
*/
zeroThroughTen[10];
// Constructors
/**
* Trusted package private constructor.
* Trusted simply means if val is INFLATED, intVal could not be null and
* if intVal is null, val could not be INFLATED.
*/
this.intCompact = val;
}
/**
* 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
*/
// protect against huge length.
throw new NumberFormatException();
// 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
// use array bounds checking to handle too-long, len == 0,
// bad offset, etc.
try {
// handle the sign
boolean isneg = false; // assume positive
isneg = true; // leading minus means negative
offset++;
len--;
offset++;
len--;
}
// should now be at numeric part of the significand
boolean dot = false; // true when there is a '.'
char c; // current character
// 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;
// have digit
// First compact case, we need not to preserve the character
// and we can just compute the value in place.
if (isCompact) {
if (digit == 0) {
if (prec == 0)
prec = 1;
else if (rs != 0) {
rs *= 10;
++prec;
} // else digit is a redundant leading zero
} else {
++prec; // prec unchanged if preceded by 0s
}
} else { // the unscaled value is likely a BigInteger object.
if (prec == 0) {
prec = 1;
} else if (idx != 0) {
++prec;
} // else c must be a redundant leading zero
} else {
++prec; // prec unchanged if preceded by 0s
}
}
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();
offset++;
len--;
boolean negexp = (c == '-');
// optional sign
if (negexp || c == '+') {
offset++;
len--;
}
throw new NumberFormatException();
// skip leading zeros in the exponent
offset++;
len--;
}
throw new NumberFormatException();
// c now holds first digit of exponent
for (;; len--) {
int v;
if (c >= '0' && c <= '9') {
v = c - '0';
} else {
if (v < 0) // not a digit
throw new NumberFormatException();
}
if (len == 1)
break; // that was final character
offset++;
}
if (negexp) // apply sign
// Next test is required for backwards compatibility
throw new NumberFormatException();
break; // [saves a test]
}
// here when no characters left
throw new NumberFormatException();
// Adjust scale if exp is not zero.
// Can't call checkScale which relies on proper fields value
throw new NumberFormatException("Scale out of range.");
scl = (int)adjustedScale;
}
// Remove leading zeros from precision (digits count)
if (isCompact) {
} else {
char quick[];
if (!isneg) {
} else {
}
}
} catch (ArrayIndexOutOfBoundsException e) {
throw new NumberFormatException();
} catch (NegativeArraySizeException e) {
throw new NumberFormatException();
}
this.intCompact = rs;
}
/**
* 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
*/
}
/**
* 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
*/
}
/**
* 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
*/
}
/**
* Translates the string representation of a {@code BigDecimal}
* into a {@code BigDecimal}. The string representation consists
* of an optional sign, {@code '+'} (<tt> '\u002B'</tt>) or
* {@code '-'} (<tt>'\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>'\u0065'</tt>) or {@code 'E'} (<tt>'\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>
* <p>
* <dt><i>Sign:</i>
* <dd>{@code +}
* <dd>{@code -}
* <p>
* <dt><i>Significand:</i>
* <dd><i>IntegerPart</i> {@code .} <i>FractionPart<sub>opt</sub></i>
* <dd>{@code .} <i>FractionPart</i>
* <dd><i>IntegerPart</i>
* <p>
* <dt><i>IntegerPart:</i>
* <dd><i>Digits</i>
* <p>
* <dt><i>FractionPart:</i>
* <dd><i>Digits</i>
* <p>
* <dt><i>Exponent:</i>
* <dd><i>ExponentIndicator SignedInteger</i>
* <p>
* <dt><i>ExponentIndicator:</i>
* <dd>{@code e}
* <dd>{@code E}
* <p>
* <dt><i>SignedInteger:</i>
* <dd><i>Sign<sub>opt</sub> Digits</i>
* <p>
* <dt><i>Digits:</i>
* <dd><i>Digit</i>
* <dd><i>Digits Digit</i>
* <p>
* <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> × 10<sup> <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 ±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}.
*/
}
/**
* 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
*/
}
/**
* 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> × 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.
*/
throw new NumberFormatException("Infinite or NaN");
// Translate the double into sign, exponent and significand, according
// to the formulae in JLS, Section 20.10.22.
exponent -= 1075;
// At this point, val == sign * significand * 2**exponent.
/*
* Special case zero to supress nonterminating normalization
* and bogus scale calculation.
*/
if (significand == 0) {
intCompact = 0;
precision = 1;
return;
}
// Normalize
significand >>= 1;
exponent++;
}
// Calculate intVal and scale
long s = sign * significand;
BigInteger b;
if (exponent < 0) {
} else if (exponent > 0) {
} else {
b = BigInteger.valueOf(s);
}
intCompact = compactValFor(b);
}
/**
* 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> × 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
*/
this(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}.
*/
}
/**
* 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
*/
this(val);
}
/**
* Translates a {@code BigInteger} unscaled value and an
* {@code int} scale into a {@code BigDecimal}. The value of
* the {@code BigDecimal} is
* <tt>(unscaledVal × 10<sup>-scale</sup>)</tt>.
*
* @param unscaledVal unscaled value of the {@code BigDecimal}.
* @param scale scale of the {@code BigDecimal}.
*/
// Negative scales are now allowed
this(unscaledVal);
}
/**
* 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 ×
* 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
*/
this(unscaledVal);
}
/**
* 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
*/
intCompact = val;
}
/**
* 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
*/
intCompact = val;
}
/**
* 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
*/
this.intCompact = val;
}
/**
* 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
*/
this(val);
}
// Static Factory Methods
/**
* 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 × 10<sup>-scale</sup>)</tt>.
*/
if (scale == 0)
return valueOf(unscaledVal);
else if (unscaledVal == 0) {
return ZERO_SCALED_BY[scale];
else
}
}
/**
* 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}.
*/
return zeroThroughTen[(int)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
*/
// 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.
}
// Arithmetic Operations
/**
* 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}
*/
long xs = this.intCompact;
if (sdiff != 0) {
if (sdiff < 0) {
} else {
}
}
// See "Hacker's Delight" section 2-12 for explanation of
// the overflow test.
}
}
/**
* 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 lhs = this;
// Could optimize if values are compact
this.inflate();
// If either number is zero then the other number, rounded and
// scaled if necessary, is used as the result.
{
if (lhsIsZero || augendIsZero) {
// Could use a factory for zero instead of a new object
if (lhsIsZero && augendIsZero)
return result;
return scaledResult;
} else { // result.scale < preferredScale
if (precisionDiff >= scaleDiff)
else
}
}
}
}
}
/**
* 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.
*/
assert padding != 0;
} else { // lhs is small; augend is big
}
/*
* 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)
*/
/*
* 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.
*/
}
// Since addition is symmetric, preserving input order in
// returned operands doesn't matter
return result;
}
/**
* 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}
*/
}
/**
* 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
*/
return add(nsubtrahend);
// share the special rounding code in add()
}
/**
* Returns a {@code BigDecimal} whose value is <tt>(this ×
* 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}
*/
long x = this.intCompact;
long y = multiplicand.intCompact;
// Might be able to do a more clever check incorporating the
// inflated check into the overflow computation.
/*
* If the product is not an overflowed value, continue
* to use the compact representation. if either of x or y
* is INFLATED, the product should also be regarded as
* an overflow. Before using the overflow test suggested in
* "Hacker's Delight" section 2-12, we perform quick checks
* using the precision information to see whether the overflow
* would occur since division is expensive on most CPUs.
*/
long product = x * y;
productScale, 0);
}
else if (x != INFLATED)
else
}
/**
* Returns a {@code BigDecimal} whose value is <tt>(this ×
* 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
*/
return multiply(multiplicand);
}
/**
* 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
*/
/*
* IMPLEMENTATION NOTE: This method *must* return a new object
* since divideAndRound uses divide to generate a value whose
* scale is then modified.
*/
throw new IllegalArgumentException("Invalid rounding mode");
/*
* Rescale dividend or divisor (whichever can be "upscaled" to
* produce correctly scaled quotient).
* Take care to detect out-of-range scales
*/
BigDecimal dividend = this;
else
}
/**
* Internally used for division operation. The dividend and divisor are
* passed both in {@code long} format and {@code BigInteger} format. 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.
*/
int scale, int roundingMode,
int preferredScale) {
boolean isRemainderZero; // record remainder is zero or not
int qsign; // quotient sign
long q = 0, r = 0; // store quotient & remainder in long
if (isLongDivision) {
isRemainderZero = (r == 0);
} else {
// Descend into mutables for faster remainder checks
mq = new MutableBigInteger();
isRemainderZero = (r == 0);
} else {
}
}
boolean increment = false;
if (!isRemainderZero) {
int cmpFracHalf;
/* Round as appropriate */
throw new ArithmeticException("Rounding necessary");
increment = true;
increment = false;
} else {
if (r <= HALF_LONG_MIN_VALUE || r > HALF_LONG_MAX_VALUE) {
} else {
}
} else {
}
if (cmpFracHalf < 0)
increment = false; // We're closer to higher digit
increment = true;
else if (roundingMode == ROUND_HALF_UP)
increment = true;
else if (roundingMode == ROUND_HALF_DOWN)
increment = false;
else // roundingMode == ROUND_HALF_EVEN, true iff quotient is odd
}
}
if (isLongDivision)
else {
if (increment)
}
return res;
}
/**
* 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
*/
}
/**
* 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
*/
}
/**
* 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
*/
}
/**
* 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
*/
/*
* Handle zero cases first.
*/
throw new ArithmeticException("Division by zero");
}
// Calculate preferred scale
return (preferredScale >= 0 &&
else {
this.inflate();
/*
* 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.
*/
try {
} catch (ArithmeticException e) {
throw new ArithmeticException("Non-terminating decimal expansion; " +
"no exact representable decimal result.");
}
// 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;
}
}
/**
* 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
*/
if (mcp == 0)
BigDecimal dividend = this;
// 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.
throw new ArithmeticException("Division by zero");
}
// Normalize dividend & divisor so that both fall into [0.1, 0.999...]
// In order to find out whether the divide generates the exact result,
// we avoid calling the above divide method. 'quotient' holds the
// return BigDecimal object whose scale will be set to 'scl'.
else
// doRound, here, only affects 1000000000 case.
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
*/
// Calculate preferred scale
// much faster when this << divisor
}
// Perform a divide with enough digits to round to a correct
// integer value; then remove any fractional digits
RoundingMode.DOWN));
}
// pad with zeros if necessary
}
return quotient;
}
/**
* 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
*/
return divideToIntegralValue(divisor);
// Calculate preferred 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.
/*
* 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.
*/
// If the quotient is the full integer value,
// |dividend-product| < |divisor|.
throw new ArithmeticException("Division impossible");
}
/*
* 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.
*/
}
// else result.scale() == 0;
int precisionDiff;
} else {
return result;
}
}
/**
* 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
*/
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
*/
return divrem[1];
}
/**
* 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
*/
// we use the identity x = i * y + r to determine r
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
*/
return divideAndRemainder(divisor);
BigDecimal lhs = this;
return result;
}
/**
* 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
*/
if (n < 0 || n > 999999999)
throw new ArithmeticException("Invalid operation");
// Don't attempt to support "supernormal" numbers.
this.inflate();
}
/**
* 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
*/
return pow(n);
if (n < -999999999 || n > 999999999)
throw new ArithmeticException("Invalid operation");
if (n == 0)
return ONE; // x**0 == 1 in X3.274
this.inflate();
BigDecimal lhs = this;
throw new ArithmeticException("Invalid operation");
}
// ready to carry out power calculation...
boolean seenbit = false; // set once we've seen a 1-bit
for (int i=1;;i++) { // for each bit [top bit ignored]
seenbit = true; // OK, we're off
}
if (i == 31)
break; // that was the last bit
if (seenbit)
// else (!seenbit) no point in squaring ONE
}
// if negative n, calculate the reciprocal using working precision
if (n<0) // [hence mc.precision>0]
// round to final precision and strip zeros
}
/**
* 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)}
*/
}
/**
* 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
*/
}
/**
* Returns a {@code BigDecimal} whose value is {@code (-this)},
* and whose scale is {@code this.scale()}.
*
* @return {@code -this}.
*/
if (intCompact != INFLATED)
else {
}
return result;
}
/**
* 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
*/
}
/**
* 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
*/
return this;
}
/**
* 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
*/
return this;
}
/**
* 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.
*/
public int signum() {
return (intCompact != INFLATED)?
}
/**
* 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}.
*/
public int scale() {
return scale;
}
/**
* 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
*/
public int precision() {
if (result == 0) {
long s = intCompact;
if (s != INFLATED)
result = longDigitLength(s);
else
}
return result;
}
/**
* 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
*/
return this.inflate();
}
// Rounding Modes
/**
* Rounding mode to round away from zero. Always increments the
* digit prior to a nonzero discarded fraction. Note that this rounding
* mode never decreases the magnitude of the calculated value.
*/
/**
* Rounding mode to round towards zero. Never increments the digit
* prior to a discarded fraction (i.e., truncates). Note that this
* rounding mode never increases the magnitude of the calculated value.
*/
/**
* Rounding mode to round towards positive infinity. If the
* {@code BigDecimal} is positive, behaves as for
* {@code ROUND_UP}; if negative, behaves as for
* {@code ROUND_DOWN}. Note that this rounding mode never
* decreases the calculated value.
*/
/**
* Rounding mode to round towards negative infinity. If the
* {@code BigDecimal} is positive, behave as for
* {@code ROUND_DOWN}; if negative, behave as for
* {@code ROUND_UP}. Note that this rounding mode never
* increases the calculated value.
*/
/**
* Rounding mode to round towards {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case round up.
* Behaves as for {@code ROUND_UP} if the discarded fraction is
* ≥ 0.5; otherwise, behaves as for {@code ROUND_DOWN}. Note
* that this is the rounding mode that most of us were taught in
* grade school.
*/
/**
* Rounding mode to round towards {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case round
* down. Behaves as for {@code ROUND_UP} if the discarded
* fraction is {@literal >} 0.5; otherwise, behaves as for
* {@code ROUND_DOWN}.
*/
/**
* Rounding mode to round towards the {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case, round
* towards the even neighbor. Behaves as for
* {@code ROUND_HALF_UP} if the digit to the left of the
* discarded fraction is odd; behaves as for
* {@code ROUND_HALF_DOWN} if it's even. Note that this is the
* rounding mode that minimizes cumulative error when applied
* repeatedly over a sequence of calculations.
*/
/**
* Rounding mode to assert that the requested operation has an exact
* result, hence no rounding is necessary. If this rounding mode is
* specified on an operation that yields an inexact result, an
* {@code ArithmeticException} is thrown.
*/
/**
* 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
*/
}
/**
* 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
*/
}
/**
* 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
*/
throw new IllegalArgumentException("Invalid rounding mode");
return this;
long rs = this.intCompact;
} else {
// newScale < oldScale -- drop some digits
// Can't predict the precision due to the effect of rounding.
else
}
}
/**
* 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)
*/
}
// Decimal Point Motion Operations
/**
* 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 ×
* 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.
*/
// Cannot use movePointRight(-n) in case of n==Integer.MIN_VALUE
}
/**
* 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
* × 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.
*/
// Cannot use movePointLeft(-n) in case of n==Integer.MIN_VALUE
}
/**
* 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)}.
*
* @throws ArithmeticException if the scale would be
* outside the range of a 32-bit integer.
*
* @since 1.5
*/
}
/**
* 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]
*
* @return a numerically equal {@code BigDecimal} with any
* trailing zeros removed.
* @since 1.5
*/
this.inflate();
return result;
}
// Comparison Operations
/**
* 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)} <<i>op</i>> {@code 0)}, where
* <<i>op</i>> 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}.
*/
// Quick path for equal scale and non-inflated case.
long xs = intCompact;
}
if (xsign == 0)
return 0;
}
/**
* Version of compareTo that ignores sign.
*/
// Match scales, avoid unnecessary inflation
long xs = this.intCompact;
if (xs == 0)
if (ys == 0)
return 1;
if (sdiff != 0) {
// Avoid matching scales if the (adjusted) exponents differ
return -1;
return 1;
if (sdiff < 0) {
}
} else { // sdiff > 0
}
}
}
return 1;
else
}
/**
* Compares this {@code BigDecimal} with the specified
* {@code Object} for equality. Unlike {@link
* #compareTo(BigDecimal) compareTo}, this method considers two
* {@code BigDecimal} objects equal only if they are equal in
* value and scale (thus 2.0 is not equal to 2.00 when compared by
* this method).
*
* @param x {@code Object} to which this {@code BigDecimal} is
* to be compared.
* @return {@code true} if and only if the specified {@code Object} is a
* {@code BigDecimal} whose value and scale are equal to this
* {@code BigDecimal}'s.
* @see #compareTo(java.math.BigDecimal)
* @see #hashCode
*/
if (!(x instanceof BigDecimal))
return false;
if (x == this)
return true;
return false;
long s = this.intCompact;
if (s != INFLATED) {
return xs == s;
}
/**
* 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)
*/
}
/**
* 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)
*/
}
// Hash Function
/**
* Returns the hash code for this {@code BigDecimal}. Note that
* two {@code BigDecimal} objects that are numerically equal but
* differ in scale (like 2.0 and 2.00) will generally <i>not</i>
* have the same hash code.
*
* @return hash code for this {@code BigDecimal}.
* @see #equals(Object)
*/
public int hashCode() {
if (intCompact != INFLATED) {
} else
}
// Format Converters
/**
* Returns the string representation of this {@code BigDecimal},
* using scientific notation if an exponent is needed.
*
* <p>A standard canonical string form of the {@code BigDecimal}
* is created as though by the following steps: first, the
* absolute value of the unscaled value of the {@code BigDecimal}
* is converted to a string in base ten using the characters
* {@code '0'} through {@code '9'} with no leading zeros (except
* if its value is zero, in which case a single {@code '0'}
* character is used).
*
* <p>Next, an <i>adjusted exponent</i> is calculated; this is the
* negated scale, plus the number of characters in the converted
* unscaled value, less one. That is,
* {@code -scale+(ulength-1)}, where {@code ulength} is the
* length of the absolute value of the unscaled value in decimal
* digits (its <i>precision</i>).
*
* <p>If the scale is greater than or equal to zero and the
* adjusted exponent is greater than or equal to {@code -6}, the
* number will be converted to a character form without using
* exponential notation. In this case, if the scale is zero then
* no decimal point is added and if the scale is positive a
* decimal point will be inserted with the scale specifying the
* number of characters to the right of the decimal point.
* {@code '0'} characters are added to the left of the converted
* unscaled value as necessary. If no character precedes the
* decimal point after this insertion then a conventional
* {@code '0'} character is prefixed.
*
* <p>Otherwise (that is, if the scale is negative, or the
* adjusted exponent is less than {@code -6}), the number will be
* converted to a character form using exponential notation. In
* this case, if the converted {@code BigInteger} has more than
* one digit a decimal point is inserted after the first digit.
* An exponent in character form is then suffixed to the converted
* unscaled value (perhaps with inserted decimal point); this
* comprises the letter {@code 'E'} followed immediately by the
* adjusted exponent converted to a character form. The latter is
* in base ten, using the characters {@code '0'} through
* {@code '9'} with no leading zeros, and is always prefixed by a
* sign character {@code '-'} (<tt>'\u002D'</tt>) if the
* adjusted exponent is negative, {@code '+'}
* (<tt>'\u002B'</tt>) otherwise).
*
* <p>Finally, the entire string is prefixed by a minus sign
* character {@code '-'} (<tt>'\u002D'</tt>) if the unscaled
* value is less than zero. No sign character is prefixed if the
* unscaled value is zero or positive.
*
* <p><b>Examples:</b>
* <p>For each representation [<i>unscaled value</i>, <i>scale</i>]
* on the left, the resulting string is shown on the right.
* <pre>
* [123,0] "123"
* [-123,0] "-123"
* [123,-1] "1.23E+3"
* [123,-3] "1.23E+5"
* [123,1] "12.3"
* [123,5] "0.00123"
* [123,10] "1.23E-8"
* [-123,12] "-1.23E-10"
* </pre>
*
* <b>Notes:</b>
* <ol>
*
* <li>There is a one-to-one mapping between the distinguishable
* {@code BigDecimal} values and the result of this conversion.
* That is, every distinguishable {@code BigDecimal} value
* (unscaled value and scale) has a unique string representation
* as a result of using {@code toString}. If that string
* representation is converted back to a {@code BigDecimal} using
* the {@link #BigDecimal(String)} constructor, then the original
* value will be recovered.
*
* <li>The string produced for a given number is always the same;
* it is not affected by locale. This means that it can be used
* as a canonical string representation for exchanging decimal
* data, or as a key for a Hashtable, etc. Locale-sensitive
* number formatting and parsing is handled by the {@link
* java.text.NumberFormat} class and its subclasses.
*
* <li>The {@link #toEngineeringString} method may be used for
* presenting numbers with exponents in engineering notation, and the
* {@link #setScale(int,RoundingMode) setScale} method may be used for
* rounding a {@code BigDecimal} so it has a known number of digits after
* the decimal point.
*
* <li>The digit-to-character mapping provided by
* {@code Character.forDigit} is used.
*
* </ol>
*
* @return string representation of this {@code BigDecimal}.
* @see Character#forDigit
* @see #BigDecimal(java.lang.String)
*/
return sc;
}
/**
* 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
*/
return layoutChars(false);
}
/**
* 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>'\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 bd = this;
}
/* Returns a digit.digit string */
/* Insert decimal point */
if (signum < 0)
} else { /* We must insert zeros between point and intVal */
for (int i=0; i<-insertionPoint; i++)
}
}
/**
* 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™ 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}.
*/
// force to an integer, quietly
}
/**
* 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
*/
// round to an integer, with Exception if decimal part non-0
}
/**
* 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™ 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}.
*/
public long longValue(){
toBigInteger().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
*/
public long longValueExact() {
return intCompact;
// If more than 19 digits in integer part it cannot possibly fit
// Fastpath zero and < 1.0 numbers (the latter can be very slow
// to round if very small)
if (this.signum() == 0)
return 0;
throw new ArithmeticException("Rounding necessary");
// round to an integer, with Exception if decimal part non-0
}
private static class LongOverflow {
/** BigInteger equal to Long.MIN_VALUE. */
/** BigInteger equal to Long.MAX_VALUE. */
}
}
/**
* 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™ 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}.
*/
public int intValue() {
(int)intCompact :
toBigInteger().intValue();
}
/**
* 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
*/
public int intValueExact() {
long num;
return (int)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
*/
public short shortValueExact() {
long num;
return (short)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
*/
public byte byteValueExact() {
long num;
return (byte)num;
}
/**
* 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™ 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}.
*/
public float floatValue(){
return (float)intCompact;
// Somewhat inefficient, but guaranteed to work.
}
/**
* 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™ 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}.
*/
public double doubleValue(){
return (double)intCompact;
// Somewhat inefficient, but guaranteed to work.
}
/**
* 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
*/
}
// Private class to build a string representation for BigDecimal object.
// "StringBuilderHelper" is constructed as a thread local variable so it is
// thread safe. The StringBuilder field acts as a buffer to hold the temporary
// representation of BigDecimal. The cmpCharArray holds all the characters for
// the compact representation of BigDecimal (except for '-' sign' if it is
// negative) if its intCompact field is not INFLATED. It is shared by all
// calls to toString() and its variants in that particular thread.
static class StringBuilderHelper {
sb = new StringBuilder();
// All non negative longs can be made to fit into 19 character array.
cmpCharArray = new char[19];
}
// Accessors.
return sb;
}
char[] getCompactCharArray() {
return cmpCharArray;
}
/**
* 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.
*/
assert intCompact >= 0;
long q;
int r;
// since we start from the least significant digit, charPos points to
// the last character in cmpCharArray.
q = intCompact / 100;
r = (int)(intCompact - q * 100);
intCompact = q;
}
int q2;
int i2 = (int)intCompact;
while (i2 >= 100) {
}
if (i2 >= 10)
return charPos;
}
final static char[] DIGIT_TENS = {
'0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
'1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
'3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
'4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
'5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
'6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
'7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
'8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
'9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
};
final static char[] DIGIT_ONES = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
};
}
/**
* 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}
*/
return (intCompact != INFLATED) ?
char[] coeff;
int offset; // offset is the starting index for coeff array
// Get the significand as an absolute value
if (intCompact != INFLATED) {
} else {
offset = 0;
}
// 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"
}
} else { // xx.xx form
}
} else { // E-notation is needed
if (sci) { // Scientific notation
}
} else { // Engineering notation
if (sig < 0)
sig++;
if (signum() == 0) {
switch (sig) {
case 1:
break;
case 2:
adjusted += 3;
break;
case 3:
adjusted += 3;
break;
default:
}
// may need some zeros, too
} else { // xx.xxE form
}
}
}
}
}
/**
* 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>)
*/
if (n < 0)
return BigInteger.ZERO;
if (n < BIG_TEN_POWERS_TABLE_MAX) {
return pows[n];
else
return expandBigIntegerTenPowers(n);
}
// BigInteger.pow is slow, so make 10**n by constructing a
// BigInteger from a character string (still not very fast)
char tenpow[] = new char[n + 1];
for (int i = 1; i <= n; i++)
tenpow[i] = '0';
return new BigInteger(tenpow);
}
/**
* Expand the BIG_TEN_POWERS_TABLE array to contain at least 10**n.
*
* @param n the power of ten to be returned (>=0)
* @return a {@code BigDecimal} with the value (10<sup>n</sup>) and
* in the meantime, the BIG_TEN_POWERS_TABLE array gets
* expanded to the size greater than n.
*/
synchronized(BigDecimal.class) {
// The following comparison and the above synchronized statement is
// to prevent multiple threads from expanding the same array.
if (curLen <= n) {
while (newLen <= n)
newLen <<= 1;
// Based on the following facts:
// 1. pows is a private local varible;
// 2. the following store is a volatile store.
// the newly created array elements can be safely published.
}
return pows[n];
}
}
private static final long[] LONG_TEN_POWERS_TABLE = {
1, // 0 / 10^0
10, // 1 / 10^1
100, // 2 / 10^2
1000, // 3 / 10^3
10000, // 4 / 10^4
100000, // 5 / 10^5
1000000, // 6 / 10^6
10000000, // 7 / 10^7
100000000, // 8 / 10^8
1000000000, // 9 / 10^9
10000000000L, // 10 / 10^10
100000000000L, // 11 / 10^11
1000000000000L, // 12 / 10^12
10000000000000L, // 13 / 10^13
100000000000000L, // 14 / 10^14
1000000000000000L, // 15 / 10^15
10000000000000000L, // 16 / 10^16
100000000000000000L, // 17 / 10^17
1000000000000000000L // 18 / 10^18
};
};
private static final int BIG_TEN_POWERS_TABLE_INITLEN =
private static final int BIG_TEN_POWERS_TABLE_MAX =
private static final long THRESHOLDS_TABLE[] = {
};
/**
* Compute val * 10 ^ n; return this product if it is
* representable as a long, INFLATED otherwise.
*/
return val;
long[] tab = LONG_TEN_POWERS_TABLE;
long[] bounds = THRESHOLDS_TABLE;
if (val == 1)
return tenpower;
}
return INFLATED;
}
/**
* Compute this * 10 ^ n.
* Needed mainly to allow special casing to trap zero value
*/
if (n <= 0)
return this.inflate();
if (intCompact != INFLATED)
else
}
/**
* Assign appropriate BigInteger to intVal field if intVal is
* null, i.e. the compact representation is in use.
*/
return intVal;
}
/**
* Match the scales of two {@code BigDecimal}s to align their
* least significant digits.
*
* <p>If the scales of val[0] and val[1] differ, rescale
* (non-destructively) the lower-scaled {@code BigDecimal} so
* they match. That is, the lower-scaled reference will be
* replaced by a reference to a new object with the same scale as
* the other {@code BigDecimal}.
*
* @param val array of two elements referring to the two
* {@code BigDecimal}s to be aligned.
*/
return;
}
}
/**
* Reconstitute the {@code BigDecimal} instance from a stream (that is,
* deserialize it).
*
* @param s the stream being read.
*/
// Read in all fields
s.defaultReadObject();
// validate possibly bad fields
// [all values of scale are now allowed]
}
}
/**
* Serialize this {@code BigDecimal} to the stream in question
*
* @param s the stream to serialize to.
*/
// Must inflate to maintain compatible serial form.
this.inflate();
// Write proper fields
s.defaultWriteObject();
}
/**
* 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.
*/
private static int longDigitLength(long x) {
/*
* As described in "Bit Twiddling Hacks" by Sean Anderson,
* 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 != INFLATED;
if (x < 0)
x = -x;
if (x < 10) // must screen for 0, might as well 10
return 1;
int n = 64; // not 63, to avoid needing to add 1 later
int y = (int)(x >>> 32);
if (y == 0) { n -= 32; y = (int)x; }
if (y >>> 16 == 0) { n -= 16; y <<= 16; }
if (y >>> 24 == 0) { n -= 8; y <<= 8; }
if (y >>> 28 == 0) { n -= 4; y <<= 4; }
if (y >>> 30 == 0) { n -= 2; y <<= 2; }
int r = (((y >>> 31) + n) * 1233) >>> 12;
long[] tab = LONG_TEN_POWERS_TABLE;
// if r >= length, must have max possible digits for long
}
/**
* 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
*/
/*
* 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;
}
/**
* Remove insignificant trailing zeros from this
* {@code BigDecimal} until the preferred scale is reached or no
* more zeros can be removed. If the preferred scale is less than
* Integer.MIN_VALUE, all the trailing zeros will be removed.
*
* {@code BigInteger} assistance could help, here?
*
* <p>WARNING: This method should only be called on new objects as
* it mutates the value fields.
*
* @return this {@code BigDecimal} with a scale possibly reduced
* to be closed to the preferred scale.
*/
this.inflate();
scale > preferredScale) {
break; // odd number cannot end in 0
break; // non-0 remainder
precision--;
}
return this;
}
/**
* 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.
*/
BigInteger b;
if (intCompact != 0 &&
}
return asInt;
}
/**
* Round an operand; used only if digits > 0. Does not change
* {@code this}; if rounding is needed a new {@code BigDecimal}
* is created and returned.
*
* @param mc the context to use.
* @throws ArithmeticException if the result is inexact but the
* rounding mode is {@code UNNECESSARY}.
*/
return rounded;
}
/** Round this BigDecimal according to the MathContext settings;
* used only if precision {@literal >} 0.
*
* <p>WARNING: This method should only be called on new objects as
* it mutates the value fields.
*
* @param mc the context to use.
* @throws ArithmeticException if the rounding mode is
* {@code RoundingMode.UNNECESSARY} and the
* {@code BigDecimal} operation would require rounding.
*/
if (rounded == this) // wasn't rounded
return;
}
/**
* Returns a {@code BigDecimal} rounded according to the
* MathContext settings; used only if {@code mc.precision > 0}.
* Does not change {@code this}; if rounding is needed a new
* {@code BigDecimal} is created and returned.
*
* @param mc the context to use.
* @return a {@code BigDecimal} rounded according to the MathContext
* settings. May return this, if no rounding needed.
* @throws ArithmeticException if the rounding mode is
* {@code RoundingMode.UNNECESSARY} and the
* result is inexact.
*/
int drop;
// This might (rarely) iterate to cover the 999=>1000 case
else
}
return d;
}
/**
* Returns the compact value for given {@code BigInteger}, or
* INFLATED if too big. Relies on internal representation of
* {@code BigInteger}.
*/
int[] m = b.mag;
if (len == 0)
return 0;
int d = m[0];
return INFLATED;
long u = (len == 2)?
(((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;
}
/*
* Internal printing routine
*/
name,
}
/**
* 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.
*/
if (intCompact == INFLATED) {
print("audit", this);
throw new AssertionError("null intVal");
}
// Check precision
print("audit", this);
throw new AssertionError("precision mismatch");
}
} else {
if (val != intCompact) {
print("audit", this);
throw new AssertionError("Inconsistent state, intCompact=" +
}
}
// Check precision
print("audit", this);
throw new AssertionError("precision mismatch");
}
}
return this;
}
}