jdk/src/share/classes/java/math/BigDecimal.java
author xlu
Sun, 24 May 2009 16:29:57 -0700
changeset 2922 dd6d609861f0
parent 2 90ce3da70b43
child 3053 a42e2cc2aaa5
permissions -rw-r--r--
6622432: RFE: Performance improvements to java.math.BigDecimal Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Portions Copyright 1996-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * Portions Copyright IBM Corporation, 2001. All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
package java.math;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
    32
import java.util.Arrays;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
    33
import static java.math.BigInteger.LONG_MASK;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
    34
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Immutable, arbitrary-precision signed decimal numbers.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * {@code BigDecimal} consists of an arbitrary precision integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <i>unscaled value</i> and a 32-bit integer <i>scale</i>.  If zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * or positive, the scale is the number of digits to the right of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * decimal point.  If negative, the unscaled value of the number is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * multiplied by ten to the power of the negation of the scale.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * value of the number represented by the {@code BigDecimal} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * therefore <tt>(unscaledValue &times; 10<sup>-scale</sup>)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>The {@code BigDecimal} class provides operations for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * arithmetic, scale manipulation, rounding, comparison, hashing, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * format conversion.  The {@link #toString} method provides a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * canonical representation of a {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>The {@code BigDecimal} class gives its user complete control
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * over rounding behavior.  If no rounding mode is specified and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * exact result cannot be represented, an exception is thrown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * otherwise, calculations can be carried out to a chosen precision
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * and rounding mode by supplying an appropriate {@link MathContext}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * object to the operation.  In either case, eight <em>rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * modes</em> are provided for the control of rounding.  Using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * integer fields in this class (such as {@link #ROUND_HALF_UP}) to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * represent rounding mode is largely obsolete; the enumeration values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * of the {@code RoundingMode} {@code enum}, (such as {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * RoundingMode#HALF_UP}) should be used instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p>When a {@code MathContext} object is supplied with a precision
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * setting of 0 (for example, {@link MathContext#UNLIMITED}),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * arithmetic operations are exact, as are the arithmetic methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * which take no {@code MathContext} object.  (This is the only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * behavior that was supported in releases prior to 5.)  As a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * corollary of computing the exact result, the rounding mode setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * of a {@code MathContext} object with a precision setting of 0 is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * not used and thus irrelevant.  In the case of divide, the exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * quotient could have an infinitely long decimal expansion; for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * example, 1 divided by 3.  If the quotient has a nonterminating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * decimal expansion and the operation is specified to return an exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * result, an {@code ArithmeticException} is thrown.  Otherwise, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * exact result of the division is returned, as done for other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <p>When the precision setting is not 0, the rules of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * {@code BigDecimal} arithmetic are broadly compatible with selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * modes of operation of the arithmetic defined in ANSI X3.274-1996
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * and ANSI X3.274-1996/AM 1-2000 (section 7.4).  Unlike those
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * standards, {@code BigDecimal} includes many rounding modes, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * were mandatory for division in {@code BigDecimal} releases prior
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * to 5.  Any conflicts between these ANSI standards and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * {@code BigDecimal} specification are resolved in favor of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <p>Since the same numerical value can have different
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * representations (with different scales), the rules of arithmetic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * and rounding must specify both the numerical result and the scale
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * used in the result's representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <p>In general the rounding modes and precision setting determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * how operations return results with a limited number of digits when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * the exact result has more digits (perhaps infinitely many in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * case of division) than the number of digits returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * First, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * total number of digits to return is specified by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * {@code MathContext}'s {@code precision} setting; this determines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * the result's <i>precision</i>.  The digit count starts from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * leftmost nonzero digit of the exact result.  The rounding mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * determines how any discarded trailing digits affect the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * <p>For all arithmetic operators , the operation is carried out as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * though an exact intermediate result were first calculated and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * rounded to the number of digits specified by the precision setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * (if necessary), using the selected rounding mode.  If the exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * result is not returned, some digit positions of the exact result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * are discarded.  When rounding increases the magnitude of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * returned result, it is possible for a new digit position to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * created by a carry propagating to a leading {@literal "9"} digit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * For example, rounding the value 999.9 to three digits rounding up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * would be numerically equal to one thousand, represented as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * 100&times;10<sup>1</sup>.  In such cases, the new {@literal "1"} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * the leading digit position of the returned result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <p>Besides a logical exact result, each arithmetic operation has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * preferred scale for representing a result.  The preferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * scale for each operation is listed in the table below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * <table border>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * <caption top><h3>Preferred Scales for Results of Arithmetic Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * </h3></caption>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * <tr><th>Operation</th><th>Preferred Scale of Result</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * <tr><td>Add</td><td>max(addend.scale(), augend.scale())</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * <tr><td>Subtract</td><td>max(minuend.scale(), subtrahend.scale())</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * <tr><td>Multiply</td><td>multiplier.scale() + multiplicand.scale()</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * <tr><td>Divide</td><td>dividend.scale() - divisor.scale()</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * These scales are the ones used by the methods which return exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * arithmetic results; except that an exact divide may have to use a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * larger scale since the exact result may have more digits.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * example, {@code 1/32} is {@code 0.03125}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * <p>Before rounding, the scale of the logical exact intermediate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * result is the preferred scale for that operation.  If the exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * numerical result cannot be represented in {@code precision}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * digits, rounding selects the set of digits to return and the scale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * of the result is reduced from the scale of the intermediate result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * to the least scale which can represent the {@code precision}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * digits actually returned.  If the exact result can be represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * with at most {@code precision} digits, the representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * of the result with the scale closest to the preferred scale is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * returned.  In particular, an exactly representable quotient may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * represented in fewer than {@code precision} digits by removing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * trailing zeros and decreasing the scale.  For example, rounding to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * three digits using the {@linkplain RoundingMode#FLOOR floor}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * rounding mode, <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * {@code 19/100 = 0.19   // integer=19,  scale=2} <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * but<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * {@code 21/110 = 0.190  // integer=190, scale=3} <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * <p>Note that for add, subtract, and multiply, the reduction in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * scale will equal the number of digit positions of the exact result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * which are discarded. If the rounding causes a carry propagation to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * create a new high-order digit position, an additional digit of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * result is discarded than when no new digit position is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * <p>Other methods may have slightly different rounding semantics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * For example, the result of the {@code pow} method using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * {@linkplain #pow(int, MathContext) specified algorithm} can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * occasionally differ from the rounded mathematical result by more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * than one unit in the last place, one <i>{@linkplain #ulp() ulp}</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * <p>Two types of operations are provided for manipulating the scale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * of a {@code BigDecimal}: scaling/rounding operations and decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * point motion operations.  Scaling/rounding operations ({@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * #setScale setScale} and {@link #round round}) return a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * {@code BigDecimal} whose value is approximately (or exactly) equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * to that of the operand, but whose scale or precision is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * specified value; that is, they increase or decrease the precision
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * of the stored number with minimal effect on its value.  Decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * point motion operations ({@link #movePointLeft movePointLeft} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 * {@link #movePointRight movePointRight}) return a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * {@code BigDecimal} created from the operand by moving the decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 * point a specified distance in the specified direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * <p>For the sake of brevity and clarity, pseudo-code is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * throughout the descriptions of {@code BigDecimal} methods.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 * pseudo-code expression {@code (i + j)} is shorthand for "a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 * {@code BigDecimal} whose value is that of the {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 * {@code i} added to that of the {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 * {@code j}." The pseudo-code expression {@code (i == j)} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 * shorthand for "{@code true} if and only if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 * {@code BigDecimal} {@code i} represents the same value as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 * {@code BigDecimal} {@code j}." Other pseudo-code expressions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 * are interpreted similarly.  Square brackets are used to represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 * the particular {@code BigInteger} and scale pair defining a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 * {@code BigDecimal} value; for example [19, 2] is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 * {@code BigDecimal} numerically equal to 0.19 having a scale of 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 * <p>Note: care should be exercised if {@code BigDecimal} objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 * are used as keys in a {@link java.util.SortedMap SortedMap} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 * elements in a {@link java.util.SortedSet SortedSet} since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 * {@code BigDecimal}'s <i>natural ordering</i> is <i>inconsistent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 * with equals</i>.  See {@link Comparable}, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * java.util.SortedMap} or {@link java.util.SortedSet} for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * <p>All methods and constructors for this class throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 * {@code NullPointerException} when passed a {@code null} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 * reference for any input parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 * @see     BigInteger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 * @see     MathContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 * @see     RoundingMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 * @see     java.util.SortedMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * @see     java.util.SortedSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * @author  Mike Cowlishaw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 * @author  Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
public class BigDecimal extends Number implements Comparable<BigDecimal> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * The unscaled value of this BigDecimal, as returned by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * #unscaledValue}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @see #unscaledValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    private volatile BigInteger intVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * The scale of this BigDecimal, as returned by {@link #scale}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @see #scale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   235
    private int scale;  // Note: this may have any value, so
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   236
                        // calculations must be done in longs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * The number of decimal digits in this BigDecimal, or 0 if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * number of digits are not known (lookaside information).  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * nonzero, the value is guaranteed correct.  Use the precision()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * method to obtain and set the value if it might be 0.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * field is mutable until set nonzero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   246
    private transient int precision;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Used to store the canonical string representation, if computed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   251
    private transient String stringCache;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Sentinel value for {@link #intCompact} indicating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * significand information is only available from {@code intVal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   257
    static final long INFLATED = Long.MIN_VALUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * If the absolute value of the significand of this BigDecimal is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * less than or equal to {@code Long.MAX_VALUE}, the value can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * compactly stored in this field and used in computations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   264
    private transient long intCompact;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    // All 18-digit base ten strings fit into a long; not all 19-digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    // strings will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    private static final int MAX_COMPACT_DIGITS = 18;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private static final int MAX_BIGINT_BITS = 62;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /* Appease the serialization gods */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    private static final long serialVersionUID = 6108874887143696463L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   275
    private static final ThreadLocal<StringBuilderHelper>
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   276
        threadLocalStringBuilderHelper = new ThreadLocal<StringBuilderHelper>() {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   277
        @Override
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   278
        protected StringBuilderHelper initialValue() {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   279
            return new StringBuilderHelper();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   280
        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   281
    };
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   282
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    // Cache of common small BigDecimal values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    private static final BigDecimal zeroThroughTen[] = {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   285
        new BigDecimal(BigInteger.ZERO,         0,  0, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   286
        new BigDecimal(BigInteger.ONE,          1,  0, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   287
        new BigDecimal(BigInteger.valueOf(2),   2,  0, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   288
        new BigDecimal(BigInteger.valueOf(3),   3,  0, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   289
        new BigDecimal(BigInteger.valueOf(4),   4,  0, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   290
        new BigDecimal(BigInteger.valueOf(5),   5,  0, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   291
        new BigDecimal(BigInteger.valueOf(6),   6,  0, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   292
        new BigDecimal(BigInteger.valueOf(7),   7,  0, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   293
        new BigDecimal(BigInteger.valueOf(8),   8,  0, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   294
        new BigDecimal(BigInteger.valueOf(9),   9,  0, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   295
        new BigDecimal(BigInteger.TEN,          10, 0, 2),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   296
    };
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   297
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   298
    // Cache of zero scaled by 0 - 15
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   299
    private static final BigDecimal[] ZERO_SCALED_BY = {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   300
        zeroThroughTen[0],
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   301
        new BigDecimal(BigInteger.ZERO, 0, 1, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   302
        new BigDecimal(BigInteger.ZERO, 0, 2, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   303
        new BigDecimal(BigInteger.ZERO, 0, 3, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   304
        new BigDecimal(BigInteger.ZERO, 0, 4, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   305
        new BigDecimal(BigInteger.ZERO, 0, 5, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   306
        new BigDecimal(BigInteger.ZERO, 0, 6, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   307
        new BigDecimal(BigInteger.ZERO, 0, 7, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   308
        new BigDecimal(BigInteger.ZERO, 0, 8, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   309
        new BigDecimal(BigInteger.ZERO, 0, 9, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   310
        new BigDecimal(BigInteger.ZERO, 0, 10, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   311
        new BigDecimal(BigInteger.ZERO, 0, 11, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   312
        new BigDecimal(BigInteger.ZERO, 0, 12, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   313
        new BigDecimal(BigInteger.ZERO, 0, 13, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   314
        new BigDecimal(BigInteger.ZERO, 0, 14, 1),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   315
        new BigDecimal(BigInteger.ZERO, 0, 15, 1),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    // Constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * The value 0, with a scale of 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public static final BigDecimal ZERO =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        zeroThroughTen[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * The value 1, with a scale of 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    public static final BigDecimal ONE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        zeroThroughTen[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * The value 10, with a scale of 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public static final BigDecimal TEN =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        zeroThroughTen[10];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    // Constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   346
     * Trusted package private constructor.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   347
     * Trusted simply means if val is INFLATED, intVal could not be null and
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   348
     * if intVal is null, val could not be INFLATED.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   349
     */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   350
    BigDecimal(BigInteger intVal, long val, int scale, int prec) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   351
        this.scale = scale;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   352
        this.precision = prec;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   353
        this.intCompact = val;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   354
        this.intVal = intVal;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   355
    }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   356
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   357
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Translates a character array representation of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * {@code BigDecimal} into a {@code BigDecimal}, accepting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * same sequence of characters as the {@link #BigDecimal(String)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * constructor, while allowing a sub-array to be specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * <p>Note that if the sequence of characters is already available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * within a character array, using this constructor is faster than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * converting the {@code char} array to string and using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * {@code BigDecimal(String)} constructor .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param  in {@code char} array that is the source of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @param  offset first character in the array to inspect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @param  len number of characters to consider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @throws NumberFormatException if {@code in} is not a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *         representation of a {@code BigDecimal} or the defined subarray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *         is not wholly within {@code in}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    public BigDecimal(char[] in, int offset, int len) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   377
        // protect against huge length.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   378
        if (offset+len > in.length || offset < 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   379
            throw new NumberFormatException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        // This is the primary string to BigDecimal constructor; all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        // incoming strings end up here; it uses explicit (inline)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        // parsing for speed and generates at most one intermediate
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   383
        // (temporary) object (a char[] array) for non-compact case.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   384
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   385
        // Use locals for all fields values until completion
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   386
        int prec = 0;                 // record precision value
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   387
        int scl = 0;                  // record scale value
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   388
        long rs = 0;                  // the compact value in long
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   389
        BigInteger rb = null;         // the inflated value in BigInteger
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        // use array bounds checking to handle too-long, len == 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        // bad offset, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            // handle the sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            boolean isneg = false;          // assume positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            if (in[offset] == '-') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                isneg = true;               // leading minus means negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                offset++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                len--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            } else if (in[offset] == '+') { // leading + allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                offset++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                len--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            // should now be at numeric part of the significand
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   406
            boolean dot = false;             // true when there is a '.'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            int cfirst = offset;             // record start of integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            long exp = 0;                    // exponent
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   409
            char c;                          // current character
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   410
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   411
            boolean isCompact = (len <= MAX_COMPACT_DIGITS);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   412
            // integer significand array & idx is the index to it. The array
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   413
            // is ONLY used when we can't use a compact representation.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   414
            char coeff[] = isCompact ? null : new char[len];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   415
            int idx = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            for (; len > 0; offset++, len--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                c = in[offset];
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   419
                // have digit
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                if ((c >= '0' && c <= '9') || Character.isDigit(c)) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   421
                    // First compact case, we need not to preserve the character
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   422
                    // and we can just compute the value in place.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   423
                    if (isCompact) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   424
                        int digit = Character.digit(c, 10);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   425
                        if (digit == 0) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   426
                            if (prec == 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   427
                                prec = 1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   428
                            else if (rs != 0) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   429
                                rs *= 10;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   430
                                ++prec;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   431
                            } // else digit is a redundant leading zero
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   432
                        } else {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   433
                            if (prec != 1 || rs != 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   434
                                ++prec; // prec unchanged if preceded by 0s
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   435
                            rs = rs * 10 + digit;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   436
                        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   437
                    } else { // the unscaled value is likely a BigInteger object.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   438
                        if (c == '0' || Character.digit(c, 10) == 0) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   439
                            if (prec == 0) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   440
                                coeff[idx] = c;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   441
                                prec = 1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   442
                            } else if (idx != 0) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   443
                                coeff[idx++] = c;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   444
                                ++prec;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   445
                            } // else c must be a redundant leading zero
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   446
                        } else {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   447
                            if (prec != 1 || idx != 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   448
                                ++prec; // prec unchanged if preceded by 0s
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   449
                            coeff[idx++] = c;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   450
                        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   451
                    }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   452
                    if (dot)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   453
                        ++scl;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   456
                // have dot
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                if (c == '.') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    // have dot
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   459
                    if (dot)         // two dots
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                        throw new NumberFormatException();
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   461
                    dot = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                // exponent expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                if ((c != 'e') && (c != 'E'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    throw new NumberFormatException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                offset++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                c = in[offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                len--;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   470
                boolean negexp = (c == '-');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                // optional sign
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   472
                if (negexp || c == '+') {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    offset++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    c = in[offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    len--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                if (len <= 0)    // no exponent digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                    throw new NumberFormatException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                // skip leading zeros in the exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                while (len > 10 && Character.digit(c, 10) == 0) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   481
                    offset++;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   482
                    c = in[offset];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   483
                    len--;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                if (len > 10)  // too many nonzero exponent digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                    throw new NumberFormatException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                // c now holds first digit of exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                for (;; len--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                    int v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                    if (c >= '0' && c <= '9') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                        v = c - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                        v = Character.digit(c, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                        if (v < 0)            // not a digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                            throw new NumberFormatException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    exp = exp * 10 + v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    if (len == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                        break;               // that was final character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    offset++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    c = in[offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                if (negexp)                  // apply sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                    exp = -exp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                // Next test is required for backwards compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                if ((int)exp != exp)         // overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    throw new NumberFormatException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                break;                       // [saves a test]
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   509
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            // here when no characters left
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   511
            if (prec == 0)              // no digits found
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                throw new NumberFormatException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   514
            // Adjust scale if exp is not zero.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            if (exp != 0) {                  // had significant exponent
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   516
                // Can't call checkScale which relies on proper fields value
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   517
                long adjustedScale = scl - exp;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   518
                if (adjustedScale > Integer.MAX_VALUE ||
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   519
                    adjustedScale < Integer.MIN_VALUE)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                    throw new NumberFormatException("Scale out of range.");
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   521
                scl = (int)adjustedScale;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            // Remove leading zeros from precision (digits count)
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   525
            if (isCompact) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   526
                rs = isneg ? -rs : rs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            } else {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   528
                char quick[];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   529
                if (!isneg) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   530
                    quick = (coeff.length != prec) ?
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   531
                        Arrays.copyOf(coeff, prec) : coeff;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   532
                } else {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   533
                    quick = new char[prec + 1];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   534
                    quick[0] = '-';
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   535
                    System.arraycopy(coeff, 0, quick, 1, prec);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   536
                }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   537
                rb = new BigInteger(quick);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   538
                rs = compactValFor(rb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        } catch (ArrayIndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            throw new NumberFormatException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        } catch (NegativeArraySizeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            throw new NumberFormatException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   545
        this.scale = scl;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   546
        this.precision = prec;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   547
        this.intCompact = rs;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   548
        this.intVal = (rs != INFLATED) ? null : rb;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * Translates a character array representation of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * {@code BigDecimal} into a {@code BigDecimal}, accepting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * same sequence of characters as the {@link #BigDecimal(String)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * constructor, while allowing a sub-array to be specified and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * with rounding according to the context settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * <p>Note that if the sequence of characters is already available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * within a character array, using this constructor is faster than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * converting the {@code char} array to string and using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * {@code BigDecimal(String)} constructor .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @param  in {@code char} array that is the source of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @param  offset first character in the array to inspect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @param  len number of characters to consider..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * @throws NumberFormatException if {@code in} is not a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *         representation of a {@code BigDecimal} or the defined subarray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *         is not wholly within {@code in}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    public BigDecimal(char[] in, int offset, int len, MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        this(in, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        if (mc.precision > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            roundThis(mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * Translates a character array representation of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * {@code BigDecimal} into a {@code BigDecimal}, accepting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * same sequence of characters as the {@link #BigDecimal(String)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * <p>Note that if the sequence of characters is already available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * as a character array, using this constructor is faster than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * converting the {@code char} array to string and using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * {@code BigDecimal(String)} constructor .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @param in {@code char} array that is the source of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @throws NumberFormatException if {@code in} is not a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *         representation of a {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    public BigDecimal(char[] in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        this(in, 0, in.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * Translates a character array representation of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * {@code BigDecimal} into a {@code BigDecimal}, accepting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * same sequence of characters as the {@link #BigDecimal(String)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * constructor and with rounding according to the context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * <p>Note that if the sequence of characters is already available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * as a character array, using this constructor is faster than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * converting the {@code char} array to string and using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * {@code BigDecimal(String)} constructor .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * @param  in {@code char} array that is the source of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * @throws NumberFormatException if {@code in} is not a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *         representation of a {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    public BigDecimal(char[] in, MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        this(in, 0, in.length, mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * Translates the string representation of a {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * into a {@code BigDecimal}.  The string representation consists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * of an optional sign, {@code '+'} (<tt> '&#92;u002B'</tt>) or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * {@code '-'} (<tt>'&#92;u002D'</tt>), followed by a sequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * zero or more decimal digits ("the integer"), optionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * followed by a fraction, optionally followed by an exponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * <p>The fraction consists of a decimal point followed by zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * or more decimal digits.  The string must contain at least one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * digit in either the integer or the fraction.  The number formed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * by the sign, the integer and the fraction is referred to as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * <i>significand</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * <p>The exponent consists of the character {@code 'e'}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * (<tt>'&#92;u0065'</tt>) or {@code 'E'} (<tt>'&#92;u0045'</tt>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * followed by one or more decimal digits.  The value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * exponent must lie between -{@link Integer#MAX_VALUE} ({@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * Integer#MIN_VALUE}+1) and {@link Integer#MAX_VALUE}, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * <p>More formally, the strings this constructor accepts are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * described by the following grammar:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * <dt><i>BigDecimalString:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * <dd><i>Sign<sub>opt</sub> Significand Exponent<sub>opt</sub></i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * <dt><i>Sign:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * <dd>{@code +}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * <dd>{@code -}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * <dt><i>Significand:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * <dd><i>IntegerPart</i> {@code .} <i>FractionPart<sub>opt</sub></i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * <dd>{@code .} <i>FractionPart</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * <dd><i>IntegerPart</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * <dt><i>IntegerPart:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * <dd>Digits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * <dt><i>FractionPart:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * <dd>Digits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * <dt><i>Exponent:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * <dd>ExponentIndicator SignedInteger</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * <dt><i>ExponentIndicator:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * <dd>{@code e}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * <dd>{@code E}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * <dt><i>SignedInteger:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * <dd>Sign<sub>opt</sub> Digits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * <dt><i>Digits:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * <dd>Digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * <dd>Digits Digit</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * <dt><i>Digit:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * <dd>any character for which {@link Character#isDigit}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * returns {@code true}, including 0, 1, 2 ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * <p>The scale of the returned {@code BigDecimal} will be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * number of digits in the fraction, or zero if the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * contains no decimal point, subject to adjustment for any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * exponent; if the string contains an exponent, the exponent is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * subtracted from the scale.  The value of the resulting scale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * must lie between {@code Integer.MIN_VALUE} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * {@code Integer.MAX_VALUE}, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * <p>The character-to-digit mapping is provided by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * java.lang.Character#digit} set to convert to radix 10.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * String may not contain any extraneous characters (whitespace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * for example).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * <p><b>Examples:</b><br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * The value of the returned {@code BigDecimal} is equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * <i>significand</i> &times; 10<sup>&nbsp;<i>exponent</i></sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * For each string on the left, the resulting representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * [{@code BigInteger}, {@code scale}] is shown on the right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * "0"            [0,0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * "0.00"         [0,2]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * "123"          [123,0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * "-123"         [-123,0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * "1.23E3"       [123,-1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * "1.23E+3"      [123,-1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * "12.3E+7"      [123,-6]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * "12.0"         [120,1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * "12.3"         [123,1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * "0.00123"      [123,5]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * "-1.23E-12"    [-123,14]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * "1234.5E-4"    [12345,5]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * "0E+7"         [0,-7]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * "-0"           [0,0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * <p>Note: For values other than {@code float} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * {@code double} NaN and &plusmn;Infinity, this constructor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * compatible with the values returned by {@link Float#toString}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * and {@link Double#toString}.  This is generally the preferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * way to convert a {@code float} or {@code double} into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * BigDecimal, as it doesn't suffer from the unpredictability of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * the {@link #BigDecimal(double)} constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * @param val String representation of {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * @throws NumberFormatException if {@code val} is not a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     *         representation of a {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    public BigDecimal(String val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        this(val.toCharArray(), 0, val.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * Translates the string representation of a {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * into a {@code BigDecimal}, accepting the same strings as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * {@link #BigDecimal(String)} constructor, with rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * according to the context settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * @param  val string representation of a {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * @throws NumberFormatException if {@code val} is not a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     *         representation of a BigDecimal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    public BigDecimal(String val, MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        this(val.toCharArray(), 0, val.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        if (mc.precision > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            roundThis(mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * Translates a {@code double} into a {@code BigDecimal} which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * is the exact decimal representation of the {@code double}'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * binary floating-point value.  The scale of the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * {@code BigDecimal} is the smallest value such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * <tt>(10<sup>scale</sup> &times; val)</tt> is an integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * <b>Notes:</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * The results of this constructor can be somewhat unpredictable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * One might assume that writing {@code new BigDecimal(0.1)} in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * Java creates a {@code BigDecimal} which is exactly equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * 0.1 (an unscaled value of 1, with a scale of 1), but it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * actually equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * 0.1000000000000000055511151231257827021181583404541015625.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * This is because 0.1 cannot be represented exactly as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * {@code double} (or, for that matter, as a binary fraction of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * any finite length).  Thus, the value that is being passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * <i>in</i> to the constructor is not exactly equal to 0.1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * appearances notwithstanding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * The {@code String} constructor, on the other hand, is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * perfectly predictable: writing {@code new BigDecimal("0.1")}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * creates a {@code BigDecimal} which is <i>exactly</i> equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * 0.1, as one would expect.  Therefore, it is generally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * recommended that the {@linkplain #BigDecimal(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * <tt>String</tt> constructor} be used in preference to this one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * When a {@code double} must be used as a source for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * {@code BigDecimal}, note that this constructor provides an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * exact conversion; it does not give the same result as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * converting the {@code double} to a {@code String} using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * {@link Double#toString(double)} method and then using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * {@link #BigDecimal(String)} constructor.  To get that result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * use the {@code static} {@link #valueOf(double)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * @param val {@code double} value to be converted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     *        {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * @throws NumberFormatException if {@code val} is infinite or NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    public BigDecimal(double val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        if (Double.isInfinite(val) || Double.isNaN(val))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            throw new NumberFormatException("Infinite or NaN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        // Translate the double into sign, exponent and significand, according
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        // to the formulae in JLS, Section 20.10.22.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        long valBits = Double.doubleToLongBits(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        int sign = ((valBits >> 63)==0 ? 1 : -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        int exponent = (int) ((valBits >> 52) & 0x7ffL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        long significand = (exponent==0 ? (valBits & ((1L<<52) - 1)) << 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                            : (valBits & ((1L<<52) - 1)) | (1L<<52));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        exponent -= 1075;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        // At this point, val == sign * significand * 2**exponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
         * Special case zero to supress nonterminating normalization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
         * and bogus scale calculation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        if (significand == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            intVal = BigInteger.ZERO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            intCompact = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            precision = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        // Normalize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        while((significand & 1) == 0) {    //  i.e., significand is even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            significand >>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            exponent++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        // Calculate intVal and scale
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   834
        long s = sign * significand;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   835
        BigInteger b;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        if (exponent < 0) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   837
            b = BigInteger.valueOf(5).pow(-exponent).multiply(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            scale = -exponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        } else if (exponent > 0) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   840
            b = BigInteger.valueOf(2).pow(exponent).multiply(s);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   841
        } else {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   842
            b = BigInteger.valueOf(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   844
        intCompact = compactValFor(b);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   845
        intVal = (intCompact != INFLATED) ? null : b;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * Translates a {@code double} into a {@code BigDecimal}, with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * rounding according to the context settings.  The scale of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * {@code BigDecimal} is the smallest value such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * <tt>(10<sup>scale</sup> &times; val)</tt> is an integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * <p>The results of this constructor can be somewhat unpredictable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * and its use is generally not recommended; see the notes under
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * the {@link #BigDecimal(double)} constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * @param  val {@code double} value to be converted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     *         {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     *         RoundingMode is UNNECESSARY.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * @throws NumberFormatException if {@code val} is infinite or NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    public BigDecimal(double val, MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        this(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        if (mc.precision > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            roundThis(mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * Translates a {@code BigInteger} into a {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * The scale of the {@code BigDecimal} is zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * @param val {@code BigInteger} value to be converted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     *            {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    public BigDecimal(BigInteger val) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   880
        intCompact = compactValFor(val);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   881
        intVal = (intCompact != INFLATED) ? null : val;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * Translates a {@code BigInteger} into a {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * rounding according to the context settings.  The scale of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * {@code BigDecimal} is zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * @param val {@code BigInteger} value to be converted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     *            {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    public BigDecimal(BigInteger val, MathContext mc) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   897
        this(val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        if (mc.precision > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            roundThis(mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * Translates a {@code BigInteger} unscaled value and an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * {@code int} scale into a {@code BigDecimal}.  The value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * the {@code BigDecimal} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * <tt>(unscaledVal &times; 10<sup>-scale</sup>)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * @param unscaledVal unscaled value of the {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * @param scale scale of the {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    public BigDecimal(BigInteger unscaledVal, int scale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        // Negative scales are now allowed
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   913
        this(unscaledVal);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        this.scale = scale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * Translates a {@code BigInteger} unscaled value and an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * {@code int} scale into a {@code BigDecimal}, with rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * according to the context settings.  The value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * {@code BigDecimal} is <tt>(unscaledVal &times;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * 10<sup>-scale</sup>)</tt>, rounded according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * {@code precision} and rounding mode settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * @param  unscaledVal unscaled value of the {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * @param  scale scale of the {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    public BigDecimal(BigInteger unscaledVal, int scale, MathContext mc) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   933
        this(unscaledVal);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        this.scale = scale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        if (mc.precision > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            roundThis(mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * Translates an {@code int} into a {@code BigDecimal}.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * scale of the {@code BigDecimal} is zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * @param val {@code int} value to be converted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     *            {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    public BigDecimal(int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        intCompact = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * Translates an {@code int} into a {@code BigDecimal}, with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * rounding according to the context settings.  The scale of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * {@code BigDecimal}, before any rounding, is zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * @param  val {@code int} value to be converted to {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    public BigDecimal(int val, MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        intCompact = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        if (mc.precision > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
            roundThis(mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * Translates a {@code long} into a {@code BigDecimal}.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * scale of the {@code BigDecimal} is zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * @param val {@code long} value to be converted to {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    public BigDecimal(long val) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   976
        this.intCompact = val;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   977
        this.intVal = (val == INFLATED) ? BigInteger.valueOf(val) : null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * Translates a {@code long} into a {@code BigDecimal}, with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * rounding according to the context settings.  The scale of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * {@code BigDecimal}, before any rounding, is zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * @param  val {@code long} value to be converted to {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    public BigDecimal(long val, MathContext mc) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   992
        this(val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        if (mc.precision > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            roundThis(mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    // Static Factory Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * Translates a {@code long} unscaled value and an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * {@code int} scale into a {@code BigDecimal}.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * {@literal "static factory method"} is provided in preference to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * a ({@code long}, {@code int}) constructor because it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * allows for reuse of frequently used {@code BigDecimal} values..
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * @param unscaledVal unscaled value of the {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * @param scale scale of the {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * @return a {@code BigDecimal} whose value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     *         <tt>(unscaledVal &times; 10<sup>-scale</sup>)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    public static BigDecimal valueOf(long unscaledVal, int scale) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1012
        if (scale == 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1013
            return valueOf(unscaledVal);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1014
        else if (unscaledVal == 0) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1015
            if (scale > 0 && scale < ZERO_SCALED_BY.length)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1016
                return ZERO_SCALED_BY[scale];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1017
            else
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1018
                return new BigDecimal(BigInteger.ZERO, 0, scale, 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1020
        return new BigDecimal(unscaledVal == INFLATED ?
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1021
                              BigInteger.valueOf(unscaledVal) : null,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1022
                              unscaledVal, scale, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * Translates a {@code long} value into a {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * with a scale of zero.  This {@literal "static factory method"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * is provided in preference to a ({@code long}) constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * because it allows for reuse of frequently used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * {@code BigDecimal} values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * @param val value of the {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * @return a {@code BigDecimal} whose value is {@code val}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    public static BigDecimal valueOf(long val) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1036
        if (val >= 0 && val < zeroThroughTen.length)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1037
            return zeroThroughTen[(int)val];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1038
        else if (val != INFLATED)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1039
            return new BigDecimal(null, val, 0, 0);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1040
        return new BigDecimal(BigInteger.valueOf(val), val, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * Translates a {@code double} into a {@code BigDecimal}, using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * the {@code double}'s canonical string representation provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * by the {@link Double#toString(double)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * <p><b>Note:</b> This is generally the preferred way to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * a {@code double} (or {@code float}) into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * {@code BigDecimal}, as the value returned is equal to that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * resulting from constructing a {@code BigDecimal} from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * result of using {@link Double#toString(double)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * @param  val {@code double} to convert to a {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * @return a {@code BigDecimal} whose value is equal to or approximately
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     *         equal to the value of {@code val}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * @throws NumberFormatException if {@code val} is infinite or NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    public static BigDecimal valueOf(double val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        // Reminder: a zero double returns '0.0', so we cannot fastpath
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        // to use the constant ZERO.  This might be important enough to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        // justify a factory approach, a cache, or a few private
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        // constants, later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        return new BigDecimal(Double.toString(val));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    // Arithmetic Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * Returns a {@code BigDecimal} whose value is {@code (this +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * augend)}, and whose scale is {@code max(this.scale(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * augend.scale())}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * @param  augend value to be added to this {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * @return {@code this + augend}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    public BigDecimal add(BigDecimal augend) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1078
        long xs = this.intCompact;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1079
        long ys = augend.intCompact;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1080
        BigInteger fst = (xs != INFLATED) ? null : this.intVal;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1081
        BigInteger snd = (ys != INFLATED) ? null : augend.intVal;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1082
        int rscale = this.scale;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1084
        long sdiff = (long)rscale - augend.scale;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1085
        if (sdiff != 0) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1086
            if (sdiff < 0) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1087
                int raise = checkScale(-sdiff);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1088
                rscale = augend.scale;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1089
                if (xs == INFLATED ||
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1090
                    (xs = longMultiplyPowerTen(xs, raise)) == INFLATED)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1091
                    fst = bigMultiplyPowerTen(raise);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1092
            } else {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1093
                int raise = augend.checkScale(sdiff);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1094
                if (ys == INFLATED ||
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1095
                    (ys = longMultiplyPowerTen(ys, raise)) == INFLATED)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1096
                    snd = augend.bigMultiplyPowerTen(raise);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1097
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1099
        if (xs != INFLATED && ys != INFLATED) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1100
            long sum = xs + ys;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1101
            // See "Hacker's Delight" section 2-12 for explanation of
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1102
            // the overflow test.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1103
            if ( (((sum ^ xs) & (sum ^ ys))) >= 0L) // not overflowed
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1104
                return new BigDecimal(null, sum, rscale, 0);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1105
        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1106
        if (fst == null)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1107
            fst = BigInteger.valueOf(xs);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1108
        if (snd == null)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1109
            snd = BigInteger.valueOf(ys);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1110
        BigInteger sum = fst.add(snd);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1111
        return (fst.signum == snd.signum) ?
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1112
            new BigDecimal(sum, INFLATED, rscale, 0) :
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1113
            new BigDecimal(sum, rscale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * Returns a {@code BigDecimal} whose value is {@code (this + augend)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * with rounding according to the context settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * If either number is zero and the precision setting is nonzero then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * the other number, rounded if necessary, is used as the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * @param  augend value to be added to this {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * @return {@code this + augend}, rounded as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    public BigDecimal add(BigDecimal augend, MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        if (mc.precision == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            return add(augend);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        BigDecimal lhs = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        // Could optimize if values are compact
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        this.inflate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        augend.inflate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        // If either number is zero then the other number, rounded and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        // scaled if necessary, is used as the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            boolean lhsIsZero = lhs.signum() == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            boolean augendIsZero = augend.signum() == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            if (lhsIsZero || augendIsZero) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                int preferredScale = Math.max(lhs.scale(), augend.scale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                BigDecimal result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                // Could use a factory for zero instead of a new object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                if (lhsIsZero && augendIsZero)
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1151
                    return new BigDecimal(BigInteger.ZERO, 0, preferredScale, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1153
                result = lhsIsZero ? doRound(augend, mc) : doRound(lhs, mc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                if (result.scale() == preferredScale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                    return result;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1157
                else if (result.scale() > preferredScale) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1158
                    BigDecimal scaledResult =
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1159
                        new BigDecimal(result.intVal, result.intCompact,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1160
                                       result.scale, 0);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1161
                    scaledResult.stripZerosToMatchScale(preferredScale);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1162
                    return scaledResult;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1163
                } else { // result.scale < preferredScale
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                    int precisionDiff = mc.precision - result.precision();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                    int scaleDiff     = preferredScale - result.scale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                    if (precisionDiff >= scaleDiff)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                        return result.setScale(preferredScale); // can achieve target scale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                        return result.setScale(result.scale() + precisionDiff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        long padding = (long)lhs.scale - augend.scale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        if (padding != 0) {        // scales differ; alignment needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            BigDecimal arg[] = preAlign(lhs, augend, padding, mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            matchScale(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            lhs    = arg[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            augend = arg[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1183
        BigDecimal d = new BigDecimal(lhs.inflate().add(augend.inflate()),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1184
                                      lhs.scale);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1185
        return doRound(d, mc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * Returns an array of length two, the sum of whose entries is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * equal to the rounded sum of the {@code BigDecimal} arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * <p>If the digit positions of the arguments have a sufficient
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * gap between them, the value smaller in magnitude can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * condensed into a {@literal "sticky bit"} and the end result will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * round the same way <em>if</em> the precision of the final
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * result does not include the high order digit of the small
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * magnitude operand.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     * <p>Note that while strictly speaking this is an optimization,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * it makes a much wider range of additions practical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * <p>This corresponds to a pre-shift operation in a fixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * precision floating-point adder; this method is complicated by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * variable precision of the result as determined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * MathContext.  A more nuanced operation could implement a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * {@literal "right shift"} on the smaller magnitude operand so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * that the number of digits of the smaller operand could be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * reduced even though the significands partially overlapped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
    private BigDecimal[] preAlign(BigDecimal lhs, BigDecimal augend,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                                  long padding, MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        assert padding != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        BigDecimal big;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        BigDecimal small;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        if (padding < 0) {     // lhs is big;   augend is small
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            big   = lhs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            small = augend;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        } else {               // lhs is small; augend is big
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            big   = augend;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            small = lhs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
         * This is the estimated scale of an ulp of the result; it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
         * assumes that the result doesn't have a carry-out on a true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
         * add (e.g. 999 + 1 => 1000) or any subtractive cancellation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
         * on borrowing (e.g. 100 - 1.2 => 98.8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        long estResultUlpScale = (long)big.scale - big.precision() + mc.precision;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
         * The low-order digit position of big is big.scale().  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
         * is true regardless of whether big has a positive or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
         * negative scale.  The high-order digit position of small is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
         * small.scale - (small.precision() - 1).  To do the full
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
         * condensation, the digit positions of big and small must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
         * disjoint *and* the digit positions of small should not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
         * directly visible in the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        long smallHighDigitPos = (long)small.scale - small.precision() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        if (smallHighDigitPos > big.scale + 2 &&         // big and small disjoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            smallHighDigitPos > estResultUlpScale + 2) { // small digits not visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            small = BigDecimal.valueOf(small.signum(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                                       this.checkScale(Math.max(big.scale, estResultUlpScale) + 3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        // Since addition is symmetric, preserving input order in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        // returned operands doesn't matter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        BigDecimal[] result = {big, small};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * Returns a {@code BigDecimal} whose value is {@code (this -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * subtrahend)}, and whose scale is {@code max(this.scale(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * subtrahend.scale())}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * @param  subtrahend value to be subtracted from this {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * @return {@code this - subtrahend}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    public BigDecimal subtract(BigDecimal subtrahend) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1263
        return add(subtrahend.negate());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * Returns a {@code BigDecimal} whose value is {@code (this - subtrahend)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * with rounding according to the context settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * If {@code subtrahend} is zero then this, rounded if necessary, is used as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * result.  If this is zero then the result is {@code subtrahend.negate(mc)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * @param  subtrahend value to be subtracted from this {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * @return {@code this - subtrahend}, rounded as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    public BigDecimal subtract(BigDecimal subtrahend, MathContext mc) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1281
        BigDecimal nsubtrahend = subtrahend.negate();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        if (mc.precision == 0)
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1283
            return add(nsubtrahend);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        // share the special rounding code in add()
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1285
        return add(nsubtrahend, mc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     * Returns a {@code BigDecimal} whose value is <tt>(this &times;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     * multiplicand)</tt>, and whose scale is {@code (this.scale() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     * multiplicand.scale())}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * @param  multiplicand value to be multiplied by this {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     * @return {@code this * multiplicand}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
    public BigDecimal multiply(BigDecimal multiplicand) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        long x = this.intCompact;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        long y = multiplicand.intCompact;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1299
        int productScale = checkScale((long)scale + multiplicand.scale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        // Might be able to do a more clever check incorporating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        // inflated check into the overflow computation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        if (x != INFLATED && y != INFLATED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
             * If the product is not an overflowed value, continue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
             * to use the compact representation.  if either of x or y
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
             * is INFLATED, the product should also be regarded as
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1308
             * an overflow. Before using the overflow test suggested in
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1309
             * "Hacker's Delight" section 2-12, we perform quick checks
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1310
             * using the precision information to see whether the overflow
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1311
             * would occur since division is expensive on most CPUs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            long product = x * y;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1314
            int prec = this.precision() + multiplicand.precision();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1315
            if (prec < 19 || (prec < 21 && (y == 0 || product / y == x)))
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1316
                return new BigDecimal(null, product, productScale, 0);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1317
            return new BigDecimal(BigInteger.valueOf(x).multiply(y), INFLATED,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1318
                                  productScale, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1320
        BigInteger rb;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1321
        if (x == INFLATED && y == INFLATED)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1322
            rb = this.intVal.multiply(multiplicand.intVal);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1323
        else if (x != INFLATED)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1324
            rb = multiplicand.intVal.multiply(x);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1325
        else
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1326
            rb = this.intVal.multiply(y);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1327
        return new BigDecimal(rb, INFLATED, productScale, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * Returns a {@code BigDecimal} whose value is <tt>(this &times;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     * multiplicand)</tt>, with rounding according to the context settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * @param  multiplicand value to be multiplied by this {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * @return {@code this * multiplicand}, rounded as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
    public BigDecimal multiply(BigDecimal multiplicand, MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        if (mc.precision == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            return multiply(multiplicand);
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1344
        return doRound(this.multiply(multiplicand), mc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     * Returns a {@code BigDecimal} whose value is {@code (this /
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     * divisor)}, and whose scale is as specified.  If rounding must
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     * be performed to generate a result with the specified scale, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     * specified rounding mode is applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     * <p>The new {@link #divide(BigDecimal, int, RoundingMode)} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     * should be used in preference to this legacy method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * @param  divisor value by which this {@code BigDecimal} is to be divided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     * @param  scale scale of the {@code BigDecimal} quotient to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * @param  roundingMode rounding mode to apply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     * @return {@code this / divisor}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     * @throws ArithmeticException if {@code divisor} is zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     *         {@code roundingMode==ROUND_UNNECESSARY} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     *         the specified scale is insufficient to represent the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     *         of the division exactly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     * @throws IllegalArgumentException if {@code roundingMode} does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     *         represent a valid rounding mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     * @see    #ROUND_UP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * @see    #ROUND_DOWN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * @see    #ROUND_CEILING
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     * @see    #ROUND_FLOOR
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * @see    #ROUND_HALF_UP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * @see    #ROUND_HALF_DOWN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * @see    #ROUND_HALF_EVEN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     * @see    #ROUND_UNNECESSARY
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
         * IMPLEMENTATION NOTE: This method *must* return a new object
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1378
         * since divideAndRound uses divide to generate a value whose
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
         * scale is then modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        if (roundingMode < ROUND_UP || roundingMode > ROUND_UNNECESSARY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            throw new IllegalArgumentException("Invalid rounding mode");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
         * Rescale dividend or divisor (whichever can be "upscaled" to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
         * produce correctly scaled quotient).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
         * Take care to detect out-of-range scales
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
         */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1388
        BigDecimal dividend = this;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1389
        if (checkScale((long)scale + divisor.scale) > this.scale)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1390
            dividend = this.setScale(scale + divisor.scale, ROUND_UNNECESSARY);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1391
        else
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1392
            divisor = divisor.setScale(checkScale((long)this.scale - scale),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1393
                                       ROUND_UNNECESSARY);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1394
        return divideAndRound(dividend.intCompact, dividend.intVal,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1395
                              divisor.intCompact, divisor.intVal,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1396
                              scale, roundingMode, scale);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1397
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1399
    /**
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1400
     * Internally used for division operation. The dividend and divisor are
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1401
     * passed both in {@code long} format and {@code BigInteger} format. The
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1402
     * returned {@code BigDecimal} object is the quotient whose scale is set to
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1403
     * the passed in scale. If the remainder is not zero, it will be rounded
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1404
     * based on the passed in roundingMode. Also, if the remainder is zero and
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1405
     * the last parameter, i.e. preferredScale is NOT equal to scale, the
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1406
     * trailing zeros of the result is stripped to match the preferredScale.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1407
     */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1408
    private static BigDecimal divideAndRound(long ldividend, BigInteger bdividend,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1409
                                             long ldivisor,  BigInteger bdivisor,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1410
                                             int scale, int roundingMode,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1411
                                             int preferredScale) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1412
        boolean isRemainderZero;       // record remainder is zero or not
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1413
        int qsign;                     // quotient sign
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1414
        long q = 0, r = 0;             // store quotient & remainder in long
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1415
        MutableBigInteger mq = null;   // store quotient
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1416
        MutableBigInteger mr = null;   // store remainder
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1417
        MutableBigInteger mdivisor = null;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1418
        boolean isLongDivision = (ldividend != INFLATED && ldivisor != INFLATED);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1419
        if (isLongDivision) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1420
            q = ldividend / ldivisor;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1421
            if (roundingMode == ROUND_DOWN && scale == preferredScale)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1422
                return new BigDecimal(null, q, scale, 0);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1423
            r = ldividend % ldivisor;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1424
            isRemainderZero = (r == 0);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1425
            qsign = ((ldividend < 0) == (ldivisor < 0)) ? 1 : -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        } else {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1427
            if (bdividend == null)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1428
                bdividend = BigInteger.valueOf(ldividend);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1429
            // Descend into mutables for faster remainder checks
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1430
            MutableBigInteger mdividend = new MutableBigInteger(bdividend.mag);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1431
            mq = new MutableBigInteger();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1432
            if (ldivisor != INFLATED) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1433
                r = mdividend.divide(ldivisor, mq);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1434
                isRemainderZero = (r == 0);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1435
                qsign = (ldivisor < 0) ? -bdividend.signum : bdividend.signum;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1436
            } else {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1437
                mdivisor = new MutableBigInteger(bdivisor.mag);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1438
                mr = mdividend.divide(mdivisor, mq);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1439
                isRemainderZero = mr.isZero();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1440
                qsign = (bdividend.signum != bdivisor.signum) ? -1 : 1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1441
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1443
        boolean increment = false;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1444
        if (!isRemainderZero) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            int cmpFracHalf;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1446
            /* Round as appropriate */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1447
            if (roundingMode == ROUND_UNNECESSARY) {  // Rounding prohibited
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1448
                throw new ArithmeticException("Rounding necessary");
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1449
            } else if (roundingMode == ROUND_UP) {      // Away from zero
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1450
                increment = true;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1451
            } else if (roundingMode == ROUND_DOWN) {    // Towards zero
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1452
                increment = false;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1453
            } else if (roundingMode == ROUND_CEILING) { // Towards +infinity
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1454
                increment = (qsign > 0);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1455
            } else if (roundingMode == ROUND_FLOOR) {   // Towards -infinity
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1456
                increment = (qsign < 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
            } else {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1458
                if (isLongDivision || ldivisor != INFLATED)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1459
                    cmpFracHalf = longCompareMagnitude(2 * r, ldivisor);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1460
                else
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1461
                    cmpFracHalf = mr.compareHalf(mdivisor);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1462
                if (cmpFracHalf < 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1463
                    increment = false;     // We're closer to higher digit
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1464
                else if (cmpFracHalf > 0)  // We're closer to lower digit
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1465
                    increment = true;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1466
                else if (roundingMode == ROUND_HALF_UP)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                    increment = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                else if (roundingMode == ROUND_HALF_DOWN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                    increment = false;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1470
                else  // roundingMode == ROUND_HALF_EVEN, true iff quotient is odd
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1471
                    increment = isLongDivision ? (q & 1L) != 0L : mq.isOdd();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1474
        BigDecimal res;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1475
        if (isLongDivision)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1476
            res = new BigDecimal(null, (increment ? q + qsign : q), scale, 0);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1477
        else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
            if (increment)
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1479
                mq.add(MutableBigInteger.ONE);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1480
            res = mq.toBigDecimal(qsign, scale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
        }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1482
        if (isRemainderZero && preferredScale != scale)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1483
            res.stripZerosToMatchScale(preferredScale);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1484
        return res;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     * Returns a {@code BigDecimal} whose value is {@code (this /
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     * divisor)}, and whose scale is as specified.  If rounding must
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     * be performed to generate a result with the specified scale, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
     * specified rounding mode is applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     * @param  divisor value by which this {@code BigDecimal} is to be divided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
     * @param  scale scale of the {@code BigDecimal} quotient to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
     * @param  roundingMode rounding mode to apply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     * @return {@code this / divisor}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * @throws ArithmeticException if {@code divisor} is zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     *         {@code roundingMode==RoundingMode.UNNECESSARY} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     *         the specified scale is insufficient to represent the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     *         of the division exactly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
    public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        return divide(divisor, scale, roundingMode.oldMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
     * Returns a {@code BigDecimal} whose value is {@code (this /
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
     * divisor)}, and whose scale is {@code this.scale()}.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
     * rounding must be performed to generate a result with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
     * scale, the specified rounding mode is applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
     * <p>The new {@link #divide(BigDecimal, RoundingMode)} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     * should be used in preference to this legacy method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     * @param  divisor value by which this {@code BigDecimal} is to be divided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     * @param  roundingMode rounding mode to apply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     * @return {@code this / divisor}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     * @throws ArithmeticException if {@code divisor==0}, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     *         {@code roundingMode==ROUND_UNNECESSARY} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     *         {@code this.scale()} is insufficient to represent the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     *         of the division exactly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     * @throws IllegalArgumentException if {@code roundingMode} does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
     *         represent a valid rounding mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     * @see    #ROUND_UP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     * @see    #ROUND_DOWN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
     * @see    #ROUND_CEILING
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
     * @see    #ROUND_FLOOR
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
     * @see    #ROUND_HALF_UP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     * @see    #ROUND_HALF_DOWN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     * @see    #ROUND_HALF_EVEN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     * @see    #ROUND_UNNECESSARY
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
    public BigDecimal divide(BigDecimal divisor, int roundingMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
            return this.divide(divisor, scale, roundingMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     * Returns a {@code BigDecimal} whose value is {@code (this /
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     * divisor)}, and whose scale is {@code this.scale()}.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     * rounding must be performed to generate a result with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     * scale, the specified rounding mode is applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     * @param  divisor value by which this {@code BigDecimal} is to be divided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     * @param  roundingMode rounding mode to apply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
     * @return {@code this / divisor}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     * @throws ArithmeticException if {@code divisor==0}, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
     *         {@code roundingMode==RoundingMode.UNNECESSARY} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     *         {@code this.scale()} is insufficient to represent the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     *         of the division exactly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
    public BigDecimal divide(BigDecimal divisor, RoundingMode roundingMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        return this.divide(divisor, scale, roundingMode.oldMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     * Returns a {@code BigDecimal} whose value is {@code (this /
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     * divisor)}, and whose preferred scale is {@code (this.scale() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     * divisor.scale())}; if the exact quotient cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     * represented (because it has a non-terminating decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
     * expansion) an {@code ArithmeticException} is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     * @param  divisor value by which this {@code BigDecimal} is to be divided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * @throws ArithmeticException if the exact quotient does not have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     *         terminating decimal expansion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     * @return {@code this / divisor}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
    public BigDecimal divide(BigDecimal divisor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
         * Handle zero cases first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        if (divisor.signum() == 0) {   // x/0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
            if (this.signum() == 0)    // 0/0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                throw new ArithmeticException("Division undefined");  // NaN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
            throw new ArithmeticException("Division by zero");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        // Calculate preferred scale
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1582
        int preferredScale = saturateLong((long)this.scale - divisor.scale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        if (this.signum() == 0)        // 0/y
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1584
            return (preferredScale >= 0 &&
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1585
                    preferredScale < ZERO_SCALED_BY.length) ?
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1586
                ZERO_SCALED_BY[preferredScale] :
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1587
                new BigDecimal(null, 0, preferredScale, 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
            this.inflate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
            divisor.inflate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
             * If the quotient this/divisor has a terminating decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
             * expansion, the expansion can have no more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
             * (a.precision() + ceil(10*b.precision)/3) digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
             * Therefore, create a MathContext object with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
             * precision and do a divide with the UNNECESSARY rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
             * mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
            MathContext mc = new MathContext( (int)Math.min(this.precision() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                                                            (long)Math.ceil(10.0*divisor.precision()/3.0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                                                            Integer.MAX_VALUE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
                                              RoundingMode.UNNECESSARY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
            BigDecimal quotient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                quotient = this.divide(divisor, mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
            } catch (ArithmeticException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
                throw new ArithmeticException("Non-terminating decimal expansion; " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
                                              "no exact representable decimal result.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
            int quotientScale = quotient.scale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
            // divide(BigDecimal, mc) tries to adjust the quotient to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            // the desired one by removing trailing zeros; since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
            // exact divide method does not have an explicit digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
            // limit, we can add zeros too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
            if (preferredScale > quotientScale)
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1619
                return quotient.setScale(preferredScale, ROUND_UNNECESSARY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
            return quotient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
     * Returns a {@code BigDecimal} whose value is {@code (this /
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
     * divisor)}, with rounding according to the context settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     * @param  divisor value by which this {@code BigDecimal} is to be divided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
     * @return {@code this / divisor}, rounded as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     *         rounding mode is {@code UNNECESSARY} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     *         {@code mc.precision == 0} and the quotient has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     *         non-terminating decimal expansion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
    public BigDecimal divide(BigDecimal divisor, MathContext mc) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1639
        int mcp = mc.precision;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1640
        if (mcp == 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
            return divide(divisor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1643
        BigDecimal dividend = this;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1644
        long preferredScale = (long)dividend.scale - divisor.scale;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
        // Now calculate the answer.  We use the existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
        // divide-and-round method, but as this rounds to scale we have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        // to normalize the values here to achieve the desired result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
        // For x/y we first handle y=0 and x=0, and then normalize x and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
        // y to give x' and y' with the following constraints:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
        //   (a) 0.1 <= x' < 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        //   (b)  x' <= y' < 10*x'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        // Dividing x'/y' with the required scale set to mc.precision then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        // will give a result in the range 0.1 to 1 rounded to exactly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        // the right number of digits (except in the case of a result of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
        // 1.000... which can arise when x=y, or when rounding overflows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        // The 1.000... case will reduce properly to 1.
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1657
        if (divisor.signum() == 0) {      // x/0
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1658
            if (dividend.signum() == 0)    // 0/0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
                throw new ArithmeticException("Division undefined");  // NaN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
            throw new ArithmeticException("Division by zero");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
        }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1662
        if (dividend.signum() == 0)        // 0/y
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1663
            return new BigDecimal(BigInteger.ZERO, 0,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1664
                                  saturateLong(preferredScale), 1);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1665
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1666
        // Normalize dividend & divisor so that both fall into [0.1, 0.999...]
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1667
        int xscale = dividend.precision();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1668
        int yscale = divisor.precision();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1669
        dividend = new BigDecimal(dividend.intVal, dividend.intCompact,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1670
                                  xscale, xscale);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1671
        divisor = new BigDecimal(divisor.intVal, divisor.intCompact,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1672
                                 yscale, yscale);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1673
        if (dividend.compareMagnitude(divisor) > 0) // satisfy constraint (b)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1674
            yscale = divisor.scale -= 1;            // [that is, divisor *= 10]
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1676
        // In order to find out whether the divide generates the exact result,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1677
        // we avoid calling the above divide method. 'quotient' holds the
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1678
        // return BigDecimal object whose scale will be set to 'scl'.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1679
        BigDecimal quotient;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1680
        int scl = checkScale(preferredScale + yscale - xscale + mcp);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1681
        if (checkScale((long)mcp + yscale) > xscale)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1682
            dividend = dividend.setScale(mcp + yscale, ROUND_UNNECESSARY);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1683
        else
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1684
            divisor = divisor.setScale(checkScale((long)xscale - mcp),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1685
                                       ROUND_UNNECESSARY);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1686
        quotient = divideAndRound(dividend.intCompact, dividend.intVal,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1687
                                  divisor.intCompact, divisor.intVal,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1688
                                  scl, mc.roundingMode.oldMode,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1689
                                  checkScale(preferredScale));
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1690
        // doRound, here, only affects 1000000000 case.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1691
        quotient = doRound(quotient, mc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1693
        return quotient;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
     * Returns a {@code BigDecimal} whose value is the integer part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
     * of the quotient {@code (this / divisor)} rounded down.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
     * preferred scale of the result is {@code (this.scale() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
     * divisor.scale())}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
     * @param  divisor value by which this {@code BigDecimal} is to be divided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
     * @return The integer part of {@code this / divisor}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     * @throws ArithmeticException if {@code divisor==0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
    public BigDecimal divideToIntegralValue(BigDecimal divisor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        // Calculate preferred scale
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1709
        int preferredScale = saturateLong((long)this.scale - divisor.scale);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1710
        if (this.compareMagnitude(divisor) < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
            // much faster when this << divisor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
            return BigDecimal.valueOf(0, preferredScale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
        if(this.signum() == 0 && divisor.signum() != 0)
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1716
            return this.setScale(preferredScale, ROUND_UNNECESSARY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
        // Perform a divide with enough digits to round to a correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
        // integer value; then remove any fractional digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
        int maxDigits = (int)Math.min(this.precision() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                                      (long)Math.ceil(10.0*divisor.precision()/3.0) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
                                      Math.abs((long)this.scale() - divisor.scale()) + 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                                      Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
        BigDecimal quotient = this.divide(divisor, new MathContext(maxDigits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                                                                   RoundingMode.DOWN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
        if (quotient.scale > 0) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1728
            quotient = quotient.setScale(0, RoundingMode.DOWN);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1729
            quotient.stripZerosToMatchScale(preferredScale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        if (quotient.scale < preferredScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
            // pad with zeros if necessary
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1734
            quotient = quotient.setScale(preferredScale, ROUND_UNNECESSARY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
        return quotient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
     * Returns a {@code BigDecimal} whose value is the integer part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
     * of {@code (this / divisor)}.  Since the integer part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
     * exact quotient does not depend on the rounding mode, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
     * rounding mode does not affect the values returned by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
     * method.  The preferred scale of the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
     * {@code (this.scale() - divisor.scale())}.  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
     * {@code ArithmeticException} is thrown if the integer part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
     * the exact quotient needs more than {@code mc.precision}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
     * digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
     * @param  divisor value by which this {@code BigDecimal} is to be divided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
     * @return The integer part of {@code this / divisor}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
     * @throws ArithmeticException if {@code divisor==0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
     * @throws ArithmeticException if {@code mc.precision} {@literal >} 0 and the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
     *         requires a precision of more than {@code mc.precision} digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
    public BigDecimal divideToIntegralValue(BigDecimal divisor, MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
        if (mc.precision == 0 ||                        // exact result
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1761
            (this.compareMagnitude(divisor) < 0) )      // zero result
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
            return divideToIntegralValue(divisor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
        // Calculate preferred scale
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1765
        int preferredScale = saturateLong((long)this.scale - divisor.scale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
         * Perform a normal divide to mc.precision digits.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
         * remainder has absolute value less than the divisor, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
         * integer portion of the quotient fits into mc.precision
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
         * digits.  Next, remove any fractional digits from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
         * quotient and adjust the scale to the preferred value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
         */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1774
        BigDecimal result = this.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1775
            divide(divisor, new MathContext(mc.precision, RoundingMode.DOWN));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
        if (result.scale() < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
             * Result is an integer. See if quotient represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
             * full integer portion of the exact quotient; if it does,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
             * the computed remainder will be less than the divisor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
            BigDecimal product = result.multiply(divisor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
            // If the quotient is the full integer value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
            // |dividend-product| < |divisor|.
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1786
            if (this.subtract(product).compareMagnitude(divisor) >= 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
                throw new ArithmeticException("Division impossible");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
        } else if (result.scale() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
             * Integer portion of quotient will fit into precision
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
             * digits; recompute quotient to scale 0 to avoid double
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
             * rounding and then try to adjust, if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
            result = result.setScale(0, RoundingMode.DOWN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        // else result.scale() == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
        int precisionDiff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
        if ((preferredScale > result.scale()) &&
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1801
            (precisionDiff = mc.precision - result.precision()) > 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
            return result.setScale(result.scale() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
                                   Math.min(precisionDiff, preferredScale - result.scale) );
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1804
        } else {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1805
            result.stripZerosToMatchScale(preferredScale);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1806
            return result;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  1807
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
     * Returns a {@code BigDecimal} whose value is {@code (this % divisor)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     * <p>The remainder is given by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     * {@code this.subtract(this.divideToIntegralValue(divisor).multiply(divisor))}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     * Note that this is not the modulo operation (the result can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     * negative).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
     * @param  divisor value by which this {@code BigDecimal} is to be divided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
     * @return {@code this % divisor}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
     * @throws ArithmeticException if {@code divisor==0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
    public BigDecimal remainder(BigDecimal divisor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
        BigDecimal divrem[] = this.divideAndRemainder(divisor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        return divrem[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
     * Returns a {@code BigDecimal} whose value is {@code (this %
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
     * divisor)}, with rounding according to the context settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
     * The {@code MathContext} settings affect the implicit divide
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
     * used to compute the remainder.  The remainder computation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
     * itself is by definition exact.  Therefore, the remainder may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
     * contain more than {@code mc.getPrecision()} digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
     * <p>The remainder is given by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
     * {@code this.subtract(this.divideToIntegralValue(divisor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
     * mc).multiply(divisor))}.  Note that this is not the modulo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
     * operation (the result can be negative).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
     * @param  divisor value by which this {@code BigDecimal} is to be divided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
     * @return {@code this % divisor}, rounded as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
     * @throws ArithmeticException if {@code divisor==0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
     *         rounding mode is {@code UNNECESSARY}, or {@code mc.precision}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
     *         {@literal >} 0 and the result of {@code this.divideToIntgralValue(divisor)} would
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
     *         require a precision of more than {@code mc.precision} digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
     * @see    #divideToIntegralValue(java.math.BigDecimal, java.math.MathContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
    public BigDecimal remainder(BigDecimal divisor, MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
        BigDecimal divrem[] = this.divideAndRemainder(divisor, mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
        return divrem[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
     * Returns a two-element {@code BigDecimal} array containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
     * result of {@code divideToIntegralValue} followed by the result of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
     * {@code remainder} on the two operands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
     * <p>Note that if both the integer quotient and remainder are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
     * needed, this method is faster than using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
     * {@code divideToIntegralValue} and {@code remainder} methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
     * separately because the division need only be carried out once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
     * @param  divisor value by which this {@code BigDecimal} is to be divided,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
     *         and the remainder computed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
     * @return a two element {@code BigDecimal} array: the quotient
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
     *         (the result of {@code divideToIntegralValue}) is the initial element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
     *         and the remainder is the final element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
     * @throws ArithmeticException if {@code divisor==0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
     * @see    #divideToIntegralValue(java.math.BigDecimal, java.math.MathContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
     * @see    #remainder(java.math.BigDecimal, java.math.MathContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
    public BigDecimal[] divideAndRemainder(BigDecimal divisor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
        // we use the identity  x = i * y + r to determine r
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
        BigDecimal[] result = new BigDecimal[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
        result[0] = this.divideToIntegralValue(divisor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
        result[1] = this.subtract(result[0].multiply(divisor));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     * Returns a two-element {@code BigDecimal} array containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     * result of {@code divideToIntegralValue} followed by the result of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     * {@code remainder} on the two operands calculated with rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     * according to the context settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     * <p>Note that if both the integer quotient and remainder are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
     * needed, this method is faster than using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
     * {@code divideToIntegralValue} and {@code remainder} methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
     * separately because the division need only be carried out once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
     * @param  divisor value by which this {@code BigDecimal} is to be divided,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
     *         and the remainder computed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
     * @return a two element {@code BigDecimal} array: the quotient
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
     *         (the result of {@code divideToIntegralValue}) is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
     *         initial element and the remainder is the final element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
     * @throws ArithmeticException if {@code divisor==0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
     *         rounding mode is {@code UNNECESSARY}, or {@code mc.precision}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     *         {@literal >} 0 and the result of {@code this.divideToIntgralValue(divisor)} would
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
     *         require a precision of more than {@code mc.precision} digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
     * @see    #divideToIntegralValue(java.math.BigDecimal, java.math.MathContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
     * @see    #remainder(java.math.BigDecimal, java.math.MathContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
    public BigDecimal[] divideAndRemainder(BigDecimal divisor, MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
        if (mc.precision == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
            return divideAndRemainder(divisor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
        BigDecimal[] result = new BigDecimal[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
        BigDecimal lhs = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
        result[0] = lhs.divideToIntegralValue(divisor, mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
        result[1] = lhs.subtract(result[0].multiply(divisor));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
     * Returns a {@code BigDecimal} whose value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
     * <tt>(this<sup>n</sup>)</tt>, The power is computed exactly, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
     * unlimited precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
     * <p>The parameter {@code n} must be in the range 0 through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
     * 999999999, inclusive.  {@code ZERO.pow(0)} returns {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
     * #ONE}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
     * Note that future releases may expand the allowable exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     * range of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     * @param  n power to raise this {@code BigDecimal} to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
     * @return <tt>this<sup>n</sup></tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     * @throws ArithmeticException if {@code n} is out of range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
    public BigDecimal pow(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
        if (n < 0 || n > 999999999)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
            throw new ArithmeticException("Invalid operation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
        // No need to calculate pow(n) if result will over/underflow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
        // Don't attempt to support "supernormal" numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
        int newScale = checkScale((long)scale * n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
        this.inflate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
        return new BigDecimal(intVal.pow(n), newScale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
     * Returns a {@code BigDecimal} whose value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
     * <tt>(this<sup>n</sup>)</tt>.  The current implementation uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
     * the core algorithm defined in ANSI standard X3.274-1996 with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
     * rounding according to the context settings.  In general, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
     * returned numerical value is within two ulps of the exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
     * numerical value for the chosen precision.  Note that future
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
     * releases may use a different algorithm with a decreased
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
     * allowable error bound and increased allowable exponent range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
     * <p>The X3.274-1996 algorithm is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
     * <li> An {@code ArithmeticException} exception is thrown if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
     *  <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
     *    <li>{@code abs(n) > 999999999}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
     *    <li>{@code mc.precision == 0} and {@code n < 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
     *    <li>{@code mc.precision > 0} and {@code n} has more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
     *    {@code mc.precision} decimal digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
     *  </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
     * <li> if {@code n} is zero, {@link #ONE} is returned even if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
     * {@code this} is zero, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
     *   <li> if {@code n} is positive, the result is calculated via
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
     *   the repeated squaring technique into a single accumulator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
     *   The individual multiplications with the accumulator use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
     *   same math context settings as in {@code mc} except for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
     *   precision increased to {@code mc.precision + elength + 1}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
     *   where {@code elength} is the number of decimal digits in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
     *   {@code n}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
     *   <li> if {@code n} is negative, the result is calculated as if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
     *   {@code n} were positive; this value is then divided into one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
     *   using the working precision specified above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
     *   <li> The final value from either the positive or negative case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
     *   is then rounded to the destination precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
     *   </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
     * @param  n power to raise this {@code BigDecimal} to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
     * @param  mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
     * @return <tt>this<sup>n</sup></tt> using the ANSI standard X3.274-1996
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
     *         algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
     *         rounding mode is {@code UNNECESSARY}, or {@code n} is out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
     *         of range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
    public BigDecimal pow(int n, MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
        if (mc.precision == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
            return pow(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
        if (n < -999999999 || n > 999999999)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
            throw new ArithmeticException("Invalid operation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
        if (n == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
            return ONE;                      // x**0 == 1 in X3.274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
        this.inflate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
        BigDecimal lhs = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
        MathContext workmc = mc;           // working settings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
        int mag = Math.abs(n);               // magnitude of n
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
        if (mc.precision > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2016
            int elength = longDigitLength(mag); // length of n in digits
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
            if (elength > mc.precision)        // X3.274 rule
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
                throw new ArithmeticException("Invalid operation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
            workmc = new MathContext(mc.precision + elength + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                                      mc.roundingMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
        // ready to carry out power calculation...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
        BigDecimal acc = ONE;           // accumulator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
        boolean seenbit = false;        // set once we've seen a 1-bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
        for (int i=1;;i++) {            // for each bit [top bit ignored]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
            mag += mag;                 // shift left 1 bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
            if (mag < 0) {              // top bit is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
                seenbit = true;         // OK, we're off
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
                acc = acc.multiply(lhs, workmc); // acc=acc*x
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
            if (i == 31)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
                break;                  // that was the last bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
            if (seenbit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
                acc=acc.multiply(acc, workmc);   // acc=acc*acc [square]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
                // else (!seenbit) no point in squaring ONE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
        // if negative n, calculate the reciprocal using working precision
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
        if (n<0)                          // [hence mc.precision>0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
            acc=ONE.divide(acc, workmc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
        // round to final precision and strip zeros
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2041
        return doRound(acc, mc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
     * Returns a {@code BigDecimal} whose value is the absolute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
     * of this {@code BigDecimal}, and whose scale is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
     * {@code this.scale()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
     * @return {@code abs(this)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
    public BigDecimal abs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
        return (signum() < 0 ? negate() : this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
     * Returns a {@code BigDecimal} whose value is the absolute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
     * of this {@code BigDecimal}, with rounding according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
     * context settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
     * @param mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
     * @return {@code abs(this)}, rounded as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
    public BigDecimal abs(MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
        return (signum() < 0 ? negate(mc) : plus(mc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
     * Returns a {@code BigDecimal} whose value is {@code (-this)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
     * and whose scale is {@code this.scale()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
     * @return {@code -this}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
    public BigDecimal negate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
        BigDecimal result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
        if (intCompact != INFLATED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
            result = BigDecimal.valueOf(-intCompact, scale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
            result = new BigDecimal(intVal.negate(), scale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
            result.precision = precision;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
     * Returns a {@code BigDecimal} whose value is {@code (-this)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
     * with rounding according to the context settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
     * @param mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
     * @return {@code -this}, rounded as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
    public BigDecimal negate(MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
        return negate().plus(mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
     * Returns a {@code BigDecimal} whose value is {@code (+this)}, and whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
     * scale is {@code this.scale()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
     * <p>This method, which simply returns this {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
     * is included for symmetry with the unary minus method {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
     * #negate()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
     * @return {@code this}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
     * @see #negate()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
    public BigDecimal plus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
     * Returns a {@code BigDecimal} whose value is {@code (+this)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
     * with rounding according to the context settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
     * <p>The effect of this method is identical to that of the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
     * #round(MathContext)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
     * @param mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
     * @return {@code this}, rounded as necessary.  A zero result will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
     *         have a scale of 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
     * @see    #round(MathContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
    public BigDecimal plus(MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
        if (mc.precision == 0)                 // no rounding please
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
            return this;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2135
        return doRound(this, mc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
     * Returns the signum function of this {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
     * @return -1, 0, or 1 as the value of this {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
     *         is negative, zero, or positive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
    public int signum() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
        return (intCompact != INFLATED)?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
            Long.signum(intCompact):
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
            intVal.signum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
     * Returns the <i>scale</i> of this {@code BigDecimal}.  If zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
     * or positive, the scale is the number of digits to the right of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
     * the decimal point.  If negative, the unscaled value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
     * number is multiplied by ten to the power of the negation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
     * scale.  For example, a scale of {@code -3} means the unscaled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
     * value is multiplied by 1000.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
     * @return the scale of this {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
    public int scale() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
        return scale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
     * Returns the <i>precision</i> of this {@code BigDecimal}.  (The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
     * precision is the number of digits in the unscaled value.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
     * <p>The precision of a zero value is 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
     * @return the precision of this {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
    public int precision() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
        int result = precision;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
        if (result == 0) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2176
            long s = intCompact;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2177
            if (s != INFLATED)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2178
                result = longDigitLength(s);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2179
            else
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2180
                result = bigDigitLength(inflate());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
            precision = result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
     * Returns a {@code BigInteger} whose value is the <i>unscaled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
     * value</i> of this {@code BigDecimal}.  (Computes <tt>(this *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
     * 10<sup>this.scale()</sup>)</tt>.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
     * @return the unscaled value of this {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
    public BigInteger unscaledValue() {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2196
        return this.inflate();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
    // Rounding Modes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
     * Rounding mode to round away from zero.  Always increments the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
     * digit prior to a nonzero discarded fraction.  Note that this rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
     * mode never decreases the magnitude of the calculated value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
    public final static int ROUND_UP =           0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
     * Rounding mode to round towards zero.  Never increments the digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
     * prior to a discarded fraction (i.e., truncates).  Note that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
     * rounding mode never increases the magnitude of the calculated value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
    public final static int ROUND_DOWN =         1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
     * Rounding mode to round towards positive infinity.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
     * {@code BigDecimal} is positive, behaves as for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
     * {@code ROUND_UP}; if negative, behaves as for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
     * {@code ROUND_DOWN}.  Note that this rounding mode never
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
     * decreases the calculated value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
    public final static int ROUND_CEILING =      2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
     * Rounding mode to round towards negative infinity.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
     * {@code BigDecimal} is positive, behave as for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
     * {@code ROUND_DOWN}; if negative, behave as for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
     * {@code ROUND_UP}.  Note that this rounding mode never
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
     * increases the calculated value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
    public final static int ROUND_FLOOR =        3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
     * Rounding mode to round towards {@literal "nearest neighbor"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
     * unless both neighbors are equidistant, in which case round up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
     * Behaves as for {@code ROUND_UP} if the discarded fraction is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
     * &ge; 0.5; otherwise, behaves as for {@code ROUND_DOWN}.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
     * that this is the rounding mode that most of us were taught in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
     * grade school.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
    public final static int ROUND_HALF_UP =      4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
     * Rounding mode to round towards {@literal "nearest neighbor"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
     * unless both neighbors are equidistant, in which case round
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
     * down.  Behaves as for {@code ROUND_UP} if the discarded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
     * fraction is {@literal >} 0.5; otherwise, behaves as for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
     * {@code ROUND_DOWN}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
    public final static int ROUND_HALF_DOWN =    5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
     * Rounding mode to round towards the {@literal "nearest neighbor"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
     * unless both neighbors are equidistant, in which case, round
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
     * towards the even neighbor.  Behaves as for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
     * {@code ROUND_HALF_UP} if the digit to the left of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
     * discarded fraction is odd; behaves as for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
     * {@code ROUND_HALF_DOWN} if it's even.  Note that this is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
     * rounding mode that minimizes cumulative error when applied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
     * repeatedly over a sequence of calculations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
    public final static int ROUND_HALF_EVEN =    6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
     * Rounding mode to assert that the requested operation has an exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
     * result, hence no rounding is necessary.  If this rounding mode is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
     * specified on an operation that yields an inexact result, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
     * {@code ArithmeticException} is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
    public final static int ROUND_UNNECESSARY =  7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
    // Scaling/Rounding Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
     * Returns a {@code BigDecimal} rounded according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
     * {@code MathContext} settings.  If the precision setting is 0 then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
     * no rounding takes place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
     * <p>The effect of this method is identical to that of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
     * {@link #plus(MathContext)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
     * @param mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
     * @return a {@code BigDecimal} rounded according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
     *         {@code MathContext} settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
     * @throws ArithmeticException if the rounding mode is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
     *         {@code UNNECESSARY} and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
     *         {@code BigDecimal}  operation would require rounding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
     * @see    #plus(MathContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
    public BigDecimal round(MathContext mc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
        return plus(mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
     * Returns a {@code BigDecimal} whose scale is the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
     * value, and whose unscaled value is determined by multiplying or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
     * dividing this {@code BigDecimal}'s unscaled value by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
     * appropriate power of ten to maintain its overall value.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
     * scale is reduced by the operation, the unscaled value must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
     * divided (rather than multiplied), and the value may be changed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
     * in this case, the specified rounding mode is applied to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
     * division.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
     * <p>Note that since BigDecimal objects are immutable, calls of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
     * this method do <i>not</i> result in the original object being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
     * modified, contrary to the usual convention of having methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
     * named <tt>set<i>X</i></tt> mutate field <i>{@code X}</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
     * Instead, {@code setScale} returns an object with the proper
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
     * scale; the returned object may or may not be newly allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
     * @param  newScale scale of the {@code BigDecimal} value to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
     * @param  roundingMode The rounding mode to apply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
     * @return a {@code BigDecimal} whose scale is the specified value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
     *         and whose unscaled value is determined by multiplying or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
     *         dividing this {@code BigDecimal}'s unscaled value by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
     *         appropriate power of ten to maintain its overall value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
     * @throws ArithmeticException if {@code roundingMode==UNNECESSARY}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
     *         and the specified scaling operation would require
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
     *         rounding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
     * @see    RoundingMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
    public BigDecimal setScale(int newScale, RoundingMode roundingMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
        return setScale(newScale, roundingMode.oldMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
     * Returns a {@code BigDecimal} whose scale is the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
     * value, and whose unscaled value is determined by multiplying or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
     * dividing this {@code BigDecimal}'s unscaled value by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
     * appropriate power of ten to maintain its overall value.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
     * scale is reduced by the operation, the unscaled value must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
     * divided (rather than multiplied), and the value may be changed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
     * in this case, the specified rounding mode is applied to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
     * division.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
     * <p>Note that since BigDecimal objects are immutable, calls of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
     * this method do <i>not</i> result in the original object being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
     * modified, contrary to the usual convention of having methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
     * named <tt>set<i>X</i></tt> mutate field <i>{@code X}</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
     * Instead, {@code setScale} returns an object with the proper
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
     * scale; the returned object may or may not be newly allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
     * <p>The new {@link #setScale(int, RoundingMode)} method should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
     * be used in preference to this legacy method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
     * @param  newScale scale of the {@code BigDecimal} value to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
     * @param  roundingMode The rounding mode to apply.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
     * @return a {@code BigDecimal} whose scale is the specified value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
     *         and whose unscaled value is determined by multiplying or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
     *         dividing this {@code BigDecimal}'s unscaled value by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
     *         appropriate power of ten to maintain its overall value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
     * @throws ArithmeticException if {@code roundingMode==ROUND_UNNECESSARY}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
     *         and the specified scaling operation would require
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
     *         rounding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
     * @throws IllegalArgumentException if {@code roundingMode} does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
     *         represent a valid rounding mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
     * @see    #ROUND_UP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
     * @see    #ROUND_DOWN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
     * @see    #ROUND_CEILING
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
     * @see    #ROUND_FLOOR
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
     * @see    #ROUND_HALF_UP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
     * @see    #ROUND_HALF_DOWN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
     * @see    #ROUND_HALF_EVEN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
     * @see    #ROUND_UNNECESSARY
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
    public BigDecimal setScale(int newScale, int roundingMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
        if (roundingMode < ROUND_UP || roundingMode > ROUND_UNNECESSARY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
            throw new IllegalArgumentException("Invalid rounding mode");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2373
        int oldScale = this.scale;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2374
        if (newScale == oldScale)        // easy case
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
        if (this.signum() == 0)            // zero can have any scale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
            return BigDecimal.valueOf(0, newScale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2379
        long rs = this.intCompact;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2380
        if (newScale > oldScale) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2381
            int raise = checkScale((long)newScale - oldScale);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2382
            BigInteger rb = null;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2383
            if (rs == INFLATED ||
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2384
                (rs = longMultiplyPowerTen(rs, raise)) == INFLATED)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2385
                rb = bigMultiplyPowerTen(raise);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2386
            return new BigDecimal(rb, rs, newScale,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2387
                                  (precision > 0) ? precision + raise : 0);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2388
        } else {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2389
            // newScale < oldScale -- drop some digits
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2390
            // Can't predict the precision due to the effect of rounding.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2391
            int drop = checkScale((long)oldScale - newScale);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2392
            if (drop < LONG_TEN_POWERS_TABLE.length)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2393
                return divideAndRound(rs, this.intVal,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2394
                                      LONG_TEN_POWERS_TABLE[drop], null,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2395
                                      newScale, roundingMode, newScale);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2396
            else
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2397
                return divideAndRound(rs, this.intVal,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2398
                                      INFLATED, bigTenToThe(drop),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2399
                                      newScale, roundingMode, newScale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
     * Returns a {@code BigDecimal} whose scale is the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
     * value, and whose value is numerically equal to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
     * {@code BigDecimal}'s.  Throws an {@code ArithmeticException}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
     * if this is not possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
     * <p>This call is typically used to increase the scale, in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
     * case it is guaranteed that there exists a {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
     * of the specified scale and the correct value.  The call can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
     * also be used to reduce the scale if the caller knows that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
     * {@code BigDecimal} has sufficiently many zeros at the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
     * its fractional part (i.e., factors of ten in its integer value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
     * to allow for the rescaling without changing its value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
     * <p>This method returns the same result as the two-argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
     * versions of {@code setScale}, but saves the caller the trouble
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
     * of specifying a rounding mode in cases where it is irrelevant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
     * <p>Note that since {@code BigDecimal} objects are immutable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
     * calls of this method do <i>not</i> result in the original
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
     * object being modified, contrary to the usual convention of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
     * having methods named <tt>set<i>X</i></tt> mutate field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
     * <i>{@code X}</i>.  Instead, {@code setScale} returns an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
     * object with the proper scale; the returned object may or may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
     * not be newly allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
     * @param  newScale scale of the {@code BigDecimal} value to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
     * @return a {@code BigDecimal} whose scale is the specified value, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
     *         whose unscaled value is determined by multiplying or dividing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
     *         this {@code BigDecimal}'s unscaled value by the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
     *         power of ten to maintain its overall value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
     * @throws ArithmeticException if the specified scaling operation would
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
     *         require rounding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
     * @see    #setScale(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
     * @see    #setScale(int, RoundingMode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
    public BigDecimal setScale(int newScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
        return setScale(newScale, ROUND_UNNECESSARY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
    // Decimal Point Motion Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
     * Returns a {@code BigDecimal} which is equivalent to this one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
     * with the decimal point moved {@code n} places to the left.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
     * {@code n} is non-negative, the call merely adds {@code n} to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
     * the scale.  If {@code n} is negative, the call is equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
     * to {@code movePointRight(-n)}.  The {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
     * returned by this call has value <tt>(this &times;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
     * 10<sup>-n</sup>)</tt> and scale {@code max(this.scale()+n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
     * 0)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
     * @param  n number of places to move the decimal point to the left.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
     * @return a {@code BigDecimal} which is equivalent to this one with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
     *         decimal point moved {@code n} places to the left.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
     * @throws ArithmeticException if scale overflows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
    public BigDecimal movePointLeft(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
        // Cannot use movePointRight(-n) in case of n==Integer.MIN_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
        int newScale = checkScale((long)scale + n);
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2463
        BigDecimal num = new BigDecimal(intVal, intCompact, newScale, 0);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2464
        return num.scale < 0 ? num.setScale(0, ROUND_UNNECESSARY) : num;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
     * Returns a {@code BigDecimal} which is equivalent to this one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
     * with the decimal point moved {@code n} places to the right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
     * If {@code n} is non-negative, the call merely subtracts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
     * {@code n} from the scale.  If {@code n} is negative, the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
     * is equivalent to {@code movePointLeft(-n)}.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
     * {@code BigDecimal} returned by this call has value <tt>(this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
     * &times; 10<sup>n</sup>)</tt> and scale {@code max(this.scale()-n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
     * 0)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
     * @param  n number of places to move the decimal point to the right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
     * @return a {@code BigDecimal} which is equivalent to this one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
     *         with the decimal point moved {@code n} places to the right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
     * @throws ArithmeticException if scale overflows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
    public BigDecimal movePointRight(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
        // Cannot use movePointLeft(-n) in case of n==Integer.MIN_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
        int newScale = checkScale((long)scale - n);
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2485
        BigDecimal num = new BigDecimal(intVal, intCompact, newScale, 0);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2486
        return num.scale < 0 ? num.setScale(0, ROUND_UNNECESSARY) : num;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
     * Returns a BigDecimal whose numerical value is equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
     * ({@code this} * 10<sup>n</sup>).  The scale of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
     * the result is {@code (this.scale() - n)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
     * @throws ArithmeticException if the scale would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
     *         outside the range of a 32-bit integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
    public BigDecimal scaleByPowerOfTen(int n) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2500
        return new BigDecimal(intVal, intCompact,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2501
                              checkScale((long)scale - n), precision);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
     * Returns a {@code BigDecimal} which is numerically equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
     * this one but with any trailing zeros removed from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
     * representation.  For example, stripping the trailing zeros from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
     * the {@code BigDecimal} value {@code 600.0}, which has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
     * [{@code BigInteger}, {@code scale}] components equals to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
     * [6000, 1], yields {@code 6E2} with [{@code BigInteger},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
     * {@code scale}] components equals to [6, -2]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
     * @return a numerically equal {@code BigDecimal} with any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
     * trailing zeros removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
    public BigDecimal stripTrailingZeros() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
        this.inflate();
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2519
        BigDecimal result = new BigDecimal(intVal, scale);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2520
        result.stripZerosToMatchScale(Long.MIN_VALUE);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2521
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
    // Comparison Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
     * Compares this {@code BigDecimal} with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
     * {@code BigDecimal}.  Two {@code BigDecimal} objects that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
     * equal in value but have a different scale (like 2.0 and 2.00)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
     * are considered equal by this method.  This method is provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
     * in preference to individual methods for each of the six boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
     * comparison operators ({@literal <}, ==,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
     * {@literal >}, {@literal >=}, !=, {@literal <=}).  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
     * suggested idiom for performing these comparisons is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
     * {@code (x.compareTo(y)} &lt;<i>op</i>&gt; {@code 0)}, where
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
     * &lt;<i>op</i>&gt; is one of the six comparison operators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
     * @param  val {@code BigDecimal} to which this {@code BigDecimal} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
     *         to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
     * @return -1, 0, or 1 as this {@code BigDecimal} is numerically
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
     *          less than, equal to, or greater than {@code val}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
    public int compareTo(BigDecimal val) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2544
        // Quick path for equal scale and non-inflated case.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2545
        if (scale == val.scale) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2546
            long xs = intCompact;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2547
            long ys = val.intCompact;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2548
            if (xs != INFLATED && ys != INFLATED)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2549
                return xs != ys ? ((xs > ys) ? 1 : -1) : 0;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2550
        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2551
        int xsign = this.signum();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2552
        int ysign = val.signum();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2553
        if (xsign != ysign)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2554
            return (xsign > ysign) ? 1 : -1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2555
        if (xsign == 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2556
            return 0;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2557
        int cmp = compareMagnitude(val);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2558
        return (xsign > 0) ? cmp : -cmp;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2559
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2561
    /**
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2562
     * Version of compareTo that ignores sign.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2563
     */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2564
    private int compareMagnitude(BigDecimal val) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2565
        // Match scales, avoid unnecessary inflation
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2566
        long ys = val.intCompact;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2567
        long xs = this.intCompact;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2568
        if (xs == 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2569
            return (ys == 0) ? 0 : -1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2570
        if (ys == 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2571
            return 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2573
        int sdiff = this.scale - val.scale;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2574
        if (sdiff != 0) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2575
            // Avoid matching scales if the (adjusted) exponents differ
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2576
            int xae = this.precision() - this.scale;   // [-1]
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2577
            int yae = val.precision() - val.scale;     // [-1]
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2578
            if (xae < yae)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2579
                return -1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2580
            if (xae > yae)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2581
                return 1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2582
            BigInteger rb = null;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2583
            if (sdiff < 0) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2584
                if ( (xs == INFLATED ||
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2585
                      (xs = longMultiplyPowerTen(xs, -sdiff)) == INFLATED) &&
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2586
                     ys == INFLATED) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2587
                    rb = bigMultiplyPowerTen(-sdiff);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2588
                    return rb.compareMagnitude(val.intVal);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2589
                }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2590
            } else { // sdiff > 0
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2591
                if ( (ys == INFLATED ||
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2592
                      (ys = longMultiplyPowerTen(ys, sdiff)) == INFLATED) &&
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2593
                     xs == INFLATED) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2594
                    rb = val.bigMultiplyPowerTen(sdiff);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2595
                    return this.intVal.compareMagnitude(rb);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2596
                }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2597
            }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2598
        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2599
        if (xs != INFLATED)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2600
            return (ys != INFLATED) ? longCompareMagnitude(xs, ys) : -1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2601
        else if (ys != INFLATED)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2602
            return 1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2603
        else
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2604
            return this.intVal.compareMagnitude(val.intVal);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
     * Compares this {@code BigDecimal} with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
     * {@code Object} for equality.  Unlike {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
     * #compareTo(BigDecimal) compareTo}, this method considers two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
     * {@code BigDecimal} objects equal only if they are equal in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
     * value and scale (thus 2.0 is not equal to 2.00 when compared by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
     * this method).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
     * @param  x {@code Object} to which this {@code BigDecimal} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
     *         to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
     * @return {@code true} if and only if the specified {@code Object} is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
     *         {@code BigDecimal} whose value and scale are equal to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
     *         {@code BigDecimal}'s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
     * @see    #compareTo(java.math.BigDecimal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
     * @see    #hashCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2623
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
    public boolean equals(Object x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
        if (!(x instanceof BigDecimal))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
        BigDecimal xDec = (BigDecimal) x;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2628
        if (x == this)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2629
            return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
        if (scale != xDec.scale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
            return false;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2632
        long s = this.intCompact;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2633
        long xs = xDec.intCompact;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2634
        if (s != INFLATED) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2635
            if (xs == INFLATED)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2636
                xs = compactValFor(xDec.intVal);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2637
            return xs == s;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2638
        } else if (xs != INFLATED)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2639
            return xs == compactValFor(this.intVal);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2640
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2641
        return this.inflate().equals(xDec.inflate());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
     * Returns the minimum of this {@code BigDecimal} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
     * {@code val}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
     * @param  val value with which the minimum is to be computed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
     * @return the {@code BigDecimal} whose value is the lesser of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
     *         {@code BigDecimal} and {@code val}.  If they are equal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
     *         as defined by the {@link #compareTo(BigDecimal) compareTo}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
     *         method, {@code this} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
     * @see    #compareTo(java.math.BigDecimal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
    public BigDecimal min(BigDecimal val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
        return (compareTo(val) <= 0 ? this : val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
     * Returns the maximum of this {@code BigDecimal} and {@code val}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
     * @param  val value with which the maximum is to be computed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
     * @return the {@code BigDecimal} whose value is the greater of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
     *         {@code BigDecimal} and {@code val}.  If they are equal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
     *         as defined by the {@link #compareTo(BigDecimal) compareTo}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
     *         method, {@code this} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
     * @see    #compareTo(java.math.BigDecimal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
    public BigDecimal max(BigDecimal val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
        return (compareTo(val) >= 0 ? this : val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
    // Hash Function
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
     * Returns the hash code for this {@code BigDecimal}.  Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
     * two {@code BigDecimal} objects that are numerically equal but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
     * differ in scale (like 2.0 and 2.00) will generally <i>not</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
     * have the same hash code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
     * @return hash code for this {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
     * @see #equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2684
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
        if (intCompact != INFLATED) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2687
            long val2 = (intCompact < 0)? -intCompact : intCompact;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
            int temp = (int)( ((int)(val2 >>> 32)) * 31  +
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2689
                              (val2 & LONG_MASK));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
            return 31*((intCompact < 0) ?-temp:temp) + scale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
            return 31*intVal.hashCode() + scale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
    // Format Converters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
     * Returns the string representation of this {@code BigDecimal},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
     * using scientific notation if an exponent is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
     * <p>A standard canonical string form of the {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
     * is created as though by the following steps: first, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
     * absolute value of the unscaled value of the {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
     * is converted to a string in base ten using the characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
     * {@code '0'} through {@code '9'} with no leading zeros (except
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
     * if its value is zero, in which case a single {@code '0'}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
     * character is used).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
     * <p>Next, an <i>adjusted exponent</i> is calculated; this is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
     * negated scale, plus the number of characters in the converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
     * unscaled value, less one.  That is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
     * {@code -scale+(ulength-1)}, where {@code ulength} is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
     * length of the absolute value of the unscaled value in decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
     * digits (its <i>precision</i>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
     * <p>If the scale is greater than or equal to zero and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
     * adjusted exponent is greater than or equal to {@code -6}, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
     * number will be converted to a character form without using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
     * exponential notation.  In this case, if the scale is zero then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
     * no decimal point is added and if the scale is positive a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
     * decimal point will be inserted with the scale specifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
     * number of characters to the right of the decimal point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
     * {@code '0'} characters are added to the left of the converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
     * unscaled value as necessary.  If no character precedes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
     * decimal point after this insertion then a conventional
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
     * {@code '0'} character is prefixed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
     * <p>Otherwise (that is, if the scale is negative, or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
     * adjusted exponent is less than {@code -6}), the number will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
     * converted to a character form using exponential notation.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
     * this case, if the converted {@code BigInteger} has more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
     * one digit a decimal point is inserted after the first digit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
     * An exponent in character form is then suffixed to the converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
     * unscaled value (perhaps with inserted decimal point); this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
     * comprises the letter {@code 'E'} followed immediately by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
     * adjusted exponent converted to a character form.  The latter is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
     * in base ten, using the characters {@code '0'} through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
     * {@code '9'} with no leading zeros, and is always prefixed by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
     * sign character {@code '-'} (<tt>'&#92;u002D'</tt>) if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
     * adjusted exponent is negative, {@code '+'}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
     * (<tt>'&#92;u002B'</tt>) otherwise).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
     * <p>Finally, the entire string is prefixed by a minus sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
     * character {@code '-'} (<tt>'&#92;u002D'</tt>) if the unscaled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
     * value is less than zero.  No sign character is prefixed if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
     * unscaled value is zero or positive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
     * <p><b>Examples:</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
     * <p>For each representation [<i>unscaled value</i>, <i>scale</i>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
     * on the left, the resulting string is shown on the right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
     * [123,0]      "123"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
     * [-123,0]     "-123"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
     * [123,-1]     "1.23E+3"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
     * [123,-3]     "1.23E+5"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
     * [123,1]      "12.3"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
     * [123,5]      "0.00123"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
     * [123,10]     "1.23E-8"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
     * [-123,12]    "-1.23E-10"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
     * <b>Notes:</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
     * <li>There is a one-to-one mapping between the distinguishable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
     * {@code BigDecimal} values and the result of this conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
     * That is, every distinguishable {@code BigDecimal} value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
     * (unscaled value and scale) has a unique string representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
     * as a result of using {@code toString}.  If that string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
     * representation is converted back to a {@code BigDecimal} using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
     * the {@link #BigDecimal(String)} constructor, then the original
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
     * value will be recovered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
     * <li>The string produced for a given number is always the same;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
     * it is not affected by locale.  This means that it can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
     * as a canonical string representation for exchanging decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
     * data, or as a key for a Hashtable, etc.  Locale-sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
     * number formatting and parsing is handled by the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
     * java.text.NumberFormat} class and its subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
     * <li>The {@link #toEngineeringString} method may be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
     * presenting numbers with exponents in engineering notation, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
     * {@link #setScale(int,RoundingMode) setScale} method may be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
     * rounding a {@code BigDecimal} so it has a known number of digits after
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
     * the decimal point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
     * <li>The digit-to-character mapping provided by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
     * {@code Character.forDigit} is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
     * @return string representation of this {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
     * @see    Character#forDigit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
     * @see    #BigDecimal(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2796
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
    public String toString() {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2798
        String sc = stringCache;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2799
        if (sc == null)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2800
            stringCache = sc = layoutChars(true);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2801
        return sc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
     * Returns a string representation of this {@code BigDecimal},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
     * using engineering notation if an exponent is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
     * <p>Returns a string that represents the {@code BigDecimal} as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
     * described in the {@link #toString()} method, except that if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
     * exponential notation is used, the power of ten is adjusted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
     * be a multiple of three (engineering notation) such that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
     * integer part of nonzero values will be in the range 1 through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
     * 999.  If exponential notation is used for zero values, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
     * decimal point and one or two fractional zero digits are used so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
     * that the scale of the zero value is preserved.  Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
     * unlike the output of {@link #toString()}, the output of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
     * method is <em>not</em> guaranteed to recover the same [integer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
     * scale] pair of this {@code BigDecimal} if the output string is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
     * converting back to a {@code BigDecimal} using the {@linkplain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
     * #BigDecimal(String) string constructor}.  The result of this method meets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
     * the weaker constraint of always producing a numerically equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
     * result from applying the string constructor to the method's output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
     * @return string representation of this {@code BigDecimal}, using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
     *         engineering notation if an exponent is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
    public String toEngineeringString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
        return layoutChars(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
     * Returns a string representation of this {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
     * without an exponent field.  For values with a positive scale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
     * the number of digits to the right of the decimal point is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
     * to indicate scale.  For values with a zero or negative scale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
     * the resulting string is generated as if the value were
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
     * converted to a numerically equal value with zero scale and as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
     * if all the trailing zeros of the zero scale value were present
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
     * in the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
     * The entire string is prefixed by a minus sign character '-'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
     * (<tt>'&#92;u002D'</tt>) if the unscaled value is less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
     * zero. No sign character is prefixed if the unscaled value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
     * zero or positive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
     * Note that if the result of this method is passed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
     * {@linkplain #BigDecimal(String) string constructor}, only the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
     * numerical value of this {@code BigDecimal} will necessarily be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
     * recovered; the representation of the new {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
     * may have a different scale.  In particular, if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
     * {@code BigDecimal} has a negative scale, the string resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
     * from this method will have a scale of zero when processed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
     * the string constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
     * (This method behaves analogously to the {@code toString}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
     * method in 1.4 and earlier releases.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
     * @return a string representation of this {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
     * without an exponent field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
     * @see #toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
     * @see #toEngineeringString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
    public String toPlainString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
        BigDecimal bd = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
        if (bd.scale < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
            bd = bd.setScale(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
        bd.inflate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
        if (bd.scale == 0)      // No decimal point
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
            return bd.intVal.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
        return bd.getValueString(bd.signum(), bd.intVal.abs().toString(), bd.scale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
    /* Returns a digit.digit string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
    private String getValueString(int signum, String intString, int scale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
        /* Insert decimal point */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
        StringBuilder buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
        int insertionPoint = intString.length() - scale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
        if (insertionPoint == 0) {  /* Point goes right before intVal */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
            return (signum<0 ? "-0." : "0.") + intString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
        } else if (insertionPoint > 0) { /* Point goes inside intVal */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
            buf = new StringBuilder(intString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
            buf.insert(insertionPoint, '.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
            if (signum < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
                buf.insert(0, '-');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
        } else { /* We must insert zeros between point and intVal */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
            buf = new StringBuilder(3-insertionPoint + intString.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
            buf.append(signum<0 ? "-0." : "0.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
            for (int i=0; i<-insertionPoint; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
                buf.append('0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
            buf.append(intString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
     * Converts this {@code BigDecimal} to a {@code BigInteger}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
     * This conversion is analogous to a <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
     * href="http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25363"><i>narrowing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
     * primitive conversion</i></a> from {@code double} to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
     * {@code long} as defined in the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
     * href="http://java.sun.com/docs/books/jls/html/">Java Language
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
     * Specification</a>: any fractional part of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
     * {@code BigDecimal} will be discarded.  Note that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
     * conversion can lose information about the precision of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
     * {@code BigDecimal} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
     * To have an exception thrown if the conversion is inexact (in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
     * other words if a nonzero fractional part is discarded), use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
     * {@link #toBigIntegerExact()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
     * @return this {@code BigDecimal} converted to a {@code BigInteger}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
    public BigInteger toBigInteger() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
        // force to an integer, quietly
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2917
        return this.setScale(0, ROUND_DOWN).inflate();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
     * Converts this {@code BigDecimal} to a {@code BigInteger},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
     * checking for lost information.  An exception is thrown if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
     * {@code BigDecimal} has a nonzero fractional part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
     * @return this {@code BigDecimal} converted to a {@code BigInteger}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
     * @throws ArithmeticException if {@code this} has a nonzero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
     *         fractional part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
    public BigInteger toBigIntegerExact() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
        // round to an integer, with Exception if decimal part non-0
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2932
        return this.setScale(0, ROUND_UNNECESSARY).inflate();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
     * Converts this {@code BigDecimal} to a {@code long}.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
     * conversion is analogous to a <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
     * href="http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25363"><i>narrowing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
     * primitive conversion</i></a> from {@code double} to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
     * {@code short} as defined in the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
     * href="http://java.sun.com/docs/books/jls/html/">Java Language
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
     * Specification</a>: any fractional part of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
     * {@code BigDecimal} will be discarded, and if the resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
     * "{@code BigInteger}" is too big to fit in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
     * {@code long}, only the low-order 64 bits are returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
     * Note that this conversion can lose information about the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
     * overall magnitude and precision of this {@code BigDecimal} value as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
     * as return a result with the opposite sign.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
     * @return this {@code BigDecimal} converted to a {@code long}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
    public long longValue(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
        return (intCompact != INFLATED && scale == 0) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
            intCompact:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
            toBigInteger().longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
     * Converts this {@code BigDecimal} to a {@code long}, checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
     * for lost information.  If this {@code BigDecimal} has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
     * nonzero fractional part or is out of the possible range for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
     * {@code long} result then an {@code ArithmeticException} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
     * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
     * @return this {@code BigDecimal} converted to a {@code long}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
     * @throws ArithmeticException if {@code this} has a nonzero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
     *         fractional part, or will not fit in a {@code long}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
    public long longValueExact() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
        if (intCompact != INFLATED && scale == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
            return intCompact;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
        // If more than 19 digits in integer part it cannot possibly fit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
        if ((precision() - scale) > 19) // [OK for negative scale too]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
            throw new java.lang.ArithmeticException("Overflow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
        // Fastpath zero and < 1.0 numbers (the latter can be very slow
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
        // to round if very small)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
        if (this.signum() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
        if ((this.precision() - this.scale) <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
            throw new ArithmeticException("Rounding necessary");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
        // round to an integer, with Exception if decimal part non-0
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2983
        BigDecimal num = this.setScale(0, ROUND_UNNECESSARY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
        if (num.precision() >= 19) // need to check carefully
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
            LongOverflow.check(num);
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2986
        return num.inflate().longValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
    private static class LongOverflow {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
        /** BigInteger equal to Long.MIN_VALUE. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
        private static final BigInteger LONGMIN = BigInteger.valueOf(Long.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
        /** BigInteger equal to Long.MAX_VALUE. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
        private static final BigInteger LONGMAX = BigInteger.valueOf(Long.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
        public static void check(BigDecimal num) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  2997
            num.inflate();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
            if ((num.intVal.compareTo(LONGMIN) < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
                (num.intVal.compareTo(LONGMAX) > 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
                throw new java.lang.ArithmeticException("Overflow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
     * Converts this {@code BigDecimal} to an {@code int}.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
     * conversion is analogous to a <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
     * href="http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25363"><i>narrowing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
     * primitive conversion</i></a> from {@code double} to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
     * {@code short} as defined in the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
     * href="http://java.sun.com/docs/books/jls/html/">Java Language
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
     * Specification</a>: any fractional part of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
     * {@code BigDecimal} will be discarded, and if the resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
     * "{@code BigInteger}" is too big to fit in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
     * {@code int}, only the low-order 32 bits are returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
     * Note that this conversion can lose information about the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
     * overall magnitude and precision of this {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
     * value as well as return a result with the opposite sign.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
     * @return this {@code BigDecimal} converted to an {@code int}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
    public int intValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
        return  (intCompact != INFLATED && scale == 0) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
            (int)intCompact :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
            toBigInteger().intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
     * Converts this {@code BigDecimal} to an {@code int}, checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
     * for lost information.  If this {@code BigDecimal} has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
     * nonzero fractional part or is out of the possible range for an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
     * {@code int} result then an {@code ArithmeticException} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
     * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
     * @return this {@code BigDecimal} converted to an {@code int}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
     * @throws ArithmeticException if {@code this} has a nonzero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
     *         fractional part, or will not fit in an {@code int}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
    public int intValueExact() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
       long num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
       num = this.longValueExact();     // will check decimal part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
       if ((int)num != num)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
           throw new java.lang.ArithmeticException("Overflow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
       return (int)num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
     * Converts this {@code BigDecimal} to a {@code short}, checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
     * for lost information.  If this {@code BigDecimal} has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
     * nonzero fractional part or is out of the possible range for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
     * {@code short} result then an {@code ArithmeticException} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
     * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
     * @return this {@code BigDecimal} converted to a {@code short}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
     * @throws ArithmeticException if {@code this} has a nonzero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
     *         fractional part, or will not fit in a {@code short}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
    public short shortValueExact() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
       long num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
       num = this.longValueExact();     // will check decimal part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
       if ((short)num != num)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
           throw new java.lang.ArithmeticException("Overflow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
       return (short)num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
     * Converts this {@code BigDecimal} to a {@code byte}, checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
     * for lost information.  If this {@code BigDecimal} has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
     * nonzero fractional part or is out of the possible range for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
     * {@code byte} result then an {@code ArithmeticException} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
     * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
     * @return this {@code BigDecimal} converted to a {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
     * @throws ArithmeticException if {@code this} has a nonzero
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
     *         fractional part, or will not fit in a {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
    public byte byteValueExact() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
       long num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
       num = this.longValueExact();     // will check decimal part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
       if ((byte)num != num)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
           throw new java.lang.ArithmeticException("Overflow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
       return (byte)num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
     * Converts this {@code BigDecimal} to a {@code float}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
     * This conversion is similar to the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
     * href="http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25363"><i>narrowing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
     * primitive conversion</i></a> from {@code double} to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
     * {@code float} defined in the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
     * href="http://java.sun.com/docs/books/jls/html/">Java Language
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
     * Specification</a>: if this {@code BigDecimal} has too great a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
     * magnitude to represent as a {@code float}, it will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
     * converted to {@link Float#NEGATIVE_INFINITY} or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
     * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
     * the return value is finite, this conversion can lose
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
     * information about the precision of the {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
     * @return this {@code BigDecimal} converted to a {@code float}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
    public float floatValue(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
        if (scale == 0 && intCompact != INFLATED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
                return (float)intCompact;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
        // Somewhat inefficient, but guaranteed to work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
        return Float.parseFloat(this.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
     * Converts this {@code BigDecimal} to a {@code double}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
     * This conversion is similar to the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
     * href="http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25363"><i>narrowing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
     * primitive conversion</i></a> from {@code double} to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
     * {@code float} as defined in the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
     * href="http://java.sun.com/docs/books/jls/html/">Java Language
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
     * Specification</a>: if this {@code BigDecimal} has too great a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
     * magnitude represent as a {@code double}, it will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
     * converted to {@link Double#NEGATIVE_INFINITY} or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
     * Double#POSITIVE_INFINITY} as appropriate.  Note that even when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
     * the return value is finite, this conversion can lose
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
     * information about the precision of the {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
     * @return this {@code BigDecimal} converted to a {@code double}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
    public double doubleValue(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
        if (scale == 0 && intCompact != INFLATED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
            return (double)intCompact;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
        // Somewhat inefficient, but guaranteed to work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
        return Double.parseDouble(this.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
     * Returns the size of an ulp, a unit in the last place, of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
     * {@code BigDecimal}.  An ulp of a nonzero {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
     * value is the positive distance between this value and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
     * {@code BigDecimal} value next larger in magnitude with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
     * same number of digits.  An ulp of a zero value is numerically
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
     * equal to 1 with the scale of {@code this}.  The result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
     * stored with the same scale as {@code this} so the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
     * for zero and nonzero values is equal to {@code [1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
     * this.scale()]}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
     * @return the size of an ulp of {@code this}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
    public BigDecimal ulp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
        return BigDecimal.valueOf(1, this.scale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3153
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3154
    // Private class to build a string representation for BigDecimal object.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3155
    // "StringBuilderHelper" is constructed as a thread local variable so it is
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3156
    // thread safe. The StringBuilder field acts as a buffer to hold the temporary
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3157
    // representation of BigDecimal. The cmpCharArray holds all the characters for
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3158
    // the compact representation of BigDecimal (except for '-' sign' if it is
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3159
    // negative) if its intCompact field is not INFLATED. It is shared by all
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3160
    // calls to toString() and its variants in that particular thread.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3161
    static class StringBuilderHelper {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3162
        final StringBuilder sb;    // Placeholder for BigDecimal string
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3163
        final char[] cmpCharArray; // character array to place the intCompact
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3164
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3165
        StringBuilderHelper() {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3166
            sb = new StringBuilder();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3167
            // All non negative longs can be made to fit into 19 character array.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3168
            cmpCharArray = new char[19];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3169
        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3170
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3171
        // Accessors.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3172
        StringBuilder getStringBuilder() {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3173
            sb.setLength(0);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3174
            return sb;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3175
        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3176
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3177
        char[] getCompactCharArray() {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3178
            return cmpCharArray;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3179
        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3180
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3181
        /**
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3182
         * Places characters representing the intCompact in {@code long} into
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3183
         * cmpCharArray and returns the offset to the array where the
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3184
         * representation starts.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3185
         *
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3186
         * @param intCompact the number to put into the cmpCharArray.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3187
         * @return offset to the array where the representation starts.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3188
         * Note: intCompact must be greater or equal to zero.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3189
         */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3190
        int putIntCompact(long intCompact) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3191
            assert intCompact >= 0;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3192
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3193
            long q;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3194
            int r;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3195
            // since we start from the least significant digit, charPos points to
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3196
            // the last character in cmpCharArray.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3197
            int charPos = cmpCharArray.length;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3198
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3199
            // Get 2 digits/iteration using longs until quotient fits into an int
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3200
            while (intCompact > Integer.MAX_VALUE) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3201
                q = intCompact / 100;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3202
                r = (int)(intCompact - q * 100);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3203
                intCompact = q;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3204
                cmpCharArray[--charPos] = DIGIT_ONES[r];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3205
                cmpCharArray[--charPos] = DIGIT_TENS[r];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3206
            }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3207
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3208
            // Get 2 digits/iteration using ints when i2 >= 100
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3209
            int q2;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3210
            int i2 = (int)intCompact;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3211
            while (i2 >= 100) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3212
                q2 = i2 / 100;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3213
                r  = i2 - q2 * 100;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3214
                i2 = q2;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3215
                cmpCharArray[--charPos] = DIGIT_ONES[r];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3216
                cmpCharArray[--charPos] = DIGIT_TENS[r];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3217
            }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3218
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3219
            cmpCharArray[--charPos] = DIGIT_ONES[i2];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3220
            if (i2 >= 10)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3221
                cmpCharArray[--charPos] = DIGIT_TENS[i2];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3222
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3223
            return charPos;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3224
        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3225
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3226
        final static char[] DIGIT_TENS = {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3227
            '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3228
            '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3229
            '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3230
            '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3231
            '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3232
            '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3233
            '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3234
            '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3235
            '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3236
            '9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3237
        };
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3238
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3239
        final static char[] DIGIT_ONES = {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3240
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3241
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3242
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3243
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3244
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3245
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3246
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3247
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3248
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3249
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3250
        };
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3251
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
     * Lay out this {@code BigDecimal} into a {@code char[]} array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
     * The Java 1.2 equivalent to this was called {@code getValueString}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
     * @param  sci {@code true} for Scientific exponential notation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
     *          {@code false} for Engineering
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
     * @return string with canonical string representation of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
     *         {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
    private String layoutChars(boolean sci) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
        if (scale == 0)                      // zero scale is trivial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
            return (intCompact != INFLATED) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
                Long.toString(intCompact):
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
                intVal.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3268
        StringBuilderHelper sbHelper = threadLocalStringBuilderHelper.get();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3269
        char[] coeff;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3270
        int offset;  // offset is the starting index for coeff array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
        // Get the significand as an absolute value
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3272
        if (intCompact != INFLATED) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3273
            offset = sbHelper.putIntCompact(Math.abs(intCompact));
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3274
            coeff  = sbHelper.getCompactCharArray();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3275
        } else {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3276
            offset = 0;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3277
            coeff  = intVal.abs().toString().toCharArray();
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3278
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
        // Construct a buffer, with sufficient capacity for all cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
        // If E-notation is needed, length will be: +1 if negative, +1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
        // if '.' needed, +2 for "E+", + up to 10 for adjusted exponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
        // Otherwise it could have +1 if negative, plus leading "0.00000"
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3284
        StringBuilder buf = sbHelper.getStringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
        if (signum() < 0)             // prefix '-' if negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
            buf.append('-');
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3287
        int coeffLen = coeff.length - offset;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3288
        long adjusted = -(long)scale + (coeffLen -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
        if ((scale >= 0) && (adjusted >= -6)) { // plain number
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3290
            int pad = scale - coeffLen;         // count of padding zeros
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3291
            if (pad >= 0) {                     // 0.xxx form
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
                buf.append('0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
                buf.append('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
                for (; pad>0; pad--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
                    buf.append('0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
                }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3297
                buf.append(coeff, offset, coeffLen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
            } else {                         // xx.xx form
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3299
                buf.append(coeff, offset, -pad);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
                buf.append('.');
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3301
                buf.append(coeff, -pad + offset, scale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
        } else { // E-notation is needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
            if (sci) {                       // Scientific notation
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3305
                buf.append(coeff[offset]);   // first character
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3306
                if (coeffLen > 1) {          // more to come
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
                    buf.append('.');
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3308
                    buf.append(coeff, offset + 1, coeffLen - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
            } else {                         // Engineering notation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
                int sig = (int)(adjusted % 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
                if (sig < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
                    sig += 3;                // [adjusted was negative]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
                adjusted -= sig;             // now a multiple of 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
                sig++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
                if (signum() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
                    switch (sig) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
                    case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
                        buf.append('0'); // exponent is a multiple of three
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
                    case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
                        buf.append("0.00");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
                        adjusted += 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
                    case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
                        buf.append("0.0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
                        adjusted += 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
                        throw new AssertionError("Unexpected sig value " + sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
                    }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3332
                } else if (sig >= coeffLen) {   // significand all in integer
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3333
                    buf.append(coeff, offset, coeffLen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
                    // may need some zeros, too
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3335
                    for (int i = sig - coeffLen; i > 0; i--)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
                        buf.append('0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3337
                } else {                     // xx.xxE form
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3338
                    buf.append(coeff, offset, sig);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
                    buf.append('.');
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3340
                    buf.append(coeff, offset + sig, coeffLen - sig);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
            if (adjusted != 0) {             // [!sci could have made 0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
                buf.append('E');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
                if (adjusted > 0)            // force sign for positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
                    buf.append('+');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
                buf.append(adjusted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
     * Return 10 to the power n, as a {@code BigInteger}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
     * @param  n the power of ten to be returned (>=0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
     * @return a {@code BigInteger} with the value (10<sup>n</sup>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3359
    private static BigInteger bigTenToThe(int n) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3360
        if (n < 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3361
            return BigInteger.ZERO;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3362
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3363
        if (n < BIG_TEN_POWERS_TABLE_MAX) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3364
            BigInteger[] pows = BIG_TEN_POWERS_TABLE;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3365
            if (n < pows.length)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3366
                return pows[n];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3367
            else
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3368
                return expandBigIntegerTenPowers(n);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3369
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
        // BigInteger.pow is slow, so make 10**n by constructing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
        // BigInteger from a character string (still not very fast)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3372
        char tenpow[] = new char[n + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
        tenpow[0] = '1';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
        for (int i = 1; i <= n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
            tenpow[i] = '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
        return new BigInteger(tenpow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
    }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3378
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3379
    /**
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3380
     * Expand the BIG_TEN_POWERS_TABLE array to contain at least 10**n.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3381
     *
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3382
     * @param n the power of ten to be returned (>=0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3383
     * @return a {@code BigDecimal} with the value (10<sup>n</sup>) and
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3384
     *         in the meantime, the BIG_TEN_POWERS_TABLE array gets
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3385
     *         expanded to the size greater than n.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3386
     */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3387
    private static BigInteger expandBigIntegerTenPowers(int n) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3388
        synchronized(BigDecimal.class) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3389
            BigInteger[] pows = BIG_TEN_POWERS_TABLE;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3390
            int curLen = pows.length;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3391
            // The following comparison and the above synchronized statement is
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3392
            // to prevent multiple threads from expanding the same array.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3393
            if (curLen <= n) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3394
                int newLen = curLen << 1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3395
                while (newLen <= n)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3396
                    newLen <<= 1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3397
                pows = Arrays.copyOf(pows, newLen);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3398
                for (int i = curLen; i < newLen; i++)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3399
                    pows[i] = pows[i - 1].multiply(BigInteger.TEN);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3400
                // Based on the following facts:
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3401
                // 1. pows is a private local varible;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3402
                // 2. the following store is a volatile store.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3403
                // the newly created array elements can be safely published.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3404
                BIG_TEN_POWERS_TABLE = pows;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3405
            }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3406
            return pows[n];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3407
        }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3408
    }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3409
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3410
    private static final long[] LONG_TEN_POWERS_TABLE = {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3411
        1,                     // 0 / 10^0
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3412
        10,                    // 1 / 10^1
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3413
        100,                   // 2 / 10^2
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3414
        1000,                  // 3 / 10^3
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3415
        10000,                 // 4 / 10^4
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3416
        100000,                // 5 / 10^5
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3417
        1000000,               // 6 / 10^6
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3418
        10000000,              // 7 / 10^7
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3419
        100000000,             // 8 / 10^8
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3420
        1000000000,            // 9 / 10^9
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3421
        10000000000L,          // 10 / 10^10
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3422
        100000000000L,         // 11 / 10^11
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3423
        1000000000000L,        // 12 / 10^12
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3424
        10000000000000L,       // 13 / 10^13
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3425
        100000000000000L,      // 14 / 10^14
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3426
        1000000000000000L,     // 15 / 10^15
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3427
        10000000000000000L,    // 16 / 10^16
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3428
        100000000000000000L,   // 17 / 10^17
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3429
        1000000000000000000L   // 18 / 10^18
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3430
    };
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3431
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3432
    private static volatile BigInteger BIG_TEN_POWERS_TABLE[] = {BigInteger.ONE,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
        BigInteger.valueOf(10),       BigInteger.valueOf(100),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
        BigInteger.valueOf(1000),     BigInteger.valueOf(10000),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
        BigInteger.valueOf(100000),   BigInteger.valueOf(1000000),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
        BigInteger.valueOf(10000000), BigInteger.valueOf(100000000),
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3437
        BigInteger.valueOf(1000000000),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3438
        BigInteger.valueOf(10000000000L),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3439
        BigInteger.valueOf(100000000000L),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3440
        BigInteger.valueOf(1000000000000L),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3441
        BigInteger.valueOf(10000000000000L),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3442
        BigInteger.valueOf(100000000000000L),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3443
        BigInteger.valueOf(1000000000000000L),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3444
        BigInteger.valueOf(10000000000000000L),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3445
        BigInteger.valueOf(100000000000000000L),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3446
        BigInteger.valueOf(1000000000000000000L)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3447
    };
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3448
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3449
    private static final int BIG_TEN_POWERS_TABLE_INITLEN =
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3450
        BIG_TEN_POWERS_TABLE.length;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3451
    private static final int BIG_TEN_POWERS_TABLE_MAX =
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3452
        16 * BIG_TEN_POWERS_TABLE_INITLEN;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3453
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3454
    private static final long THRESHOLDS_TABLE[] = {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3455
        Long.MAX_VALUE,                     // 0
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3456
        Long.MAX_VALUE/10L,                 // 1
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3457
        Long.MAX_VALUE/100L,                // 2
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3458
        Long.MAX_VALUE/1000L,               // 3
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3459
        Long.MAX_VALUE/10000L,              // 4
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3460
        Long.MAX_VALUE/100000L,             // 5
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3461
        Long.MAX_VALUE/1000000L,            // 6
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3462
        Long.MAX_VALUE/10000000L,           // 7
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3463
        Long.MAX_VALUE/100000000L,          // 8
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3464
        Long.MAX_VALUE/1000000000L,         // 9
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3465
        Long.MAX_VALUE/10000000000L,        // 10
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3466
        Long.MAX_VALUE/100000000000L,       // 11
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3467
        Long.MAX_VALUE/1000000000000L,      // 12
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3468
        Long.MAX_VALUE/10000000000000L,     // 13
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3469
        Long.MAX_VALUE/100000000000000L,    // 14
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3470
        Long.MAX_VALUE/1000000000000000L,   // 15
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3471
        Long.MAX_VALUE/10000000000000000L,  // 16
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3472
        Long.MAX_VALUE/100000000000000000L, // 17
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3473
        Long.MAX_VALUE/1000000000000000000L // 18
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3474
    };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3477
     * Compute val * 10 ^ n; return this product if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3478
     * representable as a long, INFLATED otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3480
    private static long longMultiplyPowerTen(long val, int n) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3481
        if (val == 0 || n <= 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3482
            return val;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3483
        long[] tab = LONG_TEN_POWERS_TABLE;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3484
        long[] bounds = THRESHOLDS_TABLE;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3485
        if (n < tab.length && n < bounds.length) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3486
            long tenpower = tab[n];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3487
            if (val == 1)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3488
                return tenpower;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3489
            if (Math.abs(val) <= bounds[n])
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3490
                return val * tenpower;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
        return INFLATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3495
    /**
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3496
     * Compute this * 10 ^ n.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3497
     * Needed mainly to allow special casing to trap zero value
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3498
     */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3499
    private BigInteger bigMultiplyPowerTen(int n) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3500
        if (n <= 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3501
            return this.inflate();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3503
        if (intCompact != INFLATED)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3504
            return bigTenToThe(n).multiply(intCompact);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3505
        else
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3506
            return intVal.multiply(bigTenToThe(n));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
     * Assign appropriate BigInteger to intVal field if intVal is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
     * null, i.e. the compact representation is in use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3513
    private BigInteger inflate() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
        if (intVal == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
            intVal = BigInteger.valueOf(intCompact);
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3516
        return intVal;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
     * Match the scales of two {@code BigDecimal}s to align their
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
     * least significant digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
     * <p>If the scales of val[0] and val[1] differ, rescale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
     * (non-destructively) the lower-scaled {@code BigDecimal} so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
     * they match.  That is, the lower-scaled reference will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
     * replaced by a reference to a new object with the same scale as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
     * the other {@code BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
     * @param  val array of two elements referring to the two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
     *         {@code BigDecimal}s to be aligned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
    private static void matchScale(BigDecimal[] val) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3533
        if (val[0].scale == val[1].scale) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3534
            return;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3535
        } else if (val[0].scale < val[1].scale) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3536
            val[0] = val[0].setScale(val[1].scale, ROUND_UNNECESSARY);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3537
        } else if (val[1].scale < val[0].scale) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3538
            val[1] = val[1].setScale(val[0].scale, ROUND_UNNECESSARY);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3539
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
     * Reconstitute the {@code BigDecimal} instance from a stream (that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
     * deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3546
     * @param s the stream being read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3547
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3548
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
        // Read in all fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
        // validate possibly bad fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
        if (intVal == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
            String message = "BigDecimal: null intVal in stream";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
            throw new java.io.StreamCorruptedException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
        // [all values of scale are now allowed]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
        }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3558
        intCompact = compactValFor(intVal);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
    * Serialize this {@code BigDecimal} to the stream in question
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3564
    * @param s the stream to serialize to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
   private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
       throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
       // Must inflate to maintain compatible serial form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
       this.inflate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3571
       // Write proper fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3572
       s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3573
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3574
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3575
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
    /**
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3577
     * Returns the length of the absolute value of a {@code long}, in decimal
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3578
     * digits.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3579
     *
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3580
     * @param x the {@code long}
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3581
     * @return the length of the unscaled value, in deciaml digits.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3582
     */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3583
    private static int longDigitLength(long x) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3584
        /*
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3585
         * As described in "Bit Twiddling Hacks" by Sean Anderson,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3586
         * (http://graphics.stanford.edu/~seander/bithacks.html)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3587
         * integer log 10 of x is within 1 of
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3588
         * (1233/4096)* (1 + integer log 2 of x).
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3589
         * The fraction 1233/4096 approximates log10(2). So we first
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3590
         * do a version of log2 (a variant of Long class with
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3591
         * pre-checks and opposite directionality) and then scale and
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3592
         * check against powers table. This is a little simpler in
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3593
         * present context than the version in Hacker's Delight sec
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3594
         * 11-4.  Adding one to bit length allows comparing downward
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3595
         * from the LONG_TEN_POWERS_TABLE that we need anyway.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3596
         */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3597
        assert x != INFLATED;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3598
        if (x < 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3599
            x = -x;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3600
        if (x < 10) // must screen for 0, might as well 10
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3601
            return 1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3602
        int n = 64; // not 63, to avoid needing to add 1 later
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3603
        int y = (int)(x >>> 32);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3604
        if (y == 0) { n -= 32; y = (int)x; }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3605
        if (y >>> 16 == 0) { n -= 16; y <<= 16; }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3606
        if (y >>> 24 == 0) { n -=  8; y <<=  8; }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3607
        if (y >>> 28 == 0) { n -=  4; y <<=  4; }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3608
        if (y >>> 30 == 0) { n -=  2; y <<=  2; }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3609
        int r = (((y >>> 31) + n) * 1233) >>> 12;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3610
        long[] tab = LONG_TEN_POWERS_TABLE;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3611
        // if r >= length, must have max possible digits for long
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3612
        return (r >= tab.length || x < tab[r])? r : r+1;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3613
    }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3614
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3615
    /**
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3616
     * Returns the length of the absolute value of a BigInteger, in
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3617
     * decimal digits.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3618
     *
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3619
     * @param b the BigInteger
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3620
     * @return the length of the unscaled value, in decimal digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3622
    private static int bigDigitLength(BigInteger b) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3623
        /*
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3624
         * Same idea as the long version, but we need a better
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3625
         * approximation of log10(2). Using 646456993/2^31
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3626
         * is accurate up to max possible reported bitLength.
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3627
         */
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3628
        if (b.signum == 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3629
            return 1;
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3630
        int r = (int)((((long)b.bitLength() + 1) * 646456993) >>> 31);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3631
        return b.compareMagnitude(bigTenToThe(r)) < 0? r : r+1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3633
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3634
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3635
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3636
     * Remove insignificant trailing zeros from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
     * {@code BigDecimal} until the preferred scale is reached or no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3638
     * more zeros can be removed.  If the preferred scale is less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3639
     * Integer.MIN_VALUE, all the trailing zeros will be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3640
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3641
     * {@code BigInteger} assistance could help, here?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3642
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3643
     * <p>WARNING: This method should only be called on new objects as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3644
     * it mutates the value fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3645
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3646
     * @return this {@code BigDecimal} with a scale possibly reduced
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3647
     * to be closed to the preferred scale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3648
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3649
    private BigDecimal stripZerosToMatchScale(long preferredScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3650
        this.inflate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3651
        BigInteger qr[];                // quotient-remainder pair
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3652
        while ( intVal.compareMagnitude(BigInteger.TEN) >= 0 &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3653
                scale > preferredScale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3654
            if (intVal.testBit(0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3655
                break;                  // odd number cannot end in 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3656
            qr = intVal.divideAndRemainder(BigInteger.TEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3657
            if (qr[1].signum() != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3658
                break;                  // non-0 remainder
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3659
            intVal=qr[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3660
            scale = checkScale((long)scale-1);  // could Overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3661
            if (precision > 0)          // adjust precision if known
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3662
              precision--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3663
        }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3664
        if (intVal != null)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3665
            intCompact = compactValFor(intVal);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3666
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3667
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3668
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3669
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3670
     * Check a scale for Underflow or Overflow.  If this BigDecimal is
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3671
     * nonzero, throw an exception if the scale is outof range. If this
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3672
     * is zero, saturate the scale to the extreme value of the right
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3673
     * sign if the scale is out of range.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3674
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3675
     * @param val The new scale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3676
     * @throws ArithmeticException (overflow or underflow) if the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3677
     *         scale is out of range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3678
     * @return validated scale as an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3679
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3680
    private int checkScale(long val) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3681
        int asInt = (int)val;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3682
        if (asInt != val) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3683
            asInt = val>Integer.MAX_VALUE ? Integer.MAX_VALUE : Integer.MIN_VALUE;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3684
            BigInteger b;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3685
            if (intCompact != 0 &&
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3686
                ((b = intVal) == null || b.signum() != 0))
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3687
                throw new ArithmeticException(asInt>0 ? "Underflow":"Overflow");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3688
        }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3689
        return asInt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3690
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3691
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3692
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3693
     * Round an operand; used only if digits &gt; 0.  Does not change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3694
     * {@code this}; if rounding is needed a new {@code BigDecimal}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3695
     * is created and returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3696
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3697
     * @param mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3698
     * @throws ArithmeticException if the result is inexact but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3699
     *         rounding mode is {@code UNNECESSARY}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3700
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3701
    private BigDecimal roundOp(MathContext mc) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3702
        BigDecimal rounded = doRound(this, mc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3703
        return rounded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3705
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3706
    /** Round this BigDecimal according to the MathContext settings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3707
     *  used only if precision {@literal >} 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3708
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3709
     * <p>WARNING: This method should only be called on new objects as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3710
     * it mutates the value fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3711
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3712
     * @param mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3713
     * @throws ArithmeticException if the rounding mode is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3714
     *         {@code RoundingMode.UNNECESSARY} and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3715
     *         {@code BigDecimal} operation would require rounding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3716
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3717
    private void roundThis(MathContext mc) {
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3718
        BigDecimal rounded = doRound(this, mc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3719
        if (rounded == this)                 // wasn't rounded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3720
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3721
        this.intVal     = rounded.intVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3722
        this.intCompact = rounded.intCompact;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3723
        this.scale      = rounded.scale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3724
        this.precision  = rounded.precision;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3725
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3726
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3727
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3728
     * Returns a {@code BigDecimal} rounded according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3729
     * MathContext settings; used only if {@code mc.precision > 0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3730
     * Does not change {@code this}; if rounding is needed a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3731
     * {@code BigDecimal} is created and returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3732
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3733
     * @param mc the context to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3734
     * @return a {@code BigDecimal} rounded according to the MathContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3735
     *         settings.  May return this, if no rounding needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3736
     * @throws ArithmeticException if the rounding mode is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3737
     *         {@code RoundingMode.UNNECESSARY} and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3738
     *         result is inexact.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3739
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3740
    private static BigDecimal doRound(BigDecimal d, MathContext mc) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3741
        int mcp = mc.precision;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3742
        int drop;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3743
        // This might (rarely) iterate to cover the 999=>1000 case
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3744
        while ((drop = d.precision() - mcp) > 0) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3745
            int newScale = d.checkScale((long)d.scale - drop);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3746
            int mode = mc.roundingMode.oldMode;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3747
            if (drop < LONG_TEN_POWERS_TABLE.length)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3748
                d = divideAndRound(d.intCompact, d.intVal,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3749
                                   LONG_TEN_POWERS_TABLE[drop], null,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3750
                                   newScale, mode, newScale);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3751
            else
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3752
                d = divideAndRound(d.intCompact, d.intVal,
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3753
                                   INFLATED, bigTenToThe(drop),
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3754
                                   newScale, mode, newScale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3755
        }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3756
        return d;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3757
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3758
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3759
    /**
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3760
     * Returns the compact value for given {@code BigInteger}, or
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3761
     * INFLATED if too big. Relies on internal representation of
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3762
     * {@code BigInteger}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3763
     */
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3764
    private static long compactValFor(BigInteger b) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3765
        int[] m = b.mag;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3766
        int len = m.length;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3767
        if (len == 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3768
            return 0;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3769
        int d = m[0];
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3770
        if (len > 2 || (len == 2 && d < 0))
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3771
            return INFLATED;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3772
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3773
        long u = (len == 2)?
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3774
            (((long) m[1] & LONG_MASK) + (((long)d) << 32)) :
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3775
            (((long)d)   & LONG_MASK);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3776
        return (b.signum < 0)? -u : u;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3778
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3779
    private static int longCompareMagnitude(long x, long y) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3780
        if (x < 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3781
            x = -x;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3782
        if (y < 0)
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3783
            y = -y;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3784
        return (x < y) ? -1 : ((x == y) ? 0 : 1);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3785
    }
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3786
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3787
    private static int saturateLong(long s) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3788
        int i = (int)s;
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3789
        return (s == i) ? i : (s < 0 ? Integer.MIN_VALUE : Integer.MAX_VALUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3790
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3791
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3792
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3793
     * Internal printing routine
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3794
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3795
    private static void print(String name, BigDecimal bd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3796
        System.err.format("%s:\tintCompact %d\tintVal %d\tscale %d\tprecision %d%n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3797
                          name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3798
                          bd.intCompact,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3799
                          bd.intVal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3800
                          bd.scale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3801
                          bd.precision);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3802
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3803
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3804
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3805
     * Check internal invariants of this BigDecimal.  These invariants
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3806
     * include:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3807
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3808
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3809
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3810
     * <li>The object must be initialized; either intCompact must not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3811
     * INFLATED or intVal is non-null.  Both of these conditions may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3812
     * be true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3813
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3814
     * <li>If both intCompact and intVal and set, their values must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3815
     * consistent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3816
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3817
     * <li>If precision is nonzero, it must have the right value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3818
     * </ul>
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3819
     *
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3820
     * Note: Since this is an audit method, we are not supposed to change the
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3821
     * state of this BigDecimal object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3822
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3823
    private BigDecimal audit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3824
        if (intCompact == INFLATED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3825
            if (intVal == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3826
                print("audit", this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3827
                throw new AssertionError("null intVal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3828
            }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3829
            // Check precision
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3830
            if (precision > 0 && precision != bigDigitLength(intVal)) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3831
                print("audit", this);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3832
                throw new AssertionError("precision mismatch");
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3833
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3834
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3835
            if (intVal != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3836
                long val = intVal.longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3837
                if (val != intCompact) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3838
                    print("audit", this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3839
                    throw new AssertionError("Inconsistent state, intCompact=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3840
                                             intCompact + "\t intVal=" + val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3841
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3842
            }
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3843
            // Check precision
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3844
            if (precision > 0 && precision != longDigitLength(intCompact)) {
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3845
                print("audit", this);
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3846
                throw new AssertionError("precision mismatch");
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
  3847
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3848
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3849
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3850
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3851
}