1 /* Generated from 'BigDecimal.nrx' 8 Sep 2000 11:10:50 [v2.00] */
2 /* Options: Binary Comments Crossref Format Java Logo Strictargs Strictcase Trace2 Verbose3 */
3 package com.ibm.icu.math;
5 import java.math.BigInteger;
7 import com.ibm.icu.lang.UCharacter;
9 /* ------------------------------------------------------------------ */
10 /* BigDecimal -- Decimal arithmetic for Java */
11 /* ------------------------------------------------------------------ */
12 /* Copyright IBM Corporation, 1996-2011. All Rights Reserved. */
14 /* The BigDecimal class provides immutable arbitrary-precision */
15 /* floating point (including integer) decimal numbers. */
17 /* As the numbers are decimal, there is an exact correspondence */
18 /* between an instance of a BigDecimal object and its String */
19 /* representation; the BigDecimal class provides direct conversions */
20 /* to and from String and character array objects, and well as */
21 /* conversions to and from the Java primitive types (which may not */
23 /* ------------------------------------------------------------------ */
26 /* 1. A BigDecimal object is never changed in value once constructed; */
27 /* this avoids the need for locking. Note in particular that the */
28 /* mantissa array may be shared between many BigDecimal objects, */
29 /* so that once exposed it must not be altered. */
31 /* 2. This class looks at MathContext class fields directly (for */
32 /* performance). It must not and does not change them. */
34 /* 3. Exponent checking is delayed until finish(), as we know */
35 /* intermediate calculations cannot cause 31-bit overflow. */
36 /* [This assertion depends on MAX_DIGITS in MathContext.] */
38 /* 4. Comments for the public API now follow the javadoc conventions. */
39 /* The NetRexx -comments option is used to pass these comments */
40 /* through to the generated Java code (with -format, if desired). */
42 /* 5. System.arraycopy is faster than explicit loop as follows */
43 /* Mean length 4: equal */
44 /* Mean length 8: x2 */
45 /* Mean length 16: x3 */
46 /* Mean length 24: x4 */
47 /* From prior experience, we expect mean length a little below 8, */
48 /* but arraycopy is still the one to use, in general, until later */
49 /* measurements suggest otherwise. */
51 /* 6. 'DMSRCN' referred to below is the original (1981) IBM S/370 */
52 /* assembler code implementation of the algorithms below; it is */
53 /* now called IXXRCN and is available with the OS/390 and VM/ESA */
54 /* operating systems. */
55 /* ------------------------------------------------------------------ */
57 /* 1997.09.02 Initial version (derived from netrexx.lang classes) */
58 /* 1997.09.12 Add lostDigits checking */
59 /* 1997.10.06 Change mantissa to a byte array */
60 /* 1997.11.22 Rework power [did not prepare arguments, etc.] */
61 /* 1997.12.13 multiply did not prepare arguments */
62 /* 1997.12.14 add did not prepare and align arguments correctly */
63 /* 1998.05.02 0.07 packaging changes suggested by Sun and Oracle */
64 /* 1998.05.21 adjust remainder operator finalization */
65 /* 1998.06.04 rework to pass MathContext to finish() and round() */
66 /* 1998.06.06 change format to use round(); support rounding modes */
67 /* 1998.06.25 rename to BigDecimal and begin merge */
68 /* zero can now have trailing zeros (i.e., exp\=0) */
69 /* 1998.06.28 new methods: movePointXxxx, scale, toBigInteger */
70 /* unscaledValue, valueof */
71 /* 1998.07.01 improve byteaddsub to allow array reuse, etc. */
72 /* 1998.07.01 make null testing explicit to avoid JIT bug [Win32] */
73 /* 1998.07.07 scaled division [divide(BigDecimal, int, int)] */
74 /* 1998.07.08 setScale, faster equals */
75 /* 1998.07.11 allow 1E6 (no sign) <sigh>; new double/float conversion */
76 /* 1998.10.12 change package to com.ibm.icu.math */
77 /* 1998.12.14 power operator no longer rounds RHS [to match ANSI] */
78 /* add toBigDecimal() and BigDecimal(java.math.BigDecimal) */
79 /* 1998.12.29 improve byteaddsub by using table lookup */
80 /* 1999.02.04 lostdigits=0 behaviour rounds instead of digits+1 guard */
81 /* 1999.02.05 cleaner code for BigDecimal(char[]) */
82 /* 1999.02.06 add javadoc comments */
83 /* 1999.02.11 format() changed from 7 to 2 method form */
84 /* 1999.03.05 null pointer checking is no longer explicit */
85 /* 1999.03.05 simplify; changes from discussion with J. Bloch: */
86 /* null no longer permitted for MathContext; drop boolean, */
87 /* byte, char, float, short constructor, deprecate double */
88 /* constructor, no blanks in string constructor, add */
89 /* offset and length version of char[] constructor; */
90 /* add valueOf(double); drop booleanValue, charValue; */
91 /* add ...Exact versions of remaining convertors */
92 /* 1999.03.13 add toBigIntegerExact */
93 /* 1999.03.13 1.00 release to IBM Centre for Java Technology */
94 /* 1999.05.27 1.01 correct 0-0.2 bug under scaled arithmetic */
95 /* 1999.06.29 1.02 constructors should not allow exponent > 9 digits */
96 /* 1999.07.03 1.03 lost digits should not be checked if digits=0 */
97 /* 1999.07.06 lost digits Exception message changed */
98 /* 1999.07.10 1.04 more work on 0-0.2 (scaled arithmetic) */
99 /* 1999.07.17 improve messages from pow method */
100 /* 1999.08.08 performance tweaks */
101 /* 1999.08.15 fastpath in multiply */
102 /* 1999.11.05 1.05 fix problem in intValueExact [e.g., 5555555555] */
103 /* 1999.12.22 1.06 remove multiply fastpath, and improve performance */
104 /* 2000.01.01 copyright update [Y2K has arrived] */
105 /* 2000.06.18 1.08 no longer deprecate BigDecimal(double) */
106 /* ------------------------------------------------------------------ */
109 * The <code>BigDecimal</code> class implements immutable arbitrary-precision decimal numbers. The methods of the
110 * <code>BigDecimal</code> class provide operations for fixed and floating point arithmetic, comparison, format
111 * conversions, and hashing.
113 * As the numbers are decimal, there is an exact correspondence between an instance of a <code>BigDecimal</code> object
114 * and its <code>String</code> representation; the <code>BigDecimal</code> class provides direct conversions to and from
115 * <code>String</code> and character array (<code>char[]</code>) objects, as well as conversions to and from the Java
116 * primitive types (which may not be exact) and <code>BigInteger</code>.
118 * In the descriptions of constructors and methods in this documentation, the value of a <code>BigDecimal</code> number
119 * object is shown as the result of invoking the <code>toString()</code> method on the object. The internal
120 * representation of a decimal number is neither defined nor exposed, and is not permitted to affect the result of any
123 * The floating point arithmetic provided by this class is defined by the ANSI X3.274-1996 standard, and is also
124 * documented at <code>http://www2.hursley.ibm.com/decimal</code> <br>
125 * <i>[This URL will change.]</i>
127 * <h3>Operator methods</h3>
129 * Operations on <code>BigDecimal</code> numbers are controlled by a {@link MathContext} object, which provides the
130 * context (precision and other information) for the operation. Methods that can take a <code>MathContext</code>
131 * parameter implement the standard arithmetic operators for <code>BigDecimal</code> objects and are known as
132 * <i>operator methods</i>. The default settings provided by the constant {@link MathContext#DEFAULT} (<code>digits=9,
133 * form=SCIENTIFIC, lostDigits=false, roundingMode=ROUND_HALF_UP</code>) perform general-purpose floating point
134 * arithmetic to nine digits of precision. The <code>MathContext</code> parameter must not be <code>null</code>.
136 * Each operator method also has a version provided which does not take a <code>MathContext</code> parameter. For this
137 * version of each method, the context settings used are <code>digits=0,
138 * form=PLAIN, lostDigits=false, roundingMode=ROUND_HALF_UP</code>; these settings perform fixed point arithmetic with
139 * unlimited precision, as defined for the original BigDecimal class in Java 1.1 and Java 1.2.
141 * For monadic operators, only the optional <code>MathContext</code> parameter is present; the operation acts upon the
144 * For dyadic operators, a <code>BigDecimal</code> parameter is always present; it must not be <code>null</code>. The
145 * operation acts with the current object being the left-hand operand and the <code>BigDecimal</code> parameter being
146 * the right-hand operand.
148 * For example, adding two <code>BigDecimal</code> objects referred to by the names <code>award</code> and
149 * <code>extra</code> could be written as any of:
153 * <br>award.add(extra, MathContext.DEFAULT)
154 * <br>award.add(extra, acontext)
157 * (where <code>acontext</code> is a <code>MathContext</code> object), which would return a <code>BigDecimal</code>
158 * object whose value is the result of adding <code>award</code> and <code>extra</code> under the appropriate context
161 * When a <code>BigDecimal</code> operator method is used, a set of rules define what the result will be (and, by
162 * implication, how the result would be represented as a character string). These rules are defined in the BigDecimal
163 * arithmetic documentation (see the URL above), but in summary:
165 * <li>Results are normally calculated with up to some maximum number of significant digits. For example, if the
166 * <code>MathContext</code> parameter for an operation were <code>MathContext.DEFAULT</code> then the result would be
167 * rounded to 9 digits; the division of 2 by 3 would then result in 0.666666667. <br>
168 * You can change the default of 9 significant digits by providing the method with a suitable <code>MathContext</code>
169 * object. This lets you calculate using as many digits as you need -- thousands, if necessary. Fixed point (scaled)
170 * arithmetic is indicated by using a <code>digits</code> setting of 0 (or omitting the <code>MathContext</code>
172 * Similarly, you can change the algorithm used for rounding from the default "classic" algorithm.
174 * In standard arithmetic (that is, when the <code>form</code> setting is not <code>PLAIN</code>), a zero result is
175 * always expressed as the single digit <code>'0'</code> (that is, with no sign, decimal point, or exponent part).
177 * Except for the division and power operators in standard arithmetic, trailing zeros are preserved (this is in contrast
178 * to binary floating point operations and most electronic calculators, which lose the information about trailing zeros
179 * in the fractional part of results). <br>
183 * new BigDecimal("2.40").add( new BigDecimal("2")) => "4.40"
184 * <br>new BigDecimal("2.40").subtract(new BigDecimal("2")) => "0.40"
185 * <br>new BigDecimal("2.40").multiply(new BigDecimal("2")) => "4.80"
186 * <br>new BigDecimal("2.40").divide( new BigDecimal("2"), def) => "1.2"
189 * where the value on the right of the <code>=></code> would be the result of the operation, expressed as a
190 * <code>String</code>, and <code>def</code> (in this and following examples) refers to <code>MathContext.DEFAULT</code>
191 * ). This preservation of trailing zeros is desirable for most calculations (including financial calculations). If
192 * necessary, trailing zeros may be easily removed using division by 1.
194 * In standard arithmetic, exponential form is used for a result depending on its value and the current setting of
195 * <code>digits</code> (the default is 9 digits). If the number of places needed before the decimal point exceeds the
196 * <code>digits</code> setting, or the absolute value of the number is less than <code>0.000001</code>, then the number
197 * will be expressed in exponential notation; thus
200 * new BigDecimal("1e+6").multiply(new BigDecimal("1e+6"), def)
203 * results in <code>1E+12</code> instead of <code>1000000000000</code>, and
206 * new BigDecimal("1").divide(new BigDecimal("3E+10"), def)
209 * results in <code>3.33333333E-11</code> instead of <code>0.0000000000333333333</code>.
211 * The form of the exponential notation (scientific or engineering) is determined by the <code>form</code> setting.
214 * The names of methods in this class follow the conventions established by <code>java.lang.Number</code>,
215 * <code>java.math.BigInteger</code>, and <code>java.math.BigDecimal</code> in Java 1.1 and Java 1.2.
218 * @author Mike Cowlishaw
222 public class BigDecimal extends java.lang.Number implements java.io.Serializable, java.lang.Comparable<BigDecimal> {
223 // private static final java.lang.String $0="BigDecimal.nrx";
225 /* ----- Constants ----- */
226 /* properties constant public */// useful to others
228 * The <code>BigDecimal</code> constant "0".
234 public static final com.ibm.icu.math.BigDecimal ZERO = new com.ibm.icu.math.BigDecimal((long) 0); // use long as we
237 // .. to be able to use this, for speed
240 * The <code>BigDecimal</code> constant "1".
246 public static final com.ibm.icu.math.BigDecimal ONE = new com.ibm.icu.math.BigDecimal((long) 1); // use long as we
249 // .. to be able to use this, for speed
252 * The <code>BigDecimal</code> constant "10".
258 public static final com.ibm.icu.math.BigDecimal TEN = new com.ibm.icu.math.BigDecimal(10);
260 // the rounding modes (copied here for upwards compatibility)
262 * Rounding mode to round to a more positive number.
264 * @see MathContext#ROUND_CEILING
267 public static final int ROUND_CEILING = com.ibm.icu.math.MathContext.ROUND_CEILING;
270 * Rounding mode to round towards zero.
272 * @see MathContext#ROUND_DOWN
275 public static final int ROUND_DOWN = com.ibm.icu.math.MathContext.ROUND_DOWN;
278 * Rounding mode to round to a more negative number.
280 * @see MathContext#ROUND_FLOOR
283 public static final int ROUND_FLOOR = com.ibm.icu.math.MathContext.ROUND_FLOOR;
286 * Rounding mode to round to nearest neighbor, where an equidistant value is rounded down.
288 * @see MathContext#ROUND_HALF_DOWN
291 public static final int ROUND_HALF_DOWN = com.ibm.icu.math.MathContext.ROUND_HALF_DOWN;
294 * Rounding mode to round to nearest neighbor, where an equidistant value is rounded to the nearest even neighbor.
296 * @see MathContext#ROUND_HALF_EVEN
299 public static final int ROUND_HALF_EVEN = com.ibm.icu.math.MathContext.ROUND_HALF_EVEN;
302 * Rounding mode to round to nearest neighbor, where an equidistant value is rounded up.
304 * @see MathContext#ROUND_HALF_UP
307 public static final int ROUND_HALF_UP = com.ibm.icu.math.MathContext.ROUND_HALF_UP;
310 * Rounding mode to assert that no rounding is necessary.
312 * @see MathContext#ROUND_UNNECESSARY
315 public static final int ROUND_UNNECESSARY = com.ibm.icu.math.MathContext.ROUND_UNNECESSARY;
318 * Rounding mode to round away from zero.
320 * @see MathContext#ROUND_UP
323 public static final int ROUND_UP = com.ibm.icu.math.MathContext.ROUND_UP;
325 /* properties constant private */// locals
326 private static final byte ispos = 1; // ind: indicates positive (must be 1)
327 private static final byte iszero = 0; // ind: indicates zero (must be 0)
328 private static final byte isneg = -1; // ind: indicates negative (must be -1)
329 // [later could add NaN, +/- infinity, here]
331 private static final int MinExp = -999999999; // minimum exponent allowed
332 private static final int MaxExp = 999999999; // maximum exponent allowed
333 private static final int MinArg = -999999999; // minimum argument integer
334 private static final int MaxArg = 999999999; // maximum argument integer
336 private static final com.ibm.icu.math.MathContext plainMC = new com.ibm.icu.math.MathContext(0,
337 com.ibm.icu.math.MathContext.PLAIN); // context for plain unlimited math
339 /* properties constant private unused */// present but not referenced
340 // Serialization version
341 private static final long serialVersionUID = 8245355804974198832L;
343 // private static final java.lang.String
344 // copyright=" Copyright (c) IBM Corporation 1996, 2000. All rights reserved. ";
346 /* properties static private */
347 // Precalculated constant arrays (used by byteaddsub)
348 private static byte bytecar[] = new byte[(90 + 99) + 1]; // carry/borrow array
349 private static byte bytedig[] = diginit(); // next digit array
351 /* ----- Instance properties [all private and immutable] ----- */
352 /* properties private */
355 * The indicator. This may take the values:
357 * <li>ispos -- the number is positive <li>iszero -- the number is zero <li>isneg -- the number is negative
362 private byte ind; // assumed undefined
363 // Note: some code below assumes IND = Sign [-1, 0, 1], at present.
364 // We only need two bits for this, but use a byte [also permits
365 // smooth future extension].
368 * The formatting style. This may take the values:
370 * <li>MathContext.PLAIN -- no exponent needed <li>MathContext.SCIENTIFIC -- scientific notation required <li>
371 * MathContext.ENGINEERING -- engineering notation required
374 * This property is an optimization; it allows us to defer number layout until it is actually needed as a string,
375 * hence avoiding unnecessary formatting.
379 private byte form = (byte) com.ibm.icu.math.MathContext.PLAIN; // assumed PLAIN
380 // We only need two bits for this, at present, but use a byte
381 // [again, to allow for smooth future extension]
384 * The value of the mantissa.
386 * Once constructed, this may become shared between several BigDecimal objects, so must not be altered.
388 * For efficiency (speed), this is a byte array, with each byte taking a value of 0 -> 9.
390 * If the first byte is 0 then the value of the number is zero (and mant.length=1, except when constructed from a
391 * plain number, for example, 0.000).
395 private byte mant[]; // assumed null
400 * For fixed point arithmetic, scale is <code>-exp</code>, and can apply to zero.
402 * Note that this property can have a value less than MinExp when the mantissa has more than one digit.
410 /* ---------------------------------------------------------------- */
412 /* ---------------------------------------------------------------- */
415 * Constructs a <code>BigDecimal</code> object from a <code>java.math.BigDecimal</code>.
417 * Constructs a <code>BigDecimal</code> as though the parameter had been represented as a <code>String</code> (using
418 * its <code>toString</code> method) and the {@link #BigDecimal(java.lang.String)} constructor had then been used.
419 * The parameter must not be <code>null</code>.
421 * <i>(Note: this constructor is provided only in the <code>com.ibm.icu.math</code> version of the BigDecimal class.
422 * It would not be present in a <code>java.math</code> version.)</i>
424 * @param bd The <code>BigDecimal</code> to be translated.
428 public BigDecimal(java.math.BigDecimal bd) {
434 * Constructs a <code>BigDecimal</code> object from a <code>BigInteger</code>, with scale 0.
436 * Constructs a <code>BigDecimal</code> which is the exact decimal representation of the <code>BigInteger</code>,
437 * with a scale of zero. The value of the <code>BigDecimal</code> is identical to the value of the <code>BigInteger
438 * </code>. The parameter must not be <code>null</code>.
440 * The <code>BigDecimal</code> will contain only decimal digits, prefixed with a leading minus sign (hyphen) if the
441 * <code>BigInteger</code> is negative. A leading zero will be present only if the <code>BigInteger</code> is zero.
443 * @param bi The <code>BigInteger</code> to be converted.
447 public BigDecimal(java.math.BigInteger bi) {
448 this(bi.toString(10));
455 * Constructs a <code>BigDecimal</code> object from a <code>BigInteger</code> and a scale.
457 * Constructs a <code>BigDecimal</code> which is the exact decimal representation of the <code>BigInteger</code>,
458 * scaled by the second parameter, which may not be negative. The value of the <code>BigDecimal</code> is the <code>
459 * BigInteger</code> divided by ten to the power of the scale. The <code>BigInteger</code> parameter must not be
462 * The <code>BigDecimal</code> will contain only decimal digits, (with an embedded decimal point followed by <code>
463 * scale</code> decimal digits if the scale is positive), prefixed with a leading minus sign (hyphen) if the <code>
464 * BigInteger</code> is negative. A leading zero will be present only if the <code>BigInteger</code> is zero.
466 * @param bi The <code>BigInteger</code> to be converted.
467 * @param scale The <code>int</code> specifying the scale.
468 * @throws NumberFormatException If the scale is negative.
472 public BigDecimal(java.math.BigInteger bi, int scale) {
473 this(bi.toString(10));
475 throw new java.lang.NumberFormatException("Negative scale:" + " " + scale);
476 exp = -scale; // exponent is -scale
481 * Constructs a <code>BigDecimal</code> object from an array of characters.
483 * Constructs a <code>BigDecimal</code> as though a <code>String</code> had been constructed from the character
484 * array and the {@link #BigDecimal(java.lang.String)} constructor had then been used. The parameter must not be
487 * Using this constructor is faster than using the <code>BigDecimal(String)</code> constructor if the string is
488 * already available in character array form.
490 * @param inchars The <code>char[]</code> array containing the number to be converted.
491 * @throws NumberFormatException If the parameter is not a valid number.
495 public BigDecimal(char inchars[]) {
496 this(inchars, 0, inchars.length);
501 * Constructs a <code>BigDecimal</code> object from an array of characters.
503 * Constructs a <code>BigDecimal</code> as though a <code>String</code> had been constructed from the character
504 * array (or a subarray of that array) and the {@link #BigDecimal(java.lang.String)} constructor had then been used.
505 * The first parameter must not be <code>null</code>, and the subarray must be wholly contained within it.
507 * Using this constructor is faster than using the <code>BigDecimal(String)</code> constructor if the string is
508 * already available within a character array.
510 * @param inchars The <code>char[]</code> array containing the number to be converted.
511 * @param offset The <code>int</code> offset into the array of the start of the number to be converted.
512 * @param length The <code>int</code> length of the number.
513 * @throws NumberFormatException If the parameter is not a valid number for any reason.
517 public BigDecimal(char inchars[], int offset, int length) {
526 boolean eneg = false;
533 // This is the primary constructor; all incoming strings end up
534 // here; it uses explicit (inline) parsing for speed and to avoid
535 // generating intermediate (temporary) objects of any kind.
536 // 1998.06.25: exponent form built only if E/e in string
537 // 1998.06.25: trailing zeros not removed for zero
538 // 1999.03.06: no embedded blanks; allow offset and length
540 bad(inchars); // bad conversion (empty string)
541 // [bad offset will raise array bounds exception]
543 /* Handle and step past sign */
544 ind = ispos; // assume positive
545 if (inchars[offset] == ('-')) {
548 bad(inchars); // nothing after sign
551 } else if (inchars[offset] == ('+')) {
554 bad(inchars); // nothing after sign
558 /* We're at the start of the number */
559 exotic = false; // have extra digits
560 hadexp = false; // had explicit exponent
561 d = 0; // count of digits found
562 dotoff = -1; // offset where dot was found
563 last = -1; // last character of mantissa
567 i: for (; $1 > 0; $1--, i++) {
569 if (si >= '0') // test for Arabic digit
572 d++; // still in mantissa
575 if (si == '.') { // record and ignore
577 bad(inchars); // two dots
578 dotoff = i - offset; // offset into mantissa
582 if (si != 'E') { // expect an extra digit
583 if ((!(UCharacter.isDigit(si))))
584 bad(inchars); // not a number
585 // defer the base 10 check until later to avoid extra method call
586 exotic = true; // will need conversion later
588 d++; // still in mantissa
591 /* Found 'e' or 'E' -- now process explicit exponent */
592 // 1998.07.11: sign no longer required
593 if ((i - offset) > (length - 2))
594 bad(inchars); // no room for even one digit
596 if ((inchars[i + 1]) == ('-')) {
599 } else if ((inchars[i + 1]) == ('+'))
603 // k is offset of first expected digit
604 elen = length - ((k - offset)); // possible number of digits
605 if ((elen == 0) | (elen > 9))
606 bad(inchars); // 0 or more than 9 digits
610 for (; $2 > 0; $2--, j++) {
613 bad(inchars); // always bad
614 if (sj > '9') { // maybe an exotic digit
615 if ((!(UCharacter.isDigit(sj))))
616 bad(inchars); // not a number
617 dvalue = UCharacter.digit(sj, 10); // check base
619 bad(inchars); // not base 10
621 dvalue = ((int) (sj)) - ((int) ('0'));
622 exp = (exp * 10) + dvalue;
626 exp = -exp; // was negative
627 hadexp = true; // remember we had one
628 break i; // we are done
632 /* Here when all inspected */
634 bad(inchars); // no mantissa digits
636 exp = (exp + dotoff) - d; // adjust exponent if had dot
638 /* strip leading zeros/dot (leave final if all 0's) */
642 i: for (; i <= $3; i++) {
648 } else if (si == '.') {
649 offset++; // step past dot
651 } else if (si <= '9')
654 if ((UCharacter.digit(si, 10)) != 0)
655 break i; // non-0 or bad
656 // is 0 .. strip like '0'
664 /* Create the mantissa array */
665 mant = new byte[d]; // we know the length
666 j = offset; // input offset
668 do { // slow: check for exotica
672 for (; $4 > 0; $4--, i++) {
677 mant[i] = (byte) (((int) (sj)) - ((int) ('0')));/* easy */
679 dvalue = UCharacter.digit(sj, 10);
681 bad(inchars); // not a number after all
682 mant[i] = (byte) dvalue;
694 for (; $5 > 0; $5--, i++) {
697 mant[i] = (byte) (((int) (inchars[j])) - ((int) ('0')));
704 /* Looks good. Set the sign indicator and form, as needed. */
705 // Trailing zeros are preserved
706 // The rule here for form is:
707 // If no E-notation, then request plain notation
708 // Otherwise act as though add(0,DEFAULT) and request scientific notation
709 // [form is already PLAIN]
711 ind = iszero; // force to show zero
712 // negative exponent is significant (e.g., -3 for 0.000) if plain
714 exp = 0; // positive exponent can be ignored
715 if (hadexp) { // zero becomes single digit from add
720 // [ind was set earlier]
721 // now determine form
723 form = (byte) com.ibm.icu.math.MathContext.SCIENTIFIC;
724 // 1999.06.29 check for overflow
725 mag = (exp + mant.length) - 1; // true exponent in scientific notation
726 if ((mag < MinExp) | (mag > MaxExp))
730 // say 'BD(c[]): mant[0] mantlen exp ind form:' mant[0] mant.length exp ind form
735 * Constructs a <code>BigDecimal</code> object directly from a <code>double</code>.
737 * Constructs a <code>BigDecimal</code> which is the exact decimal representation of the 64-bit signed binary
738 * floating point parameter.
740 * Note that this constructor it an exact conversion; it does not give the same result as converting <code>num
741 * </code> to a <code>String</code> using the <code>Double.toString()</code> method and then using the
742 * {@link #BigDecimal(java.lang.String)} constructor. To get that result, use the static {@link #valueOf(double)}
743 * method to construct a <code>BigDecimal</code> from a <code>double</code>.
745 * @param num The <code>double</code> to be converted.
746 * @throws NumberFormatException If the parameter is infinite or not a number.
750 public BigDecimal(double num) {
751 // 1999.03.06: use exactly the old algorithm
752 // 2000.01.01: note that this constructor does give an exact result,
753 // so perhaps it should not be deprecated
754 // 2000.06.18: no longer deprecated
755 this((new java.math.BigDecimal(num)).toString());
760 * Constructs a <code>BigDecimal</code> object directly from a <code>int</code>.
762 * Constructs a <code>BigDecimal</code> which is the exact decimal representation of the 32-bit signed binary
763 * integer parameter. The <code>BigDecimal</code> will contain only decimal digits, prefixed with a leading minus
764 * sign (hyphen) if the parameter is negative. A leading zero will be present only if the parameter is zero.
766 * @param num The <code>int</code> to be converted.
770 public BigDecimal(int num) {
774 // We fastpath commoners
778 // very common single digit case
783 } else if (num == 1) {
786 } else if (num == (-1)) {
793 mant[0] = (byte) num;
796 mant[0] = (byte) -num;
806 /* We work on negative numbers so we handle the most negative number */
811 ind = isneg;/* negative */// [0 case already handled]
812 // [it is quicker, here, to pre-calculate the length with
813 // one loop, then allocate exactly the right length of byte array,
814 // then re-fill it with another loop]
815 mun = num; // working copy
824 // i is the position of the leftmost digit placed
825 mant = new byte[10 - i];
829 mant[i] = (byte) -(((byte) (num % 10)));
839 * Constructs a <code>BigDecimal</code> object directly from a <code>long</code>.
841 * Constructs a <code>BigDecimal</code> which is the exact decimal representation of the 64-bit signed binary
842 * integer parameter. The <code>BigDecimal</code> will contain only decimal digits, prefixed with a leading minus
843 * sign (hyphen) if the parameter is negative. A leading zero will be present only if the parameter is zero.
845 * @param num The <code>long</code> to be converted.
849 public BigDecimal(long num) {
853 // Not really worth fastpathing commoners in this constructor [also,
854 // we use this to construct the static constants].
855 // This is much faster than: this(String.valueOf(num).toCharArray())
856 /* We work on negative num so we handle the most negative number */
863 ind = isneg;/* negative */
873 // i is the position of the leftmost digit placed
874 mant = new byte[19 - i];
878 mant[i] = (byte) -(((byte) (num % 10)));
888 * Constructs a <code>BigDecimal</code> object from a <code>String</code>.
890 * Constructs a <code>BigDecimal</code> from the parameter, which must not be <code>null</code> and must represent a
891 * valid <i>number</i>, as described formally in the documentation referred to {@link BigDecimal above}.
893 * In summary, numbers in <code>String</code> form must have at least one digit, may have a leading sign, may have a
894 * decimal point, and exponential notation may be used. They follow conventional syntax, and may not contain blanks.
896 * Some valid strings from which a <code>BigDecimal</code> might be constructed are:
900 * "0" -- Zero "12" -- A whole number "-76" -- A signed whole number "12.70" -- Some decimal places "+0.003" -- Plus
901 * sign is allowed "17." -- The same as 17 ".5" -- The same as 0.5 "4E+9" -- Exponential notation "0.73e-7" --
902 * Exponential notation
906 * (Exponential notation means that the number includes an optional sign and a power of ten following an
907 * '</code>E</code>' that indicates how the decimal point will be shifted. Thus the <code>"4E+9"</code> above is
908 * just a short way of writing <code>4000000000</code>, and the <code>"0.73e-7"</code> is short for <code>
909 * 0.000000073</code>.)
911 * The <code>BigDecimal</code> constructed from the String is in a standard form, with no blanks, as though the
912 * {@link #add(BigDecimal)} method had been used to add zero to the number with unlimited precision. If the string
913 * uses exponential notation (that is, includes an <code>e</code> or an <code>E</code>), then the <code>BigDecimal
914 * </code> number will be expressed in scientific notation (where the power of ten is adjusted so there is a single
915 * non-zero digit to the left of the decimal point); in this case if the number is zero then it will be expressed as
916 * the single digit 0, and if non-zero it will have an exponent unless that exponent would be 0. The exponent must
917 * fit in nine digits both before and after it is expressed in scientific notation.
919 * Any digits in the parameter must be decimal; that is, <code>Character.digit(c, 10)</code> (where </code>c</code>
920 * is the character in question) would not return -1.
922 * @param string The <code>String</code> to be converted.
923 * @throws NumberFormatException If the parameter is not a valid number.
927 public BigDecimal(java.lang.String string) {
928 this(string.toCharArray(), 0, string.length());
932 /* <sgml> Make a default BigDecimal object for local use. </sgml> */
934 private BigDecimal() {
939 /* ---------------------------------------------------------------- */
940 /* Operator methods [methods which take a context parameter] */
941 /* ---------------------------------------------------------------- */
944 * Returns a plain <code>BigDecimal</code> whose value is the absolute value of this <code>BigDecimal</code>.
946 * The same as {@link #abs(MathContext)}, where the context is <code>new MathContext(0, MathContext.PLAIN)</code>.
948 * The length of the decimal part (the scale) of the result will be <code>this.scale()</code>
950 * @return A <code>BigDecimal</code> whose value is the absolute value of this <code>BigDecimal</code>.
954 public com.ibm.icu.math.BigDecimal abs() {
955 return this.abs(plainMC);
959 * Returns a <code>BigDecimal</code> whose value is the absolute value of this <code>BigDecimal</code>.
961 * If the current object is zero or positive, then the same result as invoking the {@link #plus(MathContext)} method
962 * with the same parameter is returned. Otherwise, the same result as invoking the {@link #negate(MathContext)}
963 * method with the same parameter is returned.
965 * @param set The <code>MathContext</code> arithmetic settings.
966 * @return A <code>BigDecimal</code> whose value is the absolute value of this <code>BigDecimal</code>.
970 public com.ibm.icu.math.BigDecimal abs(com.ibm.icu.math.MathContext set) {
971 if (this.ind == isneg)
972 return this.negate(set);
973 return this.plus(set);
977 * Returns a plain <code>BigDecimal</code> whose value is <code>this+rhs</code>, using fixed point arithmetic.
979 * The same as {@link #add(BigDecimal, MathContext)}, where the <code>BigDecimal</code> is <code>rhs</code>, and the
980 * context is <code>new MathContext(0, MathContext.PLAIN)</code>.
982 * The length of the decimal part (the scale) of the result will be the maximum of the scales of the two operands.
984 * @param rhs The <code>BigDecimal</code> for the right hand side of the addition.
985 * @return A <code>BigDecimal</code> whose value is <code>this+rhs</code>, using fixed point arithmetic.
989 public com.ibm.icu.math.BigDecimal add(com.ibm.icu.math.BigDecimal rhs) {
990 return this.add(rhs, plainMC);
994 * Returns a <code>BigDecimal</code> whose value is <code>this+rhs</code>.
996 * Implements the addition (<b><code>+</code></b>) operator (as defined in the decimal documentation, see
997 * {@link BigDecimal class header}), and returns the result as a <code>BigDecimal</code> object.
999 * @param rhs The <code>BigDecimal</code> for the right hand side of the addition.
1000 * @param set The <code>MathContext</code> arithmetic settings.
1001 * @return A <code>BigDecimal</code> whose value is <code>this+rhs</code>.
1005 public com.ibm.icu.math.BigDecimal add(com.ibm.icu.math.BigDecimal rhs, com.ibm.icu.math.MathContext set) {
1006 com.ibm.icu.math.BigDecimal lhs;
1008 com.ibm.icu.math.BigDecimal res;
1023 /* determine requested digits and form */
1025 checkdigits(rhs, set.digits);
1026 lhs = this; // name for clarity and proxy
1028 /* Quick exit for add floating 0 */
1029 // plus() will optimize to return same object if possible
1031 if (set.form != com.ibm.icu.math.MathContext.PLAIN)
1032 return rhs.plus(set);
1034 if (set.form != com.ibm.icu.math.MathContext.PLAIN)
1035 return lhs.plus(set);
1037 /* Prepare numbers (round, unless unlimited precision) */
1038 reqdig = set.digits; // local copy (heavily used)
1040 if (lhs.mant.length > reqdig)
1041 lhs = clone(lhs).round(set);
1042 if (rhs.mant.length > reqdig)
1043 rhs = clone(rhs).round(set);
1044 // [we could reuse the new LHS for result in this case]
1047 res = new com.ibm.icu.math.BigDecimal(); // build result here
1050 * Now see how much we have to pad or truncate lhs or rhs in order to align the numbers. If one number is much
1051 * larger than the other, then the smaller cannot affect the answer [but we may still need to pad with up to
1052 * DIGITS trailing zeros].
1054 // Note sign may be 0 if digits (reqdig) is 0
1055 // usel and user will be the byte arrays passed to the adder; we'll
1056 // use them on all paths except quick exits
1058 usellen = lhs.mant.length;
1060 userlen = rhs.mant.length;
1063 if (lhs.exp == rhs.exp) {/* no padding needed */
1064 // This is the most common, and fastest, path
1066 } else if (lhs.exp > rhs.exp) { // need to pad lhs and/or truncate rhs
1067 newlen = (usellen + lhs.exp) - rhs.exp;
1069 * If, after pad, lhs would be longer than rhs by digits+1 or more (and digits>0) then rhs cannot
1070 * affect answer, so we only need to pad up to a length of DIGITS+1.
1072 if (newlen >= ((userlen + reqdig) + 1))
1074 // LHS is sufficient
1078 if (usellen < reqdig) { // need 0 padding
1079 res.mant = extend(lhs.mant, reqdig);
1080 res.exp = res.exp - ((reqdig - usellen));
1082 return res.finish(set, false);
1084 // RHS may affect result
1085 res.exp = rhs.exp; // expected final exponent
1086 if (newlen > (reqdig + 1))
1088 // LHS will be max; RHS truncated
1089 tlen = (newlen - reqdig) - 1; // truncation length
1090 userlen = userlen - tlen;
1091 res.exp = res.exp + tlen;
1092 newlen = reqdig + 1;
1094 if (newlen > usellen)
1095 usellen = newlen; // need to pad LHS
1096 } else { // need to pad rhs and/or truncate lhs
1097 newlen = (userlen + rhs.exp) - lhs.exp;
1098 if (newlen >= ((usellen + reqdig) + 1))
1100 // RHS is sufficient
1104 if (userlen < reqdig) { // need 0 padding
1105 res.mant = extend(rhs.mant, reqdig);
1106 res.exp = res.exp - ((reqdig - userlen));
1108 return res.finish(set, false);
1110 // LHS may affect result
1111 res.exp = lhs.exp; // expected final exponent
1112 if (newlen > (reqdig + 1))
1114 // RHS will be max; LHS truncated
1115 tlen = (newlen - reqdig) - 1; // truncation length
1116 usellen = usellen - tlen;
1117 res.exp = res.exp + tlen;
1118 newlen = reqdig + 1;
1120 if (newlen > userlen)
1121 userlen = newlen; // need to pad RHS
1126 /* OK, we have aligned mantissas. Now add or subtract. */
1127 // 1998.06.27 Sign may now be 0 [e.g., 0.000] .. treat as positive
1128 // 1999.05.27 Allow for 00 on lhs [is not larger than 2 on rhs]
1129 // 1999.07.10 Allow for 00 on rhs [is not larger than 2 on rhs]
1130 if (lhs.ind == iszero)
1133 res.ind = lhs.ind; // likely sign, all paths
1134 if (((lhs.ind == isneg) ? 1 : 0) == ((rhs.ind == isneg) ? 1 : 0)) // same sign, 0 non-negative
1137 do { // different signs, so subtraction is needed
1138 mult = -1; // will cause subtract
1140 * Before we can subtract we must determine which is the larger, as our add/subtract routine only
1141 * handles non-negative results so we may need to swap the operands.
1145 if (rhs.ind == iszero) {
1146 // original A bigger
1147 } else if ((usellen < userlen) | (lhs.ind == iszero)) { // original B bigger
1153 userlen = tlen; // ..
1154 res.ind = (byte) -res.ind; // and set sign
1155 } else if (usellen > userlen) {
1156 // original A bigger
1158 {/* logical lengths the same */// need compare
1159 /* may still need to swap: compare the strings */
1162 ea = usel.length - 1;
1163 eb = user.length - 1;
1169 if (ib > eb) {/* identical */
1170 if (set.form != com.ibm.icu.math.MathContext.PLAIN)
1172 // [if PLAIN we must do the subtract, in case of 0.000 results]
1182 if (ca < cb) {/* swap needed */
1188 userlen = tlen; // ..
1189 res.ind = (byte) -res.ind;
1193 /* mantissas the same, so far */
1198 } // lengths the same
1205 /* here, A is > B if subtracting */
1206 // add [A+B*1] or subtract [A+(B*-1)]
1207 res.mant = byteaddsub(usel, usellen, user, userlen, mult, false);
1208 // [reuse possible only after chop; accounting makes not worthwhile]
1210 // Finish() rounds before stripping leading 0's, then sets form, etc.
1211 return res.finish(set, false);
1215 * Compares this <code>BigDecimal</code> to another, using unlimited precision.
1217 * The same as {@link #compareTo(BigDecimal, MathContext)}, where the <code>BigDecimal</code> is <code>rhs</code>,
1218 * and the context is <code>new MathContext(0, MathContext.PLAIN)</code>.
1220 * @param rhs The <code>BigDecimal</code> for the right hand side of the comparison.
1221 * @return An <code>int</code> whose value is -1, 0, or 1 as <code>this</code> is numerically less than, equal to,
1222 * or greater than <code>rhs</code>.
1226 public int compareTo(com.ibm.icu.math.BigDecimal rhs) {
1227 return this.compareTo(rhs, plainMC);
1231 * Compares this <code>BigDecimal</code> to another.
1233 * Implements numeric comparison, (as defined in the decimal documentation, see {@link BigDecimal class header}),
1234 * and returns a result of type <code>int</code>.
1236 * The result will be:
1237 * <table cellpadding=2>
1239 * <td align=right><b>-1</b></td> <td>if the current object is less than the first parameter</td>
1242 * <td align=right><b>0</b></td> <td>if the current object is equal to the first parameter</td>
1245 * <td align=right><b>1</b></td> <td>if the current object is greater than the first parameter.</td>
1249 * A {@link #compareTo(BigDecimal)} method is also provided.
1251 * @param rhs The <code>BigDecimal</code> for the right hand side of the comparison.
1252 * @param set The <code>MathContext</code> arithmetic settings.
1253 * @return An <code>int</code> whose value is -1, 0, or 1 as <code>this</code> is numerically less than, equal to,
1254 * or greater than <code>rhs</code>.
1258 public int compareTo(com.ibm.icu.math.BigDecimal rhs, com.ibm.icu.math.MathContext set) {
1261 com.ibm.icu.math.BigDecimal newrhs;
1262 // rhs=null will raise NullPointerException, as per Comparable interface
1264 checkdigits(rhs, set.digits);
1265 // [add will recheck in slowpath cases .. but would report -rhs]
1266 if ((this.ind == rhs.ind) & (this.exp == rhs.exp)) {
1267 /* sign & exponent the same [very common] */
1268 thislength = this.mant.length;
1269 if (thislength < rhs.mant.length)
1270 return (byte) -this.ind;
1271 if (thislength > rhs.mant.length)
1274 * lengths are the same; we can do a straight mantissa compare unless maybe rounding [rounding is very
1277 if ((thislength <= set.digits) | (set.digits == 0)) {
1279 int $6 = thislength;
1281 for (; $6 > 0; $6--, i++) {
1282 if (this.mant[i] < rhs.mant[i])
1283 return (byte) -this.ind;
1284 if (this.mant[i] > rhs.mant[i])
1288 return 0; // identical
1290 /* drop through for full comparison */
1292 /* More fastpaths possible */
1293 if (this.ind < rhs.ind)
1295 if (this.ind > rhs.ind)
1298 /* carry out a subtract to make the comparison */
1299 newrhs = clone(rhs); // safe copy
1300 newrhs.ind = (byte) -newrhs.ind; // prepare to subtract
1301 return this.add(newrhs, set).ind; // add, and return sign of result
1305 * Returns a plain <code>BigDecimal</code> whose value is <code>this/rhs</code>, using fixed point arithmetic.
1307 * The same as {@link #divide(BigDecimal, int)}, where the <code>BigDecimal</code> is <code>rhs</code>, and the
1308 * rounding mode is {@link MathContext#ROUND_HALF_UP}.
1310 * The length of the decimal part (the scale) of the result will be the same as the scale of the current object, if
1311 * the latter were formatted without exponential notation.
1313 * @param rhs The <code>BigDecimal</code> for the right hand side of the division.
1314 * @return A plain <code>BigDecimal</code> whose value is <code>this/rhs</code>, using fixed point arithmetic.
1315 * @throws ArithmeticException If <code>rhs</code> is zero.
1319 public com.ibm.icu.math.BigDecimal divide(com.ibm.icu.math.BigDecimal rhs) {
1320 return this.dodivide('D', rhs, plainMC, -1);
1324 * Returns a plain <code>BigDecimal</code> whose value is <code>this/rhs</code>, using fixed point arithmetic and a
1327 * The same as {@link #divide(BigDecimal, int, int)}, where the <code>BigDecimal</code> is <code>rhs</code>, and the
1328 * second parameter is <code>this.scale()</code>, and the third is <code>round</code>.
1330 * The length of the decimal part (the scale) of the result will therefore be the same as the scale of the current
1331 * object, if the latter were formatted without exponential notation.
1334 * @param rhs The <code>BigDecimal</code> for the right hand side of the division.
1335 * @param round The <code>int</code> rounding mode to be used for the division (see the {@link MathContext} class).
1336 * @return A plain <code>BigDecimal</code> whose value is <code>this/rhs</code>, using fixed point arithmetic and
1337 * the specified rounding mode.
1338 * @throws IllegalArgumentException if <code>round</code> is not a valid rounding mode.
1339 * @throws ArithmeticException if <code>rhs</code> is zero.
1340 * @throws ArithmeticException if <code>round</code> is {@link MathContext#ROUND_UNNECESSARY} and <code>this.scale()</code> is insufficient to represent the result exactly.
1344 public com.ibm.icu.math.BigDecimal divide(com.ibm.icu.math.BigDecimal rhs, int round) {
1345 com.ibm.icu.math.MathContext set;
1346 set = new com.ibm.icu.math.MathContext(0, com.ibm.icu.math.MathContext.PLAIN, false, round); // [checks round,
1348 return this.dodivide('D', rhs, set, -1); // take scale from LHS
1352 * Returns a plain <code>BigDecimal</code> whose value is <code>this/rhs</code>, using fixed point arithmetic and a
1353 * given scale and rounding mode.
1355 * The same as {@link #divide(BigDecimal, MathContext)}, where the <code>BigDecimal</code> is <code>rhs</code>,
1356 * <code>new MathContext(0, MathContext.PLAIN, false, round)</code>, except that the length of the decimal part (the
1357 * scale) to be used for the result is explicit rather than being taken from <code>this</code>.
1359 * The length of the decimal part (the scale) of the result will be the same as the scale of the current object, if
1360 * the latter were formatted without exponential notation.
1363 * @param rhs The <code>BigDecimal</code> for the right hand side of the division.
1364 * @param scale The <code>int</code> scale to be used for the result.
1365 * @param round The <code>int</code> rounding mode to be used for the division (see the {@link MathContext} class).
1366 * @return A plain <code>BigDecimal</code> whose value is <code>this/rhs</code>, using fixed point arithmetic and
1367 * the specified rounding mode.
1368 * @throws IllegalArgumentException if <code>round</code> is not a valid rounding mode.
1369 * @throws ArithmeticException if <code>rhs</code> is zero.
1370 * @throws ArithmeticException if <code>scale</code> is negative.
1371 * @throws ArithmeticException if <code>round</code> is {@link MathContext#ROUND_UNNECESSARY} and <code>scale</code> is insufficient
1372 * to represent the result exactly.
1376 public com.ibm.icu.math.BigDecimal divide(com.ibm.icu.math.BigDecimal rhs, int scale, int round) {
1377 com.ibm.icu.math.MathContext set;
1379 throw new java.lang.ArithmeticException("Negative scale:" + " " + scale);
1380 set = new com.ibm.icu.math.MathContext(0, com.ibm.icu.math.MathContext.PLAIN, false, round); // [checks round]
1381 return this.dodivide('D', rhs, set, scale);
1385 * Returns a <code>BigDecimal</code> whose value is <code>this/rhs</code>.
1387 * Implements the division (<b><code>/</code></b>) operator (as defined in the decimal documentation, see
1388 * {@link BigDecimal class header}), and returns the result as a <code>BigDecimal</code> object.
1390 * @param rhs The <code>BigDecimal</code> for the right hand side of the division.
1391 * @param set The <code>MathContext</code> arithmetic settings.
1392 * @return A <code>BigDecimal</code> whose value is <code>this/rhs</code>.
1393 * @throws ArithmeticException if <code>rhs</code> is zero.
1397 public com.ibm.icu.math.BigDecimal divide(com.ibm.icu.math.BigDecimal rhs, com.ibm.icu.math.MathContext set) {
1398 return this.dodivide('D', rhs, set, -1);
1402 * Returns a plain <code>BigDecimal</code> whose value is the integer part of <code>this/rhs</code>.
1404 * The same as {@link #divideInteger(BigDecimal, MathContext)}, where the <code>BigDecimal</code> is <code>rhs
1405 * </code>, and the context is <code>new MathContext(0, MathContext.PLAIN)</code>.
1407 * @param rhs The <code>BigDecimal</code> for the right hand side of the integer division.
1408 * @return A <code>BigDecimal</code> whose value is the integer part of <code>this/rhs</code>.
1409 * @throws ArithmeticException if <code>rhs</code> is zero.
1413 public com.ibm.icu.math.BigDecimal divideInteger(com.ibm.icu.math.BigDecimal rhs) {
1414 // scale 0 to drop .000 when plain
1415 return this.dodivide('I', rhs, plainMC, 0);
1419 * Returns a <code>BigDecimal</code> whose value is the integer part of <code>this/rhs</code>.
1421 * Implements the integer division operator (as defined in the decimal documentation, see {@link BigDecimal class
1422 * header}), and returns the result as a <code>BigDecimal</code> object.
1424 * @param rhs The <code>BigDecimal</code> for the right hand side of the integer division.
1425 * @param set The <code>MathContext</code> arithmetic settings.
1426 * @return A <code>BigDecimal</code> whose value is the integer part of <code>this/rhs</code>.
1427 * @throws ArithmeticException if <code>rhs</code> is zero.
1428 * @throws ArithmeticException if the result will not fit in the number of digits specified for the context.
1432 public com.ibm.icu.math.BigDecimal divideInteger(com.ibm.icu.math.BigDecimal rhs, com.ibm.icu.math.MathContext set) {
1433 // scale 0 to drop .000 when plain
1434 return this.dodivide('I', rhs, set, 0);
1438 * Returns a plain <code>BigDecimal</code> whose value is the maximum of <code>this</code> and <code>rhs</code>.
1440 * The same as {@link #max(BigDecimal, MathContext)}, where the <code>BigDecimal</code> is <code>rhs</code>, and the
1441 * context is <code>new MathContext(0, MathContext.PLAIN)</code>.
1443 * @param rhs The <code>BigDecimal</code> for the right hand side of the comparison.
1444 * @return A <code>BigDecimal</code> whose value is the maximum of <code>this</code> and <code>rhs</code>.
1448 public com.ibm.icu.math.BigDecimal max(com.ibm.icu.math.BigDecimal rhs) {
1449 return this.max(rhs, plainMC);
1453 * Returns a <code>BigDecimal</code> whose value is the maximum of <code>this</code> and <code>rhs</code>.
1455 * Returns the larger of the current object and the first parameter.
1457 * If calling the {@link #compareTo(BigDecimal, MathContext)} method with the same parameters would return <code>1
1458 * </code> or <code>0</code>, then the result of calling the {@link #plus(MathContext)} method on the current object
1459 * (using the same <code>MathContext</code> parameter) is returned. Otherwise, the result of calling the
1460 * {@link #plus(MathContext)} method on the first parameter object (using the same <code>MathContext</code>
1461 * parameter) is returned.
1463 * @param rhs The <code>BigDecimal</code> for the right hand side of the comparison.
1464 * @param set The <code>MathContext</code> arithmetic settings.
1465 * @return A <code>BigDecimal</code> whose value is the maximum of <code>this</code> and <code>rhs</code>.
1469 public com.ibm.icu.math.BigDecimal max(com.ibm.icu.math.BigDecimal rhs, com.ibm.icu.math.MathContext set) {
1470 if ((this.compareTo(rhs, set)) >= 0)
1471 return this.plus(set);
1473 return rhs.plus(set);
1477 * Returns a plain <code>BigDecimal</code> whose value is the minimum of <code>this</code> and <code>rhs</code>.
1479 * The same as {@link #min(BigDecimal, MathContext)}, where the <code>BigDecimal</code> is <code>rhs</code>, and the
1480 * context is <code>new MathContext(0, MathContext.PLAIN)</code>.
1482 * @param rhs The <code>BigDecimal</code> for the right hand side of the comparison.
1483 * @return A <code>BigDecimal</code> whose value is the minimum of <code>this</code> and <code>rhs</code>.
1487 public com.ibm.icu.math.BigDecimal min(com.ibm.icu.math.BigDecimal rhs) {
1488 return this.min(rhs, plainMC);
1492 * Returns a <code>BigDecimal</code> whose value is the minimum of <code>this</code> and <code>rhs</code>.
1494 * Returns the smaller of the current object and the first parameter.
1496 * If calling the {@link #compareTo(BigDecimal, MathContext)} method with the same parameters would return <code>-1
1497 * </code> or <code>0</code>, then the result of calling the {@link #plus(MathContext)} method on the current object
1498 * (using the same <code>MathContext</code> parameter) is returned. Otherwise, the result of calling the
1499 * {@link #plus(MathContext)} method on the first parameter object (using the same <code>MathContext</code>
1500 * parameter) is returned.
1502 * @param rhs The <code>BigDecimal</code> for the right hand side of the comparison.
1503 * @param set The <code>MathContext</code> arithmetic settings.
1504 * @return A <code>BigDecimal</code> whose value is the minimum of <code>this</code> and <code>rhs</code>.
1508 public com.ibm.icu.math.BigDecimal min(com.ibm.icu.math.BigDecimal rhs, com.ibm.icu.math.MathContext set) {
1509 if ((this.compareTo(rhs, set)) <= 0)
1510 return this.plus(set);
1512 return rhs.plus(set);
1516 * Returns a plain <code>BigDecimal</code> whose value is <code>this*rhs</code>, using fixed point arithmetic.
1518 * The same as {@link #add(BigDecimal, MathContext)}, where the <code>BigDecimal</code> is <code>rhs</code>, and the
1519 * context is <code>new MathContext(0, MathContext.PLAIN)</code>.
1521 * The length of the decimal part (the scale) of the result will be the sum of the scales of the operands, if they
1522 * were formatted without exponential notation.
1524 * @param rhs The <code>BigDecimal</code> for the right hand side of the multiplication.
1525 * @return A <code>BigDecimal</code> whose value is <code>this*rhs</code>, using fixed point arithmetic.
1529 public com.ibm.icu.math.BigDecimal multiply(com.ibm.icu.math.BigDecimal rhs) {
1530 return this.multiply(rhs, plainMC);
1534 * Returns a <code>BigDecimal</code> whose value is <code>this*rhs</code>.
1536 * Implements the multiplication (<b><code> </code></b>) operator (as defined in the decimal documentation, see
1537 * {@link BigDecimal class header}), and returns the result as a <code>BigDecimal</code> object.
1539 * @param rhs The <code>BigDecimal</code> for the right hand side of the multiplication.
1540 * @param set The <code>MathContext</code> arithmetic settings.
1541 * @return A <code>BigDecimal</code> whose value is <code>this*rhs</code>.
1545 public com.ibm.icu.math.BigDecimal multiply(com.ibm.icu.math.BigDecimal rhs, com.ibm.icu.math.MathContext set) {
1546 com.ibm.icu.math.BigDecimal lhs;
1549 byte multer[] = null;
1550 byte multand[] = null;
1553 com.ibm.icu.math.BigDecimal res;
1558 checkdigits(rhs, set.digits);
1559 lhs = this; // name for clarity and proxy
1561 /* Prepare numbers (truncate, unless unlimited precision) */
1562 padding = 0; // trailing 0's to add
1563 reqdig = set.digits; // local copy
1565 if (lhs.mant.length > reqdig)
1566 lhs = clone(lhs).round(set);
1567 if (rhs.mant.length > reqdig)
1568 rhs = clone(rhs).round(set);
1569 // [we could reuse the new LHS for result in this case]
1570 } else {/* unlimited */
1571 // fixed point arithmetic will want every trailing 0; we add these
1572 // after the calculation rather than before, for speed.
1574 padding = padding + lhs.exp;
1576 padding = padding + rhs.exp;
1579 // For best speed, as in DMSRCN, we use the shorter number as the
1580 // multiplier and the longer as the multiplicand.
1581 // 1999.12.22: We used to special case when the result would fit in
1582 // a long, but with Java 1.3 this gave no advantage.
1583 if (lhs.mant.length < rhs.mant.length) {
1591 /* Calculate how long result byte array will be */
1592 multandlen = (multer.length + multand.length) - 1; // effective length
1593 // optimize for 75% of the cases where a carry is expected...
1594 if ((multer[0] * multand[0]) > 9)
1595 acclen = multandlen + 1;
1597 acclen = multandlen;
1599 /* Now the main long multiplication loop */
1600 res = new com.ibm.icu.math.BigDecimal(); // where we'll build result
1601 acc = new byte[acclen]; // accumulator, all zeros
1602 // 1998.07.01: calculate from left to right so that accumulator goes
1603 // to likely final length on first addition; this avoids a one-digit
1604 // extension (and object allocation) each time around the loop.
1605 // Initial number therefore has virtual zeros added to right.
1607 int $7 = multer.length;
1609 for (; $7 > 0; $7--, n++) {
1611 if (mult != 0) { // [optimization]
1612 // accumulate [accumulator is reusable array]
1613 acc = byteaddsub(acc, acc.length, multand, multandlen, mult, true);
1615 // divide multiplicand by 10 for next digit to right
1616 multandlen--; // 'virtual length'
1620 res.ind = (byte) (lhs.ind * rhs.ind); // final sign
1621 res.exp = (lhs.exp + rhs.exp) - padding; // final exponent
1622 // [overflow is checked by finish]
1624 /* add trailing zeros to the result, if necessary */
1628 res.mant = extend(acc, acc.length + padding); // add trailing 0s
1629 return res.finish(set, false);
1633 * Returns a plain <code>BigDecimal</code> whose value is <code>-this</code>.
1635 * The same as {@link #negate(MathContext)}, where the context is <code>new MathContext(0, MathContext.PLAIN)</code>
1638 * The length of the decimal part (the scale) of the result will be be <code>this.scale()</code>
1641 * @return A <code>BigDecimal</code> whose value is <code>-this</code>.
1645 public com.ibm.icu.math.BigDecimal negate() {
1646 return this.negate(plainMC);
1650 * Returns a <code>BigDecimal</code> whose value is <code>-this</code>.
1652 * Implements the negation (Prefix <b><code>-</code></b>) operator (as defined in the decimal documentation, see
1653 * {@link BigDecimal class header}), and returns the result as a <code>BigDecimal</code> object.
1655 * @param set The <code>MathContext</code> arithmetic settings.
1656 * @return A <code>BigDecimal</code> whose value is <code>-this</code>.
1660 public com.ibm.icu.math.BigDecimal negate(com.ibm.icu.math.MathContext set) {
1661 com.ibm.icu.math.BigDecimal res;
1662 // Originally called minus(), changed to matched Java precedents
1663 // This simply clones, flips the sign, and possibly rounds
1665 checkdigits((com.ibm.icu.math.BigDecimal) null, set.digits);
1666 res = clone(this); // safe copy
1667 res.ind = (byte) -res.ind;
1668 return res.finish(set, false);
1672 * Returns a plain <code>BigDecimal</code> whose value is <code>+this</code>. Note that <code>this</code> is not
1673 * necessarily a plain <code>BigDecimal</code>, but the result will always be.
1675 * The same as {@link #plus(MathContext)}, where the context is <code>new MathContext(0, MathContext.PLAIN)</code>.
1677 * The length of the decimal part (the scale) of the result will be be <code>this.scale()</code>
1679 * @return A <code>BigDecimal</code> whose value is <code>+this</code>.
1683 public com.ibm.icu.math.BigDecimal plus() {
1684 return this.plus(plainMC);
1688 * Returns a <code>BigDecimal</code> whose value is <code>+this</code>.
1690 * Implements the plus (Prefix <b><code>+</code></b>) operator (as defined in the decimal documentation, see
1691 * {@link BigDecimal class header}), and returns the result as a <code>BigDecimal</code> object.
1693 * This method is useful for rounding or otherwise applying a context to a decimal value.
1695 * @param set The <code>MathContext</code> arithmetic settings.
1696 * @return A <code>BigDecimal</code> whose value is <code>+this</code>.
1700 public com.ibm.icu.math.BigDecimal plus(com.ibm.icu.math.MathContext set) {
1701 // This clones and forces the result to the new settings
1702 // May return same object
1704 checkdigits((com.ibm.icu.math.BigDecimal) null, set.digits);
1705 // Optimization: returns same object for some common cases
1706 if (set.form == com.ibm.icu.math.MathContext.PLAIN)
1707 if (this.form == com.ibm.icu.math.MathContext.PLAIN) {
1708 if (this.mant.length <= set.digits)
1710 if (set.digits == 0)
1713 return clone(this).finish(set, false);
1717 * Returns a plain <code>BigDecimal</code> whose value is <code>this**rhs</code>, using fixed point arithmetic.
1719 * The same as {@link #pow(BigDecimal, MathContext)}, where the <code>BigDecimal</code> is <code>rhs</code>, and the
1720 * context is <code>new MathContext(0, MathContext.PLAIN)</code>.
1722 * The parameter is the power to which the <code>this</code> will be raised; it must be in the range 0 through
1723 * 999999999, and must have a decimal part of zero. Note that these restrictions may be removed in the future, so
1724 * they should not be used as a test for a whole number.
1726 * In addition, the power must not be negative, as no <code>MathContext</code> is used and so the result would then
1729 * @param rhs The <code>BigDecimal</code> for the right hand side of the operation (the power).
1730 * @return A <code>BigDecimal</code> whose value is <code>this**rhs</code>, using fixed point arithmetic.
1731 * @throws ArithmeticException if <code>rhs</code> is out of range or is not a whole number.
1735 public com.ibm.icu.math.BigDecimal pow(com.ibm.icu.math.BigDecimal rhs) {
1736 return this.pow(rhs, plainMC);
1739 // The name for this method is inherited from the precedent set by the
1740 // BigInteger and Math classes.
1743 * Returns a <code>BigDecimal</code> whose value is <code>this**rhs</code>.
1745 * Implements the power (<b><code> </code></b>) operator (as defined in the decimal documentation, see
1746 * {@link BigDecimal class header}), and returns the result as a <code>BigDecimal</code> object.
1748 * The first parameter is the power to which the <code>this</code> will be raised; it must be in the range
1749 * -999999999 through 999999999, and must have a decimal part of zero. Note that these restrictions may be removed
1750 * in the future, so they should not be used as a test for a whole number.
1752 * If the <code>digits</code> setting of the <code>MathContext</code> parameter is 0, the power must be zero or
1755 * @param rhs The <code>BigDecimal</code> for the right hand side of the operation (the power).
1756 * @param set The <code>MathContext</code> arithmetic settings.
1757 * @return A <code>BigDecimal</code> whose value is <code>this**rhs</code>.
1758 * @throws ArithmeticException if <code>rhs</code> is out of range or is not a whole number.
1762 public com.ibm.icu.math.BigDecimal pow(com.ibm.icu.math.BigDecimal rhs, com.ibm.icu.math.MathContext set) {
1764 com.ibm.icu.math.BigDecimal lhs;
1768 com.ibm.icu.math.MathContext workset;
1769 com.ibm.icu.math.BigDecimal res;
1773 checkdigits(rhs, set.digits);
1774 n = rhs.intcheck(MinArg, MaxArg); // check RHS by the rules
1775 lhs = this; // clarified name
1777 reqdig = set.digits; // local copy (heavily used)
1779 if (rhs.ind == isneg)
1780 throw new java.lang.ArithmeticException("Negative power:" + " " + rhs.toString());
1782 } else {/* non-0 digits */
1783 if ((rhs.mant.length + rhs.exp) > reqdig)
1784 throw new java.lang.ArithmeticException("Too many digits:" + " " + rhs.toString());
1786 /* Round the lhs to DIGITS if need be */
1787 if (lhs.mant.length > reqdig)
1788 lhs = clone(lhs).round(set);
1790 /* L for precision calculation [see ANSI X3.274-1996] */
1791 L = rhs.mant.length + rhs.exp; // length without decimal zeros/exp
1792 workdigits = (reqdig + L) + 1; // calculate the working DIGITS
1795 /* Create a copy of set for working settings */
1796 // Note: no need to check for lostDigits again.
1797 // 1999.07.17 Note: this construction must follow RHS check
1798 workset = new com.ibm.icu.math.MathContext(workdigits, set.form, false, set.roundingMode);
1800 res = ONE; // accumulator
1802 return res; // x**0 == 1
1804 n = -n; // [rhs.ind records the sign]
1805 seenbit = false; // set once we've seen a 1-bit
1808 i: for (;; i++) { // for each bit [top bit ignored]
1809 n = n + n; // shift left 1 bit
1810 if (n < 0) { // top bit is set
1811 seenbit = true; // OK, we're off
1812 res = res.multiply(lhs, workset); // acc=acc*x
1815 break i; // that was the last bit
1817 continue i; // we don't have to square 1
1818 res = res.multiply(res, workset); // acc=acc*acc [square]
1821 if (rhs.ind < 0) // was a **-n [hence digits>0]
1822 res = ONE.divide(res, workset); // .. so acc=1/acc
1823 return res.finish(set, true); // round and strip [original digits]
1827 * Returns a plain <code>BigDecimal</code> whose value is the remainder of <code>this/rhs</code>, using fixed point
1830 * The same as {@link #remainder(BigDecimal, MathContext)}, where the <code>BigDecimal</code> is <code>rhs</code>,
1831 * and the context is <code>new MathContext(0, MathContext.PLAIN)</code>.
1833 * This is not the modulo operator -- the result may be negative.
1835 * @param rhs The <code>BigDecimal</code> for the right hand side of the remainder operation.
1836 * @return A <code>BigDecimal</code> whose value is the remainder of <code>this/rhs</code>, using fixed point
1838 * @throws ArithmeticException if <code>rhs</code> is zero.
1842 public com.ibm.icu.math.BigDecimal remainder(com.ibm.icu.math.BigDecimal rhs) {
1843 return this.dodivide('R', rhs, plainMC, -1);
1847 * Returns a <code>BigDecimal</code> whose value is the remainder of <code>this/rhs</code>.
1849 * Implements the remainder operator (as defined in the decimal documentation, see {@link BigDecimal class header}),
1850 * and returns the result as a <code>BigDecimal</code> object.
1852 * This is not the modulo operator -- the result may be negative.
1854 * @param rhs The <code>BigDecimal</code> for the right hand side of the remainder operation.
1855 * @param set The <code>MathContext</code> arithmetic settings.
1856 * @return A <code>BigDecimal</code> whose value is the remainder of <code>this+rhs</code>.
1857 * @throws ArithmeticException if <code>rhs</code> is zero.
1858 * @throws ArithmeticException if the integer part of the result will not fit in the number of digits specified for the context.
1862 public com.ibm.icu.math.BigDecimal remainder(com.ibm.icu.math.BigDecimal rhs, com.ibm.icu.math.MathContext set) {
1863 return this.dodivide('R', rhs, set, -1);
1867 * Returns a plain <code>BigDecimal</code> whose value is <code>this-rhs</code>, using fixed point arithmetic.
1869 * The same as {@link #subtract(BigDecimal, MathContext)}, where the <code>BigDecimal</code> is <code>rhs</code>,
1870 * and the context is <code>new MathContext(0, MathContext.PLAIN)</code>.
1872 * The length of the decimal part (the scale) of the result will be the maximum of the scales of the two operands.
1874 * @param rhs The <code>BigDecimal</code> for the right hand side of the subtraction.
1875 * @return A <code>BigDecimal</code> whose value is <code>this-rhs</code>, using fixed point arithmetic.
1879 public com.ibm.icu.math.BigDecimal subtract(com.ibm.icu.math.BigDecimal rhs) {
1880 return this.subtract(rhs, plainMC);
1884 * Returns a <code>BigDecimal</code> whose value is <code>this-rhs</code>.
1886 * Implements the subtraction (<b><code>-</code></b>) operator (as defined in the decimal documentation, see
1887 * {@link BigDecimal class header}), and returns the result as a <code>BigDecimal</code> object.
1889 * @param rhs The <code>BigDecimal</code> for the right hand side of the subtraction.
1890 * @param set The <code>MathContext</code> arithmetic settings.
1891 * @return A <code>BigDecimal</code> whose value is <code>this-rhs</code>.
1895 public com.ibm.icu.math.BigDecimal subtract(com.ibm.icu.math.BigDecimal rhs, com.ibm.icu.math.MathContext set) {
1896 com.ibm.icu.math.BigDecimal newrhs;
1898 checkdigits(rhs, set.digits);
1899 // [add will recheck .. but would report -rhs]
1900 /* carry out the subtraction */
1901 // we could fastpath -0, but it is too rare.
1902 newrhs = clone(rhs); // safe copy
1903 newrhs.ind = (byte) -newrhs.ind; // prepare to subtract
1904 return this.add(newrhs, set); // arithmetic
1907 /* ---------------------------------------------------------------- */
1909 /* ---------------------------------------------------------------- */
1912 * Converts this <code>BigDecimal</code> to a <code>byte</code>. If the <code>BigDecimal</code> has a non-zero
1913 * decimal part or is out of the possible range for a <code>byte</code> (8-bit signed integer) result then an <code>
1914 * ArithmeticException</code> is thrown.
1916 * @return A <code>byte</code> equal in value to <code>this</code>.
1917 * @throws ArithmeticException if <code>this</code> has a non-zero decimal part, or will not fit in a <code>byte</code>.
1921 public byte byteValueExact() {
1923 num = this.intValueExact(); // will check decimal part too
1924 if ((num > 127) | (num < (-128)))
1925 throw new java.lang.ArithmeticException("Conversion overflow:" + " " + this.toString());
1930 * Converts this <code>BigDecimal</code> to a <code>double</code>. If the <code>BigDecimal</code> is out of the
1931 * possible range for a <code>double</code> (64-bit signed floating point) result then an <code>ArithmeticException
1932 * </code> is thrown.
1934 * The double produced is identical to result of expressing the <code>BigDecimal</code> as a <code>String</code> and
1935 * then converting it using the <code>Double(String)</code> constructor; this can result in values of <code>
1936 * Double.NEGATIVE_INFINITY</code> or <code>Double.POSITIVE_INFINITY</code>.
1938 * @return A <code>double</code> corresponding to <code>this</code>.
1942 public double doubleValue() {
1943 // We go via a String [as does BigDecimal in JDK 1.2]
1944 // Next line could possibly raise NumberFormatException
1945 return java.lang.Double.valueOf(this.toString()).doubleValue();
1949 * Compares this <code>BigDecimal</code> with <code>rhs</code> for equality.
1951 * If the parameter is <code>null</code>, or is not an instance of the BigDecimal type, or is not exactly equal to
1952 * the current <code>BigDecimal</code> object, then <i>false</i> is returned. Otherwise, <i>true</i> is returned.
1954 * "Exactly equal", here, means that the <code>String</code> representations of the <code>BigDecimal</code> numbers
1955 * are identical (they have the same characters in the same sequence).
1957 * The {@link #compareTo(BigDecimal, MathContext)} method should be used for more general comparisons.
1959 * @param obj The <code>Object</code> for the right hand side of the comparison.
1960 * @return A <code>boolean</code> whose value <i>true</i> if and only if the operands have identical string
1962 * @throws ClassCastException if <code>rhs</code> cannot be cast to a <code>BigDecimal</code> object.
1964 * @see #compareTo(BigDecimal)
1965 * @see #compareTo(BigDecimal, MathContext)
1968 public boolean equals(java.lang.Object obj) {
1969 com.ibm.icu.math.BigDecimal rhs;
1973 // We are equal iff toString of both are exactly the same
1975 return false; // not equal
1976 if ((!(((obj instanceof com.ibm.icu.math.BigDecimal)))))
1977 return false; // not a decimal
1978 rhs = (com.ibm.icu.math.BigDecimal) obj; // cast; we know it will work
1979 if (this.ind != rhs.ind)
1980 return false; // different signs never match
1981 if (((this.mant.length == rhs.mant.length) & (this.exp == rhs.exp)) & (this.form == rhs.form))
1983 { // mantissas say all
1984 // here with equal-length byte arrays to compare
1986 int $8 = this.mant.length;
1988 for (; $8 > 0; $8--, i++) {
1989 if (this.mant[i] != rhs.mant[i])
1993 } else { // need proper layout
1994 lca = this.layout(); // layout to character array
1996 if (lca.length != rca.length)
1997 return false; // mismatch
1998 // here with equal-length character arrays to compare
2000 int $9 = lca.length;
2002 for (; $9 > 0; $9--, i++) {
2003 if (lca[i] != rca[i])
2008 return true; // arrays have identical content
2012 * Converts this <code>BigDecimal</code> to a <code>float</code>. If the <code>BigDecimal</code> is out of the
2013 * possible range for a <code>float</code> (32-bit signed floating point) result then an <code>ArithmeticException
2014 * </code> is thrown.
2016 * The float produced is identical to result of expressing the <code>BigDecimal</code> as a <code>String</code> and
2017 * then converting it using the <code>Float(String)</code> constructor; this can result in values of <code>
2018 * Float.NEGATIVE_INFINITY</code> or <code>Float.POSITIVE_INFINITY</code>.
2020 * @return A <code>float</code> corresponding to <code>this</code>.
2024 public float floatValue() {
2025 return java.lang.Float.valueOf(this.toString()).floatValue();
2029 * Returns the <code>String</code> representation of this <code>BigDecimal</code>, modified by layout parameters.
2031 * <i>This method is provided as a primitive for use by more sophisticated classes, such as <code>DecimalFormat
2032 * </code>, that can apply locale-sensitive editing of the result. The level of formatting that it provides is a
2033 * necessary part of the BigDecimal class as it is sensitive to and must follow the calculation and rounding rules
2034 * for BigDecimal arithmetic. However, if the function is provided elsewhere, it may be removed from this class.
2037 * The parameters, for both forms of the <code>format</code> method are all of type <code>int</code>. A value of -1
2038 * for any parameter indicates that the default action or value for that parameter should be used.
2040 * The parameters, <code>before</code> and <code>after</code>, specify the number of characters to be used for the
2041 * integer part and decimal part of the result respectively. Exponential notation is not used. If either parameter
2042 * is -1 (which indicates the default action), the number of characters used will be exactly as many as are needed
2045 * <code>before</code> must be a positive number; if it is larger than is needed to contain the integer part, that
2046 * part is padded on the left with blanks to the requested length. If <code>before</code> is not large enough to
2047 * contain the integer part of the number (including the sign, for negative numbers) an exception is thrown.
2049 * <code>after</code> must be a non-negative number; if it is not the same size as the decimal part of the number,
2050 * the number will be rounded (or extended with zeros) to fit. Specifying 0 for <code>after</code> will cause the
2051 * number to be rounded to an integer (that is, it will have no decimal part or decimal point). The rounding method
2052 * will be the default, <code>MathContext.ROUND_HALF_UP</code>.
2054 * Other rounding methods, and the use of exponential notation, can be selected by using
2055 * {@link #format(int,int,int,int,int,int)}. Using the two-parameter form of the method has exactly the same effect
2056 * as using the six-parameter form with the final four parameters all being -1.
2058 * @param before The <code>int</code> specifying the number of places before the decimal point. Use -1 for 'as many as are needed'.
2059 * @param after The <code>int</code> specifying the number of places after the decimal point. Use -1 for 'as many as are needed'.
2060 * @return A <code>String</code> representing this <code>BigDecimal</code>, laid out according to the specified parameters
2061 * @throws ArithmeticException if the number cannot be laid out as requested.
2062 * @throws IllegalArgumentException if a parameter is out of range.
2068 public java.lang.String format(int before, int after) {
2069 return format(before, after, -1, -1, com.ibm.icu.math.MathContext.SCIENTIFIC, ROUND_HALF_UP);
2073 * Returns the <code>String</code> representation of this <code>BigDecimal</code>, modified by layout parameters and
2074 * allowing exponential notation.
2076 * <i>This method is provided as a primitive for use by more sophisticated classes, such as <code>DecimalFormat
2077 * </code>, that can apply locale-sensitive editing of the result. The level of formatting that it provides is a
2078 * necessary part of the BigDecimal class as it is sensitive to and must follow the calculation and rounding rules
2079 * for BigDecimal arithmetic. However, if the function is provided elsewhere, it may be removed from this class.
2082 * The parameters are all of type <code>int</code>. A value of -1 for any parameter indicates that the default
2083 * action or value for that parameter should be used.
2085 * The first two parameters (<code>before</code> and <code>after</code>) specify the number of characters to be used
2086 * for the integer part and decimal part of the result respectively, as defined for {@link #format(int,int)}. If
2087 * either of these is -1 (which indicates the default action), the number of characters used will be exactly as many
2088 * as are needed for that part.
2090 * The remaining parameters control the use of exponential notation and rounding. Three (<code>explaces</code>,
2091 * <code>exdigits</code>, and <code>exform</code>) control the exponent part of the result. As before, the default
2092 * action for any of these parameters may be selected by using the value -1.
2094 * <code>explaces</code> must be a positive number; it sets the number of places (digits after the sign of the
2095 * exponent) to be used for any exponent part, the default (when <code>explaces</code> is -1) being to use as many
2096 * as are needed. If <code>explaces</code> is not -1, space is always reserved for an exponent; if one is not needed
2097 * (for example, if the exponent will be 0) then <code>explaces</code>+2 blanks are appended to the result. <!--
2098 * (This preserves vertical alignment of similarly formatted numbers in a monospace font.) --> If <code>explaces
2099 * </code> is not -1 and is not large enough to contain the exponent, an exception is thrown.
2101 * <code>exdigits</code> sets the trigger point for use of exponential notation. If, before any rounding, the number
2102 * of places needed before the decimal point exceeds <code>exdigits</code>, or if the absolute value of the result
2103 * is less than <code>0.000001</code>, then exponential form will be used, provided that <code>exdigits</code> was
2104 * specified. When <code>exdigits</code> is -1, exponential notation will never be used. If 0 is specified for
2105 * <code>exdigits</code>, exponential notation is always used unless the exponent would be 0.
2107 * <code>exform</code> sets the form for exponential notation (if needed). It may be either
2108 * {@link MathContext#SCIENTIFIC} or {@link MathContext#ENGINEERING}. If the latter, engineering, form is requested,
2109 * up to three digits (plus sign, if negative) may be needed for the integer part of the result (<code>before</code>
2110 * ). Otherwise, only one digit (plus sign, if negative) is needed.
2112 * Finally, the sixth argument, <code>exround</code>, selects the rounding algorithm to be used, and must be one of
2113 * the values indicated by a public constant in the {@link MathContext} class whose name starts with <code>ROUND_
2114 * </code>. The default (<code>ROUND_HALF_UP</code>) may also be selected by using the value -1, as before.
2116 * The special value <code>MathContext.ROUND_UNNECESSARY</code> may be used to detect whether non-zero digits are
2117 * discarded -- if <code>exround</code> has this value than if non-zero digits would be discarded (rounded) during
2118 * formatting then an <code>ArithmeticException</code> is thrown.
2120 * @param before The <code>int</code> specifying the number of places before the decimal point. Use -1 for 'as many as
2122 * @param after The <code>int</code> specifying the number of places after the decimal point. Use -1 for 'as many as
2124 * @param explaces The <code>int</code> specifying the number of places to be used for any exponent. Use -1 for 'as many
2126 * @param exdigits The <code>int</code> specifying the trigger (digits before the decimal point) which if exceeded causes
2127 * exponential notation to be used. Use 0 to force exponential notation. Use -1 to force plain notation
2128 * (no exponential notation).
2129 * @param exformint The <code>int</code> specifying the form of exponential notation to be used (
2130 * {@link MathContext#SCIENTIFIC} or {@link MathContext#ENGINEERING}).
2131 * @param exround The <code>int</code> specifying the rounding mode to use. Use -1 for the default,
2132 * {@link MathContext#ROUND_HALF_UP}.
2133 * @return A <code>String</code> representing this <code>BigDecimal</code>, laid out according to the specified
2135 * @throws ArithmeticException if the number cannot be laid out as requested.
2136 * @throws IllegalArgumentException if a parameter is out of range.
2142 public java.lang.String format(int before, int after, int explaces, int exdigits, int exformint, int exround) {
2143 com.ibm.icu.math.BigDecimal num;
2147 byte newmant[] = null;
2157 /* Check arguments */
2158 if ((before < (-1)) | (before == 0))
2159 badarg("format", 1, java.lang.String.valueOf(before));
2161 badarg("format", 2, java.lang.String.valueOf(after));
2162 if ((explaces < (-1)) | (explaces == 0))
2163 badarg("format", 3, java.lang.String.valueOf(explaces));
2164 if (exdigits < (-1))
2165 badarg("format", 4, java.lang.String.valueOf(explaces));
2167 if (exformint == com.ibm.icu.math.MathContext.SCIENTIFIC) {
2168 } else if (exformint == com.ibm.icu.math.MathContext.ENGINEERING) {
2169 } else if (exformint == (-1))
2170 exformint = com.ibm.icu.math.MathContext.SCIENTIFIC;
2171 // note PLAIN isn't allowed
2173 badarg("format", 5, java.lang.String.valueOf(exformint));
2176 // checking the rounding mode is done by trying to construct a
2177 // MathContext object with that mode; it will fail if bad
2178 if (exround != ROUND_HALF_UP) {
2179 try { // if non-default...
2180 if (exround == (-1))
2181 exround = ROUND_HALF_UP;
2183 new com.ibm.icu.math.MathContext(9, com.ibm.icu.math.MathContext.SCIENTIFIC, false, exround);
2184 } catch (java.lang.IllegalArgumentException $10) {
2185 badarg("format", 6, java.lang.String.valueOf(exround));
2189 num = clone(this); // make private copy
2192 * Here: num is BigDecimal to format before is places before point [>0] after is places after point [>=0]
2193 * explaces is exponent places [>0] exdigits is exponent digits [>=0] exformint is exponent form [one of two]
2194 * exround is rounding mode [one of eight] 'before' through 'exdigits' are -1 if not specified
2197 /* determine form */
2200 if (exdigits == (-1))
2201 num.form = (byte) com.ibm.icu.math.MathContext.PLAIN;
2202 else if (num.ind == iszero)
2203 num.form = (byte) com.ibm.icu.math.MathContext.PLAIN;
2205 // determine whether triggers
2206 mag = num.exp + num.mant.length;
2208 num.form = (byte) exformint;
2209 else if (mag < (-5))
2210 num.form = (byte) exformint;
2212 num.form = (byte) com.ibm.icu.math.MathContext.PLAIN;
2218 * If 'after' was specified then we may need to adjust the mantissa. This is a little tricky, as we must conform
2219 * to the rules of exponential layout if necessary (e.g., we cannot end up with 10.0 if scientific).
2222 setafter: for (;;) {
2223 // calculate the current after-length
2225 if (num.form == com.ibm.icu.math.MathContext.PLAIN)
2226 thisafter = -num.exp; // has decimal part
2227 else if (num.form == com.ibm.icu.math.MathContext.SCIENTIFIC)
2228 thisafter = num.mant.length - 1;
2229 else { // engineering
2230 lead = (((num.exp + num.mant.length) - 1)) % 3; // exponent to use
2232 lead = 3 + lead; // negative exponent case
2233 lead++; // number of leading digits
2234 if (lead >= num.mant.length)
2237 thisafter = num.mant.length - lead;
2240 if (thisafter == after)
2241 break setafter; // we're in luck
2242 if (thisafter < after) { // need added trailing zeros
2243 // [thisafter can be negative]
2244 newmant = extend(num.mant, (num.mant.length + after) - thisafter);
2246 num.exp = num.exp - ((after - thisafter)); // adjust exponent
2247 if (num.exp < MinExp)
2248 throw new java.lang.ArithmeticException("Exponent Overflow:" + " " + num.exp);
2251 // We have too many digits after the decimal point; this could
2252 // cause a carry, which could change the mantissa...
2253 // Watch out for implied leading zeros in PLAIN case
2254 chop = thisafter - after; // digits to lop [is >0]
2255 if (chop > num.mant.length) { // all digits go, no chance of carry
2256 // carry on with zero
2257 num.mant = ZERO.mant;
2260 continue setafter; // recheck: we may need trailing zeros
2262 // we have a digit to inspect from existing mantissa
2263 // round the number as required
2264 need = num.mant.length - chop; // digits to end up with [may be 0]
2265 oldexp = num.exp; // save old exponent
2266 num.round(need, exround);
2267 // if the exponent grew by more than the digits we chopped, then
2268 // we must have had a carry, so will need to recheck the layout
2269 if ((num.exp - oldexp) == chop)
2270 break setafter; // number did not have carry
2271 // mantissa got extended .. so go around and check again
2275 a = num.layout(); // lay out, with exponent if required, etc.
2277 /* Here we have laid-out number in 'a' */
2278 // now apply 'before' and 'explaces' as needed
2280 // look for '.' or 'E'
2284 p: for (; $11 > 0; $11--, p++) {
2291 // p is now offset of '.', 'E', or character after end of array
2292 // that is, the current length of before part
2294 badarg("format", 1, java.lang.String.valueOf(before)); // won't fit
2295 if (p < before) { // need leading blanks
2296 newa = new char[(a.length + before) - p];
2298 int $12 = before - p;
2300 for (; $12 > 0; $12--, i++) {
2304 java.lang.System.arraycopy((java.lang.Object) a, 0, (java.lang.Object) newa, i, a.length);
2307 // [if p=before then it's just the right length]
2311 // look for 'E' [cannot be at offset 0]
2313 int $13 = a.length - 1;
2315 p: for (; $13 > 0; $13--, p--) {
2320 // p is now offset of 'E', or 0
2321 if (p == 0) { // no E part; add trailing blanks
2322 newa = new char[(a.length + explaces) + 2];
2323 java.lang.System.arraycopy((java.lang.Object) a, 0, (java.lang.Object) newa, 0, a.length);
2325 int $14 = explaces + 2;
2327 for (; $14 > 0; $14--, i++) {
2332 } else {/* found E */// may need to insert zeros
2333 places = (a.length - p) - 2; // number so far
2334 if (places > explaces)
2335 badarg("format", 3, java.lang.String.valueOf(explaces));
2336 if (places < explaces) { // need to insert zeros
2337 newa = new char[(a.length + explaces) - places];
2338 java.lang.System.arraycopy((java.lang.Object) a, 0, (java.lang.Object) newa, 0, p + 2); // through E
2341 int $15 = explaces - places;
2343 for (; $15 > 0; $15--, i++) {
2347 java.lang.System.arraycopy((java.lang.Object) a, p + 2, (java.lang.Object) newa, i, places); // remainder
2352 // [if places=explaces then it's just the right length]
2355 return new java.lang.String(a);
2359 * Returns the hashcode for this <code>BigDecimal</code>. This hashcode is suitable for use by the <code>
2360 * java.util.Hashtable</code> class.
2362 * Note that two <code>BigDecimal</code> objects are only guaranteed to produce the same hashcode if they are
2363 * exactly equal (that is, the <code>String</code> representations of the <code>BigDecimal</code> numbers are
2364 * identical -- they have the same characters in the same sequence).
2366 * @return An <code>int</code> that is the hashcode for <code>this</code>.
2370 public int hashCode() {
2371 // Maybe calculate ourselves, later. If so, note that there can be
2372 // more than one internal representation for a given toString() result.
2373 return this.toString().hashCode();
2377 * Converts this <code>BigDecimal</code> to an <code>int</code>. If the <code>BigDecimal</code> has a non-zero
2378 * decimal part it is discarded. If the <code>BigDecimal</code> is out of the possible range for an <code>int</code>
2379 * (32-bit signed integer) result then only the low-order 32 bits are used. (That is, the number may be
2380 * <i>decapitated</i>.) To avoid unexpected errors when these conditions occur, use the {@link #intValueExact}
2383 * @return An <code>int</code> converted from <code>this</code>, truncated and decapitated if necessary.
2387 public int intValue() {
2388 return toBigInteger().intValue();
2392 * Converts this <code>BigDecimal</code> to an <code>int</code>. If the <code>BigDecimal</code> has a non-zero
2393 * decimal part or is out of the possible range for an <code>int</code> (32-bit signed integer) result then an
2394 * <code>ArithmeticException</code> is thrown.
2396 * @return An <code>int</code> equal in value to <code>this</code>.
2397 * @throws ArithmeticException if <code>this</code> has a non-zero decimal part, or will not fit in an <code>int</code>.
2401 public int intValueExact() {
2407 // This does not use longValueExact() as the latter can be much
2409 // intcheck (from pow) relies on this to check decimal part
2411 return 0; // easy, and quite common
2412 /* test and drop any trailing decimal part */
2413 lodigit = mant.length - 1;
2415 lodigit = lodigit + exp; // reduces by -(-exp)
2416 /* all decimal places must be 0 */
2417 if ((!(allzero(mant, lodigit + 1))))
2418 throw new java.lang.ArithmeticException("Decimal part non-zero:" + " " + this.toString());
2420 return 0; // -1<this<1
2423 if ((exp + lodigit) > 9) // early exit
2424 throw new java.lang.ArithmeticException("Conversion overflow:" + " " + this.toString());
2427 /* convert the mantissa to binary, inline for speed */
2430 int $16 = lodigit + useexp;
2432 for (; i <= $16; i++) {
2433 result = result * 10;
2435 result = result + mant[i];
2439 /* Now, if the risky length, check for overflow */
2440 if ((lodigit + useexp) == 9) {
2441 // note we cannot just test for -ve result, as overflow can move a
2442 // zero into the top bit [consider 5555555555]
2443 topdig = result / 1000000000; // get top digit, preserving sign
2444 if (topdig != mant[0]) { // digit must match and be positive
2445 // except in the special case ...
2446 if (result == java.lang.Integer.MIN_VALUE) // looks like the special
2447 if (ind == isneg) // really was negative
2449 return result; // really had top digit 2
2450 throw new java.lang.ArithmeticException("Conversion overflow:" + " " + this.toString());
2461 * Converts this <code>BigDecimal</code> to a <code>long</code>. If the <code>BigDecimal</code> has a non-zero
2462 * decimal part it is discarded. If the <code>BigDecimal</code> is out of the possible range for a <code>long</code>
2463 * (64-bit signed integer) result then only the low-order 64 bits are used. (That is, the number may be
2464 * <i>decapitated</i>.) To avoid unexpected errors when these conditions occur, use the {@link #longValueExact}
2467 * @return A <code>long</code> converted from <code>this</code>, truncated and decapitated if necessary.
2471 public long longValue() {
2472 return toBigInteger().longValue();
2476 * Converts this <code>BigDecimal</code> to a <code>long</code>. If the <code>BigDecimal</code> has a non-zero
2477 * decimal part or is out of the possible range for a <code>long</code> (64-bit signed integer) result then an
2478 * <code>ArithmeticException</code> is thrown.
2480 * @return A <code>long</code> equal in value to <code>this</code>.
2481 * @throws ArithmeticException if <code>this</code> has a non-zero decimal part, or will not fit in a <code>long</code>.
2485 public long longValueExact() {
2492 // Identical to intValueExact except for result=long, and exp>=20 test
2494 return 0; // easy, and quite common
2495 lodigit = mant.length - 1; // last included digit
2497 lodigit = lodigit + exp; // -(-exp)
2498 /* all decimal places must be 0 */
2502 cstart = lodigit + 1;
2503 if ((!(allzero(mant, cstart))))
2504 throw new java.lang.ArithmeticException("Decimal part non-zero:" + " " + this.toString());
2506 return 0; // -1<this<1
2509 if ((exp + mant.length) > 18) // early exit
2510 throw new java.lang.ArithmeticException("Conversion overflow:" + " " + this.toString());
2514 /* convert the mantissa to binary, inline for speed */
2515 // note that we could safely use the 'test for wrap to negative'
2516 // algorithm here, but instead we parallel the intValueExact
2517 // algorithm for ease of checking and maintenance.
2520 int $17 = lodigit + useexp;
2522 for (; i <= $17; i++) {
2523 result = result * 10;
2525 result = result + mant[i];
2529 /* Now, if the risky length, check for overflow */
2530 if ((lodigit + useexp) == 18) {
2531 topdig = result / 1000000000000000000L; // get top digit, preserving sign
2532 if (topdig != mant[0]) { // digit must match and be positive
2533 // except in the special case ...
2534 if (result == java.lang.Long.MIN_VALUE) // looks like the special
2535 if (ind == isneg) // really was negative
2537 return result; // really had top digit 9
2538 throw new java.lang.ArithmeticException("Conversion overflow:" + " " + this.toString());
2549 * Returns a plain <code>BigDecimal</code> whose decimal point has been moved to the left by a specified number of
2550 * positions. The parameter, <code>n</code>, specifies the number of positions to move the decimal point. That is,
2551 * if <code>n</code> is 0 or positive, the number returned is given by:
2553 * <code> this.multiply(TEN.pow(new BigDecimal(-n))) </code>
2555 * <code>n</code> may be negative, in which case the method returns the same result as <code>movePointRight(-n)
2558 * @param n The <code>int</code> specifying the number of places to move the decimal point leftwards.
2559 * @return A <code>BigDecimal</code> derived from <code>this</code>, with the decimal point moved <code>n</code>
2560 * places to the left.
2564 public com.ibm.icu.math.BigDecimal movePointLeft(int n) {
2565 com.ibm.icu.math.BigDecimal res;
2566 // very little point in optimizing for shift of 0
2568 res.exp = res.exp - n;
2569 return res.finish(plainMC, false); // finish sets form and checks exponent
2573 * Returns a plain <code>BigDecimal</code> whose decimal point has been moved to the right by a specified number of
2574 * positions. The parameter, <code>n</code>, specifies the number of positions to move the decimal point. That is,
2575 * if <code>n</code> is 0 or positive, the number returned is given by:
2577 * <code> this.multiply(TEN.pow(new BigDecimal(n))) </code>
2579 * <code>n</code> may be negative, in which case the method returns the same result as <code>movePointLeft(-n)
2582 * @param n The <code>int</code> specifying the number of places to move the decimal point rightwards.
2583 * @return A <code>BigDecimal</code> derived from <code>this</code>, with the decimal point moved <code>n</code>
2584 * places to the right.
2588 public com.ibm.icu.math.BigDecimal movePointRight(int n) {
2589 com.ibm.icu.math.BigDecimal res;
2591 res.exp = res.exp + n;
2592 return res.finish(plainMC, false);
2596 * Returns the scale of this <code>BigDecimal</code>. Returns a non-negative <code>int</code> which is the scale of
2597 * the number. The scale is the number of digits in the decimal part of the number if the number were formatted
2598 * without exponential notation.
2600 * @return An <code>int</code> whose value is the scale of this <code>BigDecimal</code>.
2604 public int scale() {
2606 return 0; // scale can never be negative
2611 * Returns a plain <code>BigDecimal</code> with a given scale.
2613 * If the given scale (which must be zero or positive) is the same as or greater than the length of the decimal part
2614 * (the scale) of this <code>BigDecimal</code> then trailing zeros will be added to the decimal part as necessary.
2616 * If the given scale is less than the length of the decimal part (the scale) of this <code>BigDecimal</code> then
2617 * trailing digits will be removed, and in this case an <code>ArithmeticException</code> is thrown if any discarded
2618 * digits are non-zero.
2620 * The same as {@link #setScale(int, int)}, where the first parameter is the scale, and the second is <code>
2621 * MathContext.ROUND_UNNECESSARY</code>.
2623 * @param scale The <code>int</code> specifying the scale of the resulting <code>BigDecimal</code>.
2624 * @return A plain <code>BigDecimal</code> with the given scale.
2625 * @throws ArithmeticException if <code>scale</code> is negative.
2626 * @throws ArithmeticException if reducing scale would discard non-zero digits.
2630 public com.ibm.icu.math.BigDecimal setScale(int scale) {
2631 return setScale(scale, ROUND_UNNECESSARY);
2635 * Returns a plain <code>BigDecimal</code> with a given scale.
2637 * If the given scale (which must be zero or positive) is the same as or greater than the length of the decimal part
2638 * (the scale) of this <code>BigDecimal</code> then trailing zeros will be added to the decimal part as necessary.
2640 * If the given scale is less than the length of the decimal part (the scale) of this <code>BigDecimal</code> then
2641 * trailing digits will be removed, and the rounding mode given by the second parameter is used to determine if the
2642 * remaining digits are affected by a carry. In this case, an <code>IllegalArgumentException</code> is thrown if
2643 * <code>round</code> is not a valid rounding mode.
2645 * If <code>round</code> is <code>MathContext.ROUND_UNNECESSARY</code>, an <code>ArithmeticException</code> is
2646 * thrown if any discarded digits are non-zero.
2648 * @param scale The <code>int</code> specifying the scale of the resulting <code>BigDecimal</code>.
2649 * @param round The <code>int</code> rounding mode to be used for the division (see the {@link MathContext} class).
2650 * @return A plain <code>BigDecimal</code> with the given scale.
2651 * @throws IllegalArgumentException if <code>round</code> is not a valid rounding mode.
2652 * @throws ArithmeticException if <code>scale</code> is negative.
2653 * @throws ArithmeticException if <code>round</code> is <code>MathContext.ROUND_UNNECESSARY</code>, and reducing scale would discard
2658 public com.ibm.icu.math.BigDecimal setScale(int scale, int round) {
2660 com.ibm.icu.math.BigDecimal res;
2663 // at present this naughtily only checks the round value if it is
2664 // needed (used), for speed
2665 ourscale = this.scale();
2666 if (ourscale == scale) // already correct scale
2667 if (this.form == com.ibm.icu.math.MathContext.PLAIN) // .. and form
2669 res = clone(this); // need copy
2670 if (ourscale <= scale) { // simply zero-padding/changing form
2671 // if ourscale is 0 we may have lots of 0s to add
2673 padding = res.exp + scale;
2675 padding = scale - ourscale;
2676 res.mant = extend(res.mant, res.mant.length + padding);
2677 res.exp = -scale; // as requested
2678 } else {/* ourscale>scale: shortening, probably */
2680 throw new java.lang.ArithmeticException("Negative scale:" + " " + scale);
2681 // [round() will raise exception if invalid round]
2682 newlen = res.mant.length - ((ourscale - scale)); // [<=0 is OK]
2683 res = res.round(newlen, round); // round to required length
2684 // This could have shifted left if round (say) 0.9->1[.0]
2685 // Repair if so by adding a zero and reducing exponent
2686 if (res.exp != -scale) {
2687 res.mant = extend(res.mant, res.mant.length + 1);
2688 res.exp = res.exp - 1;
2691 res.form = (byte) com.ibm.icu.math.MathContext.PLAIN; // by definition
2696 * Converts this <code>BigDecimal</code> to a <code>short</code>. If the <code>BigDecimal</code> has a non-zero
2697 * decimal part or is out of the possible range for a <code>short</code> (16-bit signed integer) result then an
2698 * <code>ArithmeticException</code> is thrown.
2700 * @return A <code>short</code> equal in value to <code>this</code>.
2701 * @throws ArithmeticException if <code>this</code> has a non-zero decimal part, or will not fit in a <code>short</code>.
2705 public short shortValueExact() {
2707 num = this.intValueExact(); // will check decimal part too
2708 if ((num > 32767) | (num < (-32768)))
2709 throw new java.lang.ArithmeticException("Conversion overflow:" + " " + this.toString());
2714 * Returns the sign of this <code>BigDecimal</code>, as an <code>int</code>. This returns the <i>signum</i> function
2715 * value that represents the sign of this <code>BigDecimal</code>. That is, -1 if the <code>BigDecimal</code> is
2716 * negative, 0 if it is numerically equal to zero, or 1 if it is positive.
2718 * @return An <code>int</code> which is -1 if the <code>BigDecimal</code> is negative, 0 if it is numerically equal
2719 * to zero, or 1 if it is positive.
2723 public int signum() {
2724 return (int) this.ind; // [note this assumes values for ind.]
2728 * Converts this <code>BigDecimal</code> to a <code>java.math.BigDecimal</code>.
2730 * This is an exact conversion; the result is the same as if the <code>BigDecimal</code> were formatted as a plain
2731 * number without any rounding or exponent and then the <code>java.math.BigDecimal(java.lang.String)</code>
2732 * constructor were used to construct the result.
2734 * <i>(Note: this method is provided only in the <code>com.ibm.icu.math</code> version of the BigDecimal class. It
2735 * would not be present in a <code>java.math</code> version.)</i>
2737 * @return The <code>java.math.BigDecimal</code> equal in value to this <code>BigDecimal</code>.
2741 public java.math.BigDecimal toBigDecimal() {
2742 return new java.math.BigDecimal(this.unscaledValue(), this.scale());
2746 * Converts this <code>BigDecimal</code> to a <code>java.math.BigInteger</code>.
2748 * Any decimal part is truncated (discarded). If an exception is desired should the decimal part be non-zero, use
2749 * {@link #toBigIntegerExact()}.
2751 * @return The <code>java.math.BigInteger</code> equal in value to the integer part of this <code>BigDecimal</code>.
2755 public java.math.BigInteger toBigInteger() {
2756 com.ibm.icu.math.BigDecimal res = null;
2758 byte newmant[] = null;
2760 if ((exp >= 0) & (form == com.ibm.icu.math.MathContext.PLAIN))
2761 res = this; // can layout simply
2762 else if (exp >= 0) {
2763 res = clone(this); // safe copy
2764 res.form = (byte) com.ibm.icu.math.MathContext.PLAIN; // .. and request PLAIN
2766 { // exp<0; scale to be truncated
2767 // we could use divideInteger, but we may as well be quicker
2768 if (-this.exp >= this.mant.length)
2769 res = ZERO; // all blows away
2771 res = clone(this); // safe copy
2772 newlen = res.mant.length + res.exp;
2773 newmant = new byte[newlen]; // [shorter]
2774 java.lang.System.arraycopy((java.lang.Object) res.mant, 0, (java.lang.Object) newmant, 0,
2777 res.form = (byte) com.ibm.icu.math.MathContext.PLAIN;
2783 return new BigInteger(new java.lang.String(res.layout()));
2787 * Converts this <code>BigDecimal</code> to a <code>java.math.BigInteger</code>.
2789 * An exception is thrown if the decimal part (if any) is non-zero.
2791 * @return The <code>java.math.BigInteger</code> equal in value to the integer part of this <code>BigDecimal</code>.
2792 * @throws ArithmeticException if <code>this</code> has a non-zero decimal part.
2796 public java.math.BigInteger toBigIntegerExact() {
2797 /* test any trailing decimal part */
2798 if (exp < 0) { // possible decimal part
2799 /* all decimal places must be 0; note exp<0 */
2800 if ((!(allzero(mant, mant.length + exp))))
2801 throw new java.lang.ArithmeticException("Decimal part non-zero:" + " " + this.toString());
2803 return toBigInteger();
2807 * Returns the <code>BigDecimal</code> as a character array. The result of this method is the same as using the
2808 * sequence <code>toString().toCharArray()</code>, but avoids creating the intermediate <code>String</code> and
2809 * <code>char[]</code> objects.
2811 * @return The <code>char[]</code> array corresponding to this <code>BigDecimal</code>.
2815 public char[] toCharArray() {
2820 * Returns the <code>BigDecimal</code> as a <code>String</code>. This returns a <code>String</code> that exactly
2821 * represents this <code>BigDecimal</code>, as defined in the decimal documentation (see {@link BigDecimal class
2824 * By definition, using the {@link #BigDecimal(String)} constructor on the result <code>String</code> will create a
2825 * <code>BigDecimal</code> that is exactly equal to the original <code>BigDecimal</code>.
2827 * @return The <code>String</code> exactly corresponding to this <code>BigDecimal</code>.
2828 * @see #format(int, int)
2829 * @see #format(int, int, int, int, int, int)
2830 * @see #toCharArray()
2834 public java.lang.String toString() {
2835 return new java.lang.String(layout());
2839 * Returns the number as a <code>BigInteger</code> after removing the scale. That is, the number is expressed as a
2840 * plain number, any decimal point is then removed (retaining the digits of any decimal part), and the result is
2841 * then converted to a <code>BigInteger</code>.
2843 * @return The <code>java.math.BigInteger</code> equal in value to this <code>BigDecimal</code> multiplied by ten to
2844 * the power of <code>this.scale()</code>.
2848 public java.math.BigInteger unscaledValue() {
2849 com.ibm.icu.math.BigDecimal res = null;
2853 res = clone(this); // safe copy
2854 res.exp = 0; // drop scale
2856 return res.toBigInteger();
2860 * Translates a <code>double</code> to a <code>BigDecimal</code>.
2862 * Returns a <code>BigDecimal</code> which is the decimal representation of the 64-bit signed binary floating point
2863 * parameter. If the parameter is infinite, or is not a number (NaN), a <code>NumberFormatException</code> is
2866 * The number is constructed as though <code>num</code> had been converted to a <code>String</code> using the <code>
2867 * Double.toString()</code> method and the {@link #BigDecimal(java.lang.String)} constructor had then been used.
2868 * This is typically not an exact conversion.
2870 * @param dub The <code>double</code> to be translated.
2871 * @return The <code>BigDecimal</code> equal in value to <code>dub</code>.
2872 * @throws NumberFormatException if the parameter is infinite or not a number.
2876 public static com.ibm.icu.math.BigDecimal valueOf(double dub) {
2877 // Reminder: a zero double returns '0.0', so we cannot fastpath to
2878 // use the constant ZERO. This might be important enough to justify
2879 // a factory approach, a cache, or a few private constants, later.
2880 return new com.ibm.icu.math.BigDecimal((new java.lang.Double(dub)).toString());
2884 * Translates a <code>long</code> to a <code>BigDecimal</code>. That is, returns a plain <code>BigDecimal</code>
2885 * whose value is equal to the given <code>long</code>.
2887 * @param lint The <code>long</code> to be translated.
2888 * @return The <code>BigDecimal</code> equal in value to <code>lint</code>.
2892 public static com.ibm.icu.math.BigDecimal valueOf(long lint) {
2893 return valueOf(lint, 0);
2897 * Translates a <code>long</code> to a <code>BigDecimal</code> with a given scale. That is, returns a plain <code>
2898 * BigDecimal</code> whose unscaled value is equal to the given <code>long</code>, adjusted by the second parameter,
2899 * <code>scale</code>.
2901 * The result is given by:
2903 * <code> (new BigDecimal(lint)).divide(TEN.pow(new BigDecimal(scale))) </code>
2905 * A <code>NumberFormatException</code> is thrown if <code>scale</code> is negative.
2907 * @param lint The <code>long</code> to be translated.
2908 * @param scale The <code>int</code> scale to be applied.
2909 * @return The <code>BigDecimal</code> equal in value to <code>lint</code>.
2910 * @throws NumberFormatException if the scale is negative.
2914 public static com.ibm.icu.math.BigDecimal valueOf(long lint, int scale) {
2915 com.ibm.icu.math.BigDecimal res = null;
2921 else if (lint == 10)
2924 res = new com.ibm.icu.math.BigDecimal(lint);
2930 throw new java.lang.NumberFormatException("Negative scale:" + " " + scale);
2931 res = clone(res); // safe copy [do not mutate]
2932 res.exp = -scale; // exponent is -scale
2936 /* ---------------------------------------------------------------- */
2937 /* Private methods */
2938 /* ---------------------------------------------------------------- */
2941 * <sgml> Return char array value of a BigDecimal (conversion from BigDecimal to laid-out canonical char array).
2942 * <p>The mantissa will either already have been rounded (following an operation) or will be of length appropriate
2943 * (in the case of construction from an int, for example). <p>We must not alter the mantissa, here. <p>'form'
2944 * describes whether we are to use exponential notation (and if so, which), or if we are to lay out as a plain/pure
2948 private char[] layout() {
2951 StringBuilder sb = null;
2959 cmant = new char[mant.length]; // copy byte[] to a char[]
2961 int $18 = mant.length;
2963 for (; $18 > 0; $18--, i++) {
2964 cmant[i] = (char) (mant[i] + ((int) ('0')));
2968 if (form != com.ibm.icu.math.MathContext.PLAIN) {/* exponential notation needed */
2969 sb = new StringBuilder(cmant.length + 15); // -x.xxxE+999999999
2972 euse = (exp + cmant.length) - 1; // exponent to use
2973 /* setup sig=significant digits and copy to result */
2974 if (form == com.ibm.icu.math.MathContext.SCIENTIFIC) { // [default]
2975 sb.append(cmant[0]); // significant character
2976 if (cmant.length > 1) // have decimal part
2977 sb.append('.').append(cmant, 1, cmant.length - 1);
2980 sig = euse % 3; // common
2982 sig = 3 + sig; // negative exponent
2985 if (sig >= cmant.length) { // zero padding may be needed
2986 sb.append(cmant, 0, cmant.length);
2988 int $19 = sig - cmant.length;
2989 for (; $19 > 0; $19--) {
2993 } else { // decimal point needed
2994 sb.append(cmant, 0, sig).append('.').append(cmant, sig, cmant.length - sig);
3004 sb.append('E').append(csign).append(euse);
3006 rec = new char[sb.length()];
3007 int srcEnd = sb.length();
3009 sb.getChars(0, srcEnd, rec, 0);
3014 /* Here for non-exponential (plain) notation */
3015 if (exp == 0) {/* easy */
3017 return cmant; // non-negative integer
3018 rec = new char[cmant.length + 1];
3020 java.lang.System.arraycopy((java.lang.Object) cmant, 0, (java.lang.Object) rec, 1, cmant.length);
3024 /* Need a '.' and/or some zeros */
3025 needsign = (ind == isneg) ? 1 : 0; // space for sign? 0 or 1
3028 * MAG is the position of the point in the mantissa (index of the character it follows)
3030 mag = exp + cmant.length;
3032 if (mag < 1) {/* 0.00xxxx form */
3033 len = (needsign + 2) - exp; // needsign+2+(-mag)+cmant.length
3034 rec = new char[len];
3037 rec[needsign] = '0';
3038 rec[needsign + 1] = '.';
3042 for (; $20 > 0; $20--, i++) { // maybe none
3046 java.lang.System.arraycopy((java.lang.Object) cmant, 0, (java.lang.Object) rec, (needsign + 2) - mag,
3051 if (mag > cmant.length) {/* xxxx0000 form */
3052 len = needsign + mag;
3053 rec = new char[len];
3056 java.lang.System.arraycopy((java.lang.Object) cmant, 0, (java.lang.Object) rec, needsign, cmant.length);
3058 int $21 = mag - cmant.length;
3059 i = needsign + cmant.length;
3060 for (; $21 > 0; $21--, i++) { // never 0
3067 /* decimal point is in the middle of the mantissa */
3068 len = (needsign + 1) + cmant.length;
3069 rec = new char[len];
3072 java.lang.System.arraycopy((java.lang.Object) cmant, 0, (java.lang.Object) rec, needsign, mag);
3073 rec[needsign + mag] = '.';
3074 java.lang.System.arraycopy((java.lang.Object) cmant, mag, (java.lang.Object) rec, (needsign + mag) + 1,
3075 cmant.length - mag);
3080 * <sgml> Checks a BigDecimal argument to ensure it's a true integer in a given range. <p>If OK, returns it as an
3083 // [currently only used by pow]
3084 private int intcheck(int min, int max) {
3086 i = this.intValueExact(); // [checks for non-0 decimal part]
3087 // Use same message as though intValueExact failed due to size
3088 if ((i < min) | (i > max))
3089 throw new java.lang.ArithmeticException("Conversion overflow:" + " " + i);
3093 /* <sgml> Carry out division operations. </sgml> */
3095 * Arg1 is operation code: D=divide, I=integer divide, R=remainder Arg2 is the rhs. Arg3 is the context. Arg4 is
3096 * explicit scale iff code='D' or 'I' (-1 if none).
3098 * Underlying algorithm (complications for Remainder function and scaled division are omitted for clarity):
3100 * Test for x/0 and then 0/x Exp =Exp1 - Exp2 Exp =Exp +len(var1) -len(var2) Sign=Sign1 Sign2 Pad accumulator (Var1)
3101 * to double-length with 0's (pad1) Pad Var2 to same length as Var1 B2B=1st two digits of var2, +1 to allow for
3102 * roundup have=0 Do until (have=digits+1 OR residue=0) if exp<0 then if integer divide/residue then leave
3103 * this_digit=0 Do forever compare numbers if <0 then leave inner_loop if =0 then (- quick exit without subtract -)
3104 * do this_digit=this_digit+1; output this_digit leave outer_loop; end Compare lengths of numbers (mantissae): If
3105 * same then CA=first_digit_of_Var1 else CA=first_two_digits_of_Var1 mult=ca10/b2b -- Good and safe guess at divisor
3106 * if mult=0 then mult=1 this_digit=this_digit+mult subtract end inner_loop if have\=0 | this_digit\=0 then do
3107 * output this_digit have=have+1; end var2=var2/10 exp=exp-1 end outer_loop exp=exp+1 -- set the proper exponent if
3108 * have=0 then generate answer=0 Return to FINISHED Result defined by MATHV1
3110 * For extended commentary, see DMSRCN.
3113 private com.ibm.icu.math.BigDecimal dodivide(char code, com.ibm.icu.math.BigDecimal rhs,
3114 com.ibm.icu.math.MathContext set, int scale) {
3115 com.ibm.icu.math.BigDecimal lhs;
3118 com.ibm.icu.math.BigDecimal res;
3134 byte newvar1[] = null;
3137 byte newmant[] = null;
3140 checkdigits(rhs, set.digits);
3141 lhs = this; // name for clarity
3143 // [note we must have checked lostDigits before the following checks]
3145 throw new java.lang.ArithmeticException("Divide by 0"); // includes 0/0
3146 if (lhs.ind == 0) { // 0/x => 0 [possibly with .0s]
3147 if (set.form != com.ibm.icu.math.MathContext.PLAIN)
3151 return lhs.setScale(scale);
3154 /* Prepare numbers according to BigDecimal rules */
3155 reqdig = set.digits; // local copy (heavily used)
3157 if (lhs.mant.length > reqdig)
3158 lhs = clone(lhs).round(set);
3159 if (rhs.mant.length > reqdig)
3160 rhs = clone(rhs).round(set);
3161 } else {/* scaled divide */
3163 scale = lhs.scale();
3164 // set reqdig to be at least large enough for the computation
3165 reqdig = lhs.mant.length; // base length
3166 // next line handles both positive lhs.exp and also scale mismatch
3167 if (scale != -lhs.exp)
3168 reqdig = (reqdig + scale) + lhs.exp;
3169 reqdig = (reqdig - ((rhs.mant.length - 1))) - rhs.exp; // reduce by RHS effect
3170 if (reqdig < lhs.mant.length)
3171 reqdig = lhs.mant.length; // clamp
3172 if (reqdig < rhs.mant.length)
3173 reqdig = rhs.mant.length; // ..
3176 /* precalculate exponent */
3177 newexp = ((lhs.exp - rhs.exp) + lhs.mant.length) - rhs.mant.length;
3178 /* If new exponent -ve, then some quick exits are possible */
3182 return ZERO; // easy - no integer part
3183 /* Must be 'R'; remainder is [finished clone of] input value */
3184 return clone(lhs).finish(set, false);
3187 /* We need slow division */
3188 res = new com.ibm.icu.math.BigDecimal(); // where we'll build result
3189 res.ind = (byte) (lhs.ind * rhs.ind); // final sign (for D/I)
3190 res.exp = newexp; // initial exponent (for D/I)
3191 res.mant = new byte[reqdig + 1]; // where build the result
3193 /* Now [virtually pad the mantissae with trailing zeros */
3194 // Also copy the LHS, which will be our working array
3195 newlen = (reqdig + reqdig) + 1;
3196 var1 = extend(lhs.mant, newlen); // always makes longer, so new safe array
3197 var1len = newlen; // [remaining digits are 0]
3202 /* Calculate first two digits of rhs (var2), +1 for later estimations */
3203 b2b = (var2[0] * 10) + 1;
3204 if (var2.length > 1)
3205 b2b = b2b + var2[1];
3207 /* start the long-division loops */
3212 /* find the next digit */
3215 if (var1len < var2len)
3216 break inner; // V1 too low
3217 if (var1len == var2len) { // compare needed
3219 compare: do { // comparison
3223 for (; $22 > 0; $22--, i++) {
3224 // var1len is always <= var1.length
3225 if (i < var2.length)
3230 break inner; // V1 too low
3232 break compare; // OK to subtract
3236 * reach here if lhs and rhs are identical; subtraction will increase digit by one,
3237 * and the residue will be 0 so we are done; leave the loop with residue set to 0
3238 * (in case code is 'R' or ROUND_UNNECESSARY or a ROUND_HALF_xxxx is being checked)
3241 res.mant[have] = (byte) thisdigit;
3243 var1[0] = (byte) 0; // residue to 0 [this is all we'll test]
3244 // var1len=1 -- [optimized out]
3248 /* prepare for subtraction. Estimate BA (lengths the same) */
3249 ba = (int) var1[0]; // use only first digit
3250 } // lengths the same
3251 else {/* lhs longer than rhs */
3252 /* use first two digits for estimate */
3257 /* subtraction needed; V1>=V2 */
3258 mult = (ba * 10) / b2b;
3261 thisdigit = thisdigit + mult;
3262 // subtract; var1 reusable
3263 var1 = byteaddsub(var1, var1len, var2, var2len, -mult, true);
3265 continue inner; // maybe another subtract needed
3267 * V1 now probably has leading zeros, remove leading 0's and try again. (It could be longer than
3271 int $23 = var1len - 2;
3273 start: for (; start <= $23; start++) {
3274 if (var1[start] != 0)
3282 java.lang.System.arraycopy((java.lang.Object) var1, start, (java.lang.Object) var1, 0, var1len);
3286 /* We have the next digit */
3287 if ((have != 0) | (thisdigit != 0)) { // put the digit we got
3288 res.mant[have] = (byte) thisdigit;
3290 if (have == (reqdig + 1))
3291 break outer; // we have all we need
3293 break outer; // residue now 0
3295 /* can leave now if a scaled divide and exponent is small enough */
3297 if (-res.exp > scale)
3299 /* can leave now if not Divide and no integer part left */
3303 res.exp = res.exp - 1; // reduce the exponent
3305 * to get here, V1 is less than V2, so divide V2 by 10 and go for the next digit
3311 /* here when we have finished dividing, for some reason */
3312 // have is the number of digits we collected in res.mant
3314 have = 1; // res.mant[0] is 0; we always want a digit
3316 if ((code == 'I') | (code == 'R')) {/* check for integer overflow needed */
3317 if ((have + res.exp) > reqdig)
3318 throw new java.lang.ArithmeticException("Integer overflow");
3322 /* We were doing Remainder -- return the residue */
3323 if (res.mant[0] == 0) // no integer part was found
3324 return clone(lhs).finish(set, false); // .. so return lhs, canonical
3326 return ZERO; // simple 0 residue
3327 res.ind = lhs.ind; // sign is always as LHS
3329 * Calculate the exponent by subtracting the number of padding zeros we added and adding the
3332 padding = ((reqdig + reqdig) + 1) - lhs.mant.length;
3333 res.exp = (res.exp - padding) + lhs.exp;
3336 * strip insignificant padding zeros from residue, and create/copy the resulting mantissa if need be
3341 i: for (; i >= 1; i--) {
3342 if (!((res.exp < lhs.exp) & (res.exp < rhs.exp)))
3347 res.exp = res.exp + 1;
3350 if (d < var1.length) {/* need to reduce */
3351 newvar1 = new byte[d];
3352 java.lang.System.arraycopy((java.lang.Object) var1, 0, (java.lang.Object) newvar1, 0, d); // shorten
3356 return res.finish(set, false);
3361 else {/* 'D' -- no overflow check needed */
3362 // If there was a residue then bump the final digit (iff 0 or 5)
3363 // so that the residue is visible for ROUND_UP, ROUND_HALF_xxx and
3364 // ROUND_UNNECESSARY checks (etc.) later.
3365 // [if we finished early, the residue will be 0]
3366 if (var1[0] != 0) { // residue not 0
3367 lasthave = res.mant[have - 1];
3368 if (((lasthave % 5)) == 0)
3369 res.mant[have - 1] = (byte) (lasthave + 1);
3373 /* Here for Divide or Integer Divide */
3374 // handle scaled results first ['I' always scale 0, optional for 'D']
3377 // say 'scale have res.exp len' scale have res.exp res.mant.length
3378 if (have != res.mant.length)
3379 // already padded with 0's, so just adjust exponent
3380 res.exp = res.exp - ((res.mant.length - have));
3381 // calculate number of digits we really want [may be 0]
3382 actdig = res.mant.length - (-res.exp - scale);
3383 res.round(actdig, set.roundingMode); // round to desired length
3384 // This could have shifted left if round (say) 0.9->1[.0]
3385 // Repair if so by adding a zero and reducing exponent
3386 if (res.exp != -scale) {
3387 res.mant = extend(res.mant, res.mant.length + 1);
3388 res.exp = res.exp - 1;
3390 return res.finish(set, true); // [strip if not PLAIN]
3394 // reach here only if a non-scaled
3395 if (have == res.mant.length) { // got digits+1 digits
3398 } else {/* have<=reqdig */
3399 if (res.mant[0] == 0)
3400 return ZERO; // fastpath
3401 // make the mantissa truly just 'have' long
3402 // [we could let finish do this, during strip, if we adjusted
3403 // the exponent; however, truncation avoids the strip loop]
3404 newmant = new byte[have]; // shorten
3405 java.lang.System.arraycopy((java.lang.Object) res.mant, 0, (java.lang.Object) newmant, 0, have);
3408 return res.finish(set, true);
3411 /* <sgml> Report a conversion exception. </sgml> */
3413 private void bad(char s[]) {
3414 throw new java.lang.NumberFormatException("Not a number:" + " " + java.lang.String.valueOf(s));
3418 * <sgml> Report a bad argument to a method. </sgml> Arg1 is method name Arg2 is argument position Arg3 is what was
3422 private void badarg(java.lang.String name, int pos, java.lang.String value) {
3423 throw new java.lang.IllegalArgumentException("Bad argument" + " " + pos + " " + "to" + " " + name + ":" + " "
3428 * <sgml> Extend byte array to given length, padding with 0s. If no extension is required then return the same
3431 * Arg1 is the source byte array Arg2 is the new length (longer)
3434 private static final byte[] extend(byte inarr[], int newlen) {
3436 if (inarr.length == newlen)
3438 newarr = new byte[newlen];
3439 java.lang.System.arraycopy((java.lang.Object) inarr, 0, (java.lang.Object) newarr, 0, inarr.length);
3440 // 0 padding is carried out by the JVM on allocation initialization
3445 * <sgml> Add or subtract two >=0 integers in byte arrays <p>This routine performs the calculation: <pre> C=A+(BM)
3446 * </pre> Where M is in the range -9 through +9 <p> If M<0 then A>=B must be true, so the result is always
3449 * Leading zeros are not removed after a subtraction. The result is either the same length as the longer of A and B,
3450 * or 1 longer than that (if a carry occurred).
3452 * A is not altered unless Arg6 is 1. B is never altered.
3454 * Arg1 is A Arg2 is A length to use (if longer than A, pad with 0's) Arg3 is B Arg4 is B length to use (if longer
3455 * than B, pad with 0's) Arg5 is M, the multiplier Arg6 is 1 if A can be used to build the result (if it fits)
3457 * This routine is severely performance-critical;any change here must be measured (timed) to assure no performance
3460 // 1996.02.20 -- enhanced version of DMSRCN algorithm (1981)
3461 // 1997.10.05 -- changed to byte arrays (from char arrays)
3462 // 1998.07.01 -- changed to allow destructive reuse of LHS
3463 // 1998.07.01 -- changed to allow virtual lengths for the arrays
3464 // 1998.12.29 -- use lookaside for digit/carry calculation
3465 // 1999.08.07 -- avoid multiply when mult=1, and make db an int
3466 // 1999.12.22 -- special case m=-1, also drop 0 special case
3467 private static final byte[] byteaddsub(byte a[], int avlen, byte b[], int bvlen, int m, boolean reuse) {
3481 // We'll usually be right if we assume no carry
3482 alength = a.length; // physical lengths
3483 blength = b.length; // ..
3484 ap = avlen - 1; // -> final (rightmost) digit
3485 bp = bvlen - 1; // ..
3489 reb = (byte[]) null; // result byte array
3491 if ((maxarr + 1) == alength)
3492 reb = a; // OK to reuse A
3494 reb = new byte[maxarr + 1]; // need new array
3496 quickm = false; // 1 if no multiply needed
3498 quickm = true; // most common
3500 quickm = true; // also common
3502 digit = 0; // digit, with carry or borrow
3505 op: for (; op >= 0; op--) {
3508 digit = digit + a[ap]; // within A
3512 if (bp < blength) { // within B
3515 digit = digit + b[bp]; // most common
3517 digit = digit - b[bp]; // also common
3519 digit = digit + (b[bp] * m);
3523 /* result so far (digit) could be -90 through 99 */
3527 reb[op] = (byte) digit;
3528 digit = 0; // no carry
3533 reb[op] = bytedig[dp90]; // this digit
3534 digit = bytecar[dp90]; // carry or borrow
3539 return reb; // no carry
3540 // following line will become an Assert, later
3541 // if digit<0 then signal ArithmeticException("internal.error ["digit"]")
3543 /* We have carry -- need to make space for the extra digit */
3544 newarr = (byte[]) null;
3546 if ((maxarr + 2) == a.length)
3547 newarr = a; // OK to reuse A
3549 newarr = new byte[maxarr + 2];
3550 newarr[0] = (byte) digit; // the carried digit ..
3551 // .. and all the rest [use local loop for short numbers]
3553 int $24 = maxarr + 1;
3555 for (; $24 > 0; $24--, i++) {
3556 newarr[i + 1] = reb[i];
3560 java.lang.System.arraycopy((java.lang.Object) reb, 0, (java.lang.Object) newarr, 1, maxarr + 1);
3565 * <sgml> Initializer for digit array properties (lookaside). </sgml> Returns the digit array, and initializes the
3569 private static final byte[] diginit() {
3573 work = new byte[(90 + 99) + 1];
3576 op: for (; op <= (90 + 99); op++) {
3579 work[op] = (byte) (digit % 10);
3580 bytecar[op] = (byte) (digit / 10); // calculate carry
3584 digit = digit + 100; // yes, this is right [consider -50]
3585 work[op] = (byte) (digit % 10);
3586 bytecar[op] = (byte) ((digit / 10) - 10); // calculate borrow [NB: - after %]
3593 * <sgml> Create a copy of BigDecimal object for local use. <p>This does NOT make a copy of the mantissa array.
3594 * </sgml> Arg1 is the BigDecimal to clone (non-null)
3597 private static final com.ibm.icu.math.BigDecimal clone(com.ibm.icu.math.BigDecimal dec) {
3598 com.ibm.icu.math.BigDecimal copy;
3599 copy = new com.ibm.icu.math.BigDecimal();
3602 copy.form = dec.form;
3603 copy.mant = dec.mant;
3608 * <sgml> Check one or two numbers for lost digits. </sgml> Arg1 is RHS (or null, if none) Arg2 is current DIGITS
3609 * setting returns quietly or throws an exception
3612 private void checkdigits(com.ibm.icu.math.BigDecimal rhs, int dig) {
3614 return; // don't check if digits=0
3615 // first check lhs...
3616 if (this.mant.length > dig)
3617 if ((!(allzero(this.mant, dig))))
3618 throw new java.lang.ArithmeticException("Too many digits:" + " " + this.toString());
3621 if (rhs.mant.length > dig)
3622 if ((!(allzero(rhs.mant, dig))))
3623 throw new java.lang.ArithmeticException("Too many digits:" + " " + rhs.toString());
3627 * <sgml> Round to specified digits, if necessary. </sgml> Arg1 is requested MathContext [with length and rounding
3628 * mode] returns this, for convenience
3631 private com.ibm.icu.math.BigDecimal round(com.ibm.icu.math.MathContext set) {
3632 return round(set.digits, set.roundingMode);
3636 * <sgml> Round to specified digits, if necessary. Arg1 is requested length (digits to round to) [may be <=0 when
3637 * called from format, dodivide, etc.] Arg2 is rounding mode returns this, for convenience
3639 * ind and exp are adjusted, but not cleared for a mantissa of zero
3641 * The length of the mantissa returned will be Arg1, except when Arg1 is 0, in which case the returned mantissa
3642 * length will be 1. </sgml>
3645 private com.ibm.icu.math.BigDecimal round(int len, int mode) {
3649 boolean reuse = false;
3652 byte newmant[] = null;
3653 adjust = mant.length - len;
3655 return this; // nowt to do
3657 exp = exp + adjust; // exponent of result
3658 sign = (int) ind; // save [assumes -1, 0, 1]
3659 oldmant = mant; // save
3661 // remove the unwanted digits
3662 mant = new byte[len];
3663 java.lang.System.arraycopy((java.lang.Object) oldmant, 0, (java.lang.Object) mant, 0, len);
3664 reuse = true; // can reuse mantissa
3665 first = oldmant[len]; // first of discarded digits
3666 } else {/* len<=0 */
3669 reuse = false; // cannot reuse mantissa
3673 first = (byte) 0; // [virtual digit]
3676 // decide rounding adjustment depending on mode, sign, and discarded digits
3677 increment = 0; // bumper
3680 if (mode == ROUND_HALF_UP) { // default first [most common]
3683 } else if (mode == ROUND_UNNECESSARY) { // default for setScale()
3684 // discarding any non-zero digits is an error
3685 if ((!(allzero(oldmant, len))))
3686 throw new java.lang.ArithmeticException("Rounding necessary");
3687 } else if (mode == ROUND_HALF_DOWN) { // 0.5000 goes down
3690 else if (first == 5)
3691 if ((!(allzero(oldmant, len + 1))))
3693 } else if (mode == ROUND_HALF_EVEN) { // 0.5000 goes down if left digit even
3696 else if (first == 5) {
3697 if ((!(allzero(oldmant, len + 1))))
3700 if ((((mant[mant.length - 1]) % 2)) != 0)
3703 } else if (mode == ROUND_DOWN) {
3705 } else if (mode == ROUND_UP) { // increment if discarded non-zero
3706 if ((!(allzero(oldmant, len))))
3708 } else if (mode == ROUND_CEILING) { // more positive
3710 if ((!(allzero(oldmant, len))))
3712 } else if (mode == ROUND_FLOOR) { // more negative
3714 if ((!(allzero(oldmant, len))))
3717 throw new java.lang.IllegalArgumentException("Bad round value:" + " " + mode);
3722 if (increment != 0) {
3724 if (ind == iszero) {
3725 // we must not subtract from 0, but result is trivial anyway
3727 ind = (byte) increment;
3729 // mantissa is non-0; we can safely add or subtract 1
3731 increment = -increment;
3732 newmant = byteaddsub(mant, mant.length, ONE.mant, 1, increment, reuse);
3733 if (newmant.length > mant.length) { // had a carry
3734 // drop rightmost digit and raise exponent
3736 // mant is already the correct length
3737 java.lang.System.arraycopy((java.lang.Object) newmant, 0, (java.lang.Object) mant, 0,
3744 // rounding can increase exponent significantly
3746 throw new java.lang.ArithmeticException("Exponent Overflow:" + " " + exp);
3751 * <sgml> Test if rightmost digits are all 0. Arg1 is a mantissa array to test Arg2 is the offset of first digit to
3752 * check [may be negative; if so, digits to left are 0's] returns 1 if all the digits starting at Arg2 are 0
3754 * Arg2 may be beyond array bounds, in which case 1 is returned </sgml>
3757 private static final boolean allzero(byte array[], int start) {
3762 int $25 = array.length - 1;
3764 for (; i <= $25; i++) {
3773 * <sgml> Carry out final checks and canonicalization <p> This finishes off the current number by: 1. Rounding if
3774 * necessary (NB: length includes leading zeros) 2. Stripping trailing zeros (if requested and \PLAIN) 3. Stripping
3775 * leading zeros (always) 4. Selecting exponential notation (if required) 5. Converting a zero result to just '0'
3776 * (if \PLAIN) In practice, these operations overlap and share code. It always sets form. </sgml> Arg1 is requested
3777 * MathContext (length to round to, trigger, and FORM) Arg2 is 1 if trailing insignificant zeros should be removed
3778 * after round (for division, etc.), provided that set.form isn't PLAIN. returns this, for convenience
3781 private com.ibm.icu.math.BigDecimal finish(com.ibm.icu.math.MathContext set, boolean strip) {
3784 byte newmant[] = null;
3787 /* Round if mantissa too long and digits requested */
3788 if (set.digits != 0)
3789 if (this.mant.length > set.digits)
3793 * If strip requested (and standard formatting), remove insignificant trailing zeros.
3796 if (set.form != com.ibm.icu.math.MathContext.PLAIN) {
3797 d = this.mant.length;
3798 /* see if we need to drop any trailing zeros */
3801 i: for (; i >= 1; i--) {
3802 if (this.mant[i] != 0)
3808 if (d < this.mant.length) {/* need to reduce */
3809 newmant = new byte[d];
3810 java.lang.System.arraycopy((java.lang.Object) this.mant, 0, (java.lang.Object) newmant, 0, d);
3811 this.mant = newmant;
3815 form = (byte) com.ibm.icu.math.MathContext.PLAIN; // preset
3817 /* Now check for leading- and all- zeros in mantissa */
3819 int $26 = this.mant.length;
3821 for (; $26 > 0; $26--, i++) {
3822 if (this.mant[i] != 0) {
3823 // non-0 result; ind will be correct
3824 // remove leading zeros [e.g., after subtract]
3827 newmant = new byte[this.mant.length - i];
3828 java.lang.System.arraycopy((java.lang.Object) this.mant, i, (java.lang.Object) newmant, 0,
3829 this.mant.length - i);
3830 this.mant = newmant;
3833 // now determine form if not PLAIN
3834 mag = exp + mant.length;
3835 if (mag > 0) { // most common path
3836 if (mag > set.digits)
3837 if (set.digits != 0)
3838 form = (byte) set.form;
3839 if ((mag - 1) <= MaxExp)
3840 return this; // no overflow; quick return
3841 } else if (mag < (-5))
3842 form = (byte) set.form;
3843 /* check for overflow */
3845 if ((mag < MinExp) | (mag > MaxExp)) {
3847 // possible reprieve if form is engineering
3848 if (form == com.ibm.icu.math.MathContext.ENGINEERING) {
3849 sig = mag % 3; // leftover
3851 sig = 3 + sig; // negative exponent
3852 mag = mag - sig; // exponent to use
3853 // 1999.06.29: second test here must be MaxExp
3858 throw new java.lang.ArithmeticException("Exponent Overflow:" + " " + mag);
3866 // Drop through to here only if mantissa is all zeros
3869 if (set.form != com.ibm.icu.math.MathContext.PLAIN)
3870 exp = 0; // standard result; go to '0'
3872 exp = 0; // +ve exponent also goes to '0'
3874 // a plain number with -ve exponent; preserve and check exponent
3876 throw new java.lang.ArithmeticException("Exponent Overflow:" + " " + exp);
3879 mant = ZERO.mant; // canonical mantissa