1 /* Generated from 'MathContext.nrx' 8 Sep 2000 11:07:48 [v2.00] */
\r
2 /* Options: Binary Comments Crossref Format Java Logo Strictargs Strictcase Trace2 Verbose3 */
\r
3 package com.ibm.icu.math;
\r
5 /* ------------------------------------------------------------------ */
\r
6 /* MathContext -- Math context settings */
\r
7 /* ------------------------------------------------------------------ */
\r
8 /* Copyright IBM Corporation, 1997, 2000, 2005, 2007. All Rights Reserved. */
\r
10 /* The MathContext object encapsulates the settings used by the */
\r
11 /* BigDecimal class; it could also be used by other arithmetics. */
\r
12 /* ------------------------------------------------------------------ */
\r
15 /* 1. The properties are checked for validity on construction, so */
\r
16 /* the BigDecimal class may assume that they are correct. */
\r
17 /* ------------------------------------------------------------------ */
\r
18 /* Author: Mike Cowlishaw */
\r
19 /* 1997.09.03 Initial version (edited from netrexx.lang.RexxSet) */
\r
20 /* 1997.09.12 Add lostDigits property */
\r
21 /* 1998.05.02 Make the class immutable and final; drop set methods */
\r
22 /* 1998.06.05 Add Round (rounding modes) property */
\r
23 /* 1998.06.25 Rename from DecimalContext; allow digits=0 */
\r
24 /* 1998.10.12 change to com.ibm.icu.math package */
\r
25 /* 1999.02.06 add javadoc comments */
\r
26 /* 1999.03.05 simplify; changes from discussion with J. Bloch */
\r
27 /* 1999.03.13 1.00 release to IBM Centre for Java Technology */
\r
28 /* 1999.07.10 1.04 flag serialization unused */
\r
29 /* 2000.01.01 1.06 copyright update */
\r
30 /* ------------------------------------------------------------------ */
\r
36 * The <code>MathContext</code> immutable class encapsulates the
\r
37 * settings understood by the operator methods of the {@link BigDecimal}
\r
38 * class (and potentially other classes). Operator methods are those
\r
39 * that effect an operation on a number or a pair of numbers.
\r
41 * The settings, which are not base-dependent, comprise:
\r
43 * <li><code>digits</code>:
\r
44 * the number of digits (precision) to be used for an operation
\r
45 * <li><code>form</code>:
\r
46 * the form of any exponent that results from the operation
\r
47 * <li><code>lostDigits</code>:
\r
48 * whether checking for lost digits is enabled
\r
49 * <li><code>roundingMode</code>:
\r
50 * the algorithm to be used for rounding.
\r
53 * When provided, a <code>MathContext</code> object supplies the
\r
54 * settings for an operation directly.
\r
56 * When <code>MathContext.DEFAULT</code> is provided for a
\r
57 * <code>MathContext</code> parameter then the default settings are used
\r
58 * (<code>9, SCIENTIFIC, false, ROUND_HALF_UP</code>).
\r
60 * In the <code>BigDecimal</code> class, all methods which accept a
\r
61 * <code>MathContext</code> object defaults) also have a version of the
\r
62 * method which does not accept a MathContext parameter. These versions
\r
63 * carry out unlimited precision fixed point arithmetic (as though the
\r
64 * settings were (<code>0, PLAIN, false, ROUND_HALF_UP</code>).
\r
66 * The instance variables are shared with default access (so they are
\r
67 * directly accessible to the <code>BigDecimal</code> class), but must
\r
70 * The rounding mode constants have the same names and values as the
\r
71 * constants of the same name in <code>java.math.BigDecimal</code>, to
\r
72 * maintain compatibility with earlier versions of
\r
73 * <code>BigDecimal</code>.
\r
76 * @author Mike Cowlishaw
\r
80 public final class MathContext implements java.io.Serializable{
\r
81 //private static final java.lang.String $0="MathContext.nrx";
\r
83 /* ----- Properties ----- */
\r
84 /* properties public constant */
\r
86 * Plain (fixed point) notation, without any exponent.
\r
87 * Used as a setting to control the form of the result of a
\r
88 * <code>BigDecimal</code> operation.
\r
89 * A zero result in plain form may have a decimal part of one or
\r
96 public static final int PLAIN=0; // [no exponent]
\r
99 * Standard floating point notation (with scientific exponential
\r
100 * format, where there is one digit before any decimal point).
\r
101 * Used as a setting to control the form of the result of a
\r
102 * <code>BigDecimal</code> operation.
\r
103 * A zero result in plain form may have a decimal part of one or
\r
106 * @see #ENGINEERING
\r
110 public static final int SCIENTIFIC=1; // 1 digit before .
\r
113 * Standard floating point notation (with engineering exponential
\r
114 * format, where the power of ten is a multiple of 3).
\r
115 * Used as a setting to control the form of the result of a
\r
116 * <code>BigDecimal</code> operation.
\r
117 * A zero result in plain form may have a decimal part of one or
\r
124 public static final int ENGINEERING=2; // 1-3 digits before .
\r
126 // The rounding modes match the original BigDecimal class values
\r
128 * Rounding mode to round to a more positive number.
\r
129 * Used as a setting to control the rounding mode used during a
\r
130 * <code>BigDecimal</code> operation.
\r
132 * If any of the discarded digits are non-zero then the result
\r
133 * should be rounded towards the next more positive digit.
\r
136 public static final int ROUND_CEILING=2;
\r
139 * Rounding mode to round towards zero.
\r
140 * Used as a setting to control the rounding mode used during a
\r
141 * <code>BigDecimal</code> operation.
\r
143 * All discarded digits are ignored (truncated). The result is
\r
144 * neither incremented nor decremented.
\r
147 public static final int ROUND_DOWN=1;
\r
150 * Rounding mode to round to a more negative number.
\r
151 * Used as a setting to control the rounding mode used during a
\r
152 * <code>BigDecimal</code> operation.
\r
154 * If any of the discarded digits are non-zero then the result
\r
155 * should be rounded towards the next more negative digit.
\r
158 public static final int ROUND_FLOOR=3;
\r
161 * Rounding mode to round to nearest neighbor, where an equidistant
\r
162 * value is rounded down.
\r
163 * Used as a setting to control the rounding mode used during a
\r
164 * <code>BigDecimal</code> operation.
\r
166 * If the discarded digits represent greater than half (0.5 times)
\r
167 * the value of a one in the next position then the result should be
\r
168 * rounded up (away from zero). Otherwise the discarded digits are
\r
172 public static final int ROUND_HALF_DOWN=5;
\r
175 * Rounding mode to round to nearest neighbor, where an equidistant
\r
176 * value is rounded to the nearest even neighbor.
\r
177 * Used as a setting to control the rounding mode used during a
\r
178 * <code>BigDecimal</code> operation.
\r
180 * If the discarded digits represent greater than half (0.5 times)
\r
181 * the value of a one in the next position then the result should be
\r
182 * rounded up (away from zero). If they represent less than half,
\r
183 * then the result should be rounded down.
\r
185 * Otherwise (they represent exactly half) the result is rounded
\r
186 * down if its rightmost digit is even, or rounded up if its
\r
187 * rightmost digit is odd (to make an even digit).
\r
190 public static final int ROUND_HALF_EVEN=6;
\r
193 * Rounding mode to round to nearest neighbor, where an equidistant
\r
194 * value is rounded up.
\r
195 * Used as a setting to control the rounding mode used during a
\r
196 * <code>BigDecimal</code> operation.
\r
198 * If the discarded digits represent greater than or equal to half
\r
199 * (0.5 times) the value of a one in the next position then the result
\r
200 * should be rounded up (away from zero). Otherwise the discarded
\r
201 * digits are ignored.
\r
204 public static final int ROUND_HALF_UP=4;
\r
207 * Rounding mode to assert that no rounding is necessary.
\r
208 * Used as a setting to control the rounding mode used during a
\r
209 * <code>BigDecimal</code> operation.
\r
211 * Rounding (potential loss of information) is not permitted.
\r
212 * If any of the discarded digits are non-zero then an
\r
213 * <code>ArithmeticException</code> should be thrown.
\r
216 public static final int ROUND_UNNECESSARY=7;
\r
219 * Rounding mode to round away from zero.
\r
220 * Used as a setting to control the rounding mode used during a
\r
221 * <code>BigDecimal</code> operation.
\r
223 * If any of the discarded digits are non-zero then the result will
\r
224 * be rounded up (away from zero).
\r
227 public static final int ROUND_UP=0;
\r
230 /* properties shared */
\r
232 * The number of digits (precision) to be used for an operation.
\r
233 * A value of 0 indicates that unlimited precision (as many digits
\r
234 * as are required) will be used.
\r
236 * The {@link BigDecimal} operator methods use this value to
\r
237 * determine the precision of results.
\r
238 * Note that leading zeros (in the integer part of a number) are
\r
239 * never significant.
\r
241 * <code>digits</code> will always be non-negative.
\r
248 * The form of results from an operation.
\r
250 * The {@link BigDecimal} operator methods use this value to
\r
251 * determine the form of results, in particular whether and how
\r
252 * exponential notation should be used.
\r
254 * @see #ENGINEERING
\r
259 int form; // values for this must fit in a byte
\r
262 * Controls whether lost digits checking is enabled for an
\r
264 * Set to <code>true</code> to enable checking, or
\r
265 * to <code>false</code> to disable checking.
\r
267 * When enabled, the {@link BigDecimal} operator methods check
\r
268 * the precision of their operand or operands, and throw an
\r
269 * <code>ArithmeticException</code> if an operand is more precise
\r
270 * than the digits setting (that is, digits would be lost).
\r
271 * When disabled, operands are rounded to the specified digits.
\r
275 boolean lostDigits;
\r
278 * The rounding algorithm to be used for an operation.
\r
280 * The {@link BigDecimal} operator methods use this value to
\r
281 * determine the algorithm to be used when non-zero digits have to
\r
282 * be discarded in order to reduce the precision of a result.
\r
283 * The value must be one of the public constants whose name starts
\r
284 * with <code>ROUND_</code>.
\r
286 * @see #ROUND_CEILING
\r
288 * @see #ROUND_FLOOR
\r
289 * @see #ROUND_HALF_DOWN
\r
290 * @see #ROUND_HALF_EVEN
\r
291 * @see #ROUND_HALF_UP
\r
292 * @see #ROUND_UNNECESSARY
\r
298 /* properties private constant */
\r
299 // default settings
\r
300 private static final int DEFAULT_FORM=SCIENTIFIC;
\r
301 private static final int DEFAULT_DIGITS=9;
\r
302 private static final boolean DEFAULT_LOSTDIGITS=false;
\r
303 private static final int DEFAULT_ROUNDINGMODE=ROUND_HALF_UP;
\r
305 /* properties private constant */
\r
307 private static final int MIN_DIGITS=0; // smallest value for DIGITS.
\r
308 private static final int MAX_DIGITS=999999999; // largest value for DIGITS. If increased,
\r
309 // the BigDecimal class may need update.
\r
310 // list of valid rounding mode values, most common two first
\r
311 private static final int ROUNDS[]=new int[]{ROUND_HALF_UP,ROUND_UNNECESSARY,ROUND_CEILING,ROUND_DOWN,ROUND_FLOOR,ROUND_HALF_DOWN,ROUND_HALF_EVEN,ROUND_UP};
\r
314 private static final java.lang.String ROUNDWORDS[]=new java.lang.String[]{"ROUND_HALF_UP","ROUND_UNNECESSARY","ROUND_CEILING","ROUND_DOWN","ROUND_FLOOR","ROUND_HALF_DOWN","ROUND_HALF_EVEN","ROUND_UP"}; // matching names of the ROUNDS values
\r
319 /* properties private constant unused */
\r
321 // Serialization version
\r
322 private static final long serialVersionUID=7163376998892515376L;
\r
324 /* properties public constant */
\r
326 * A <code>MathContext</code> object initialized to the default
\r
327 * settings for general-purpose arithmetic. That is,
\r
328 * <code>digits=9 form=SCIENTIFIC lostDigits=false
\r
329 * roundingMode=ROUND_HALF_UP</code>.
\r
332 * @see #ROUND_HALF_UP
\r
335 public static final com.ibm.icu.math.MathContext DEFAULT=new com.ibm.icu.math.MathContext(DEFAULT_DIGITS,DEFAULT_FORM,DEFAULT_LOSTDIGITS,DEFAULT_ROUNDINGMODE);
\r
340 /* ----- Constructors ----- */
\r
343 * Constructs a new <code>MathContext</code> with a specified
\r
345 * The other settings are set to the default values
\r
346 * (see {@link #DEFAULT}).
\r
348 * An <code>IllegalArgumentException</code> is thrown if the
\r
349 * <code>setdigits</code> parameter is out of range
\r
350 * (<0 or >999999999).
\r
352 * @param setdigits The <code>int</code> digits setting
\r
353 * for this <code>MathContext</code>.
\r
354 * @throws IllegalArgumentException parameter out of range.
\r
358 public MathContext(int setdigits){
\r
359 this(setdigits,DEFAULT_FORM,DEFAULT_LOSTDIGITS,DEFAULT_ROUNDINGMODE);
\r
364 * Constructs a new <code>MathContext</code> with a specified
\r
365 * precision and form.
\r
366 * The other settings are set to the default values
\r
367 * (see {@link #DEFAULT}).
\r
369 * An <code>IllegalArgumentException</code> is thrown if the
\r
370 * <code>setdigits</code> parameter is out of range
\r
371 * (<0 or >999999999), or if the value given for the
\r
372 * <code>setform</code> parameter is not one of the appropriate
\r
375 * @param setdigits The <code>int</code> digits setting
\r
376 * for this <code>MathContext</code>.
\r
377 * @param setform The <code>int</code> form setting
\r
378 * for this <code>MathContext</code>.
\r
379 * @throws IllegalArgumentException parameter out of range.
\r
383 public MathContext(int setdigits,int setform){
\r
384 this(setdigits,setform,DEFAULT_LOSTDIGITS,DEFAULT_ROUNDINGMODE);
\r
388 * Constructs a new <code>MathContext</code> with a specified
\r
389 * precision, form, and lostDigits setting.
\r
390 * The roundingMode setting is set to its default value
\r
391 * (see {@link #DEFAULT}).
\r
393 * An <code>IllegalArgumentException</code> is thrown if the
\r
394 * <code>setdigits</code> parameter is out of range
\r
395 * (<0 or >999999999), or if the value given for the
\r
396 * <code>setform</code> parameter is not one of the appropriate
\r
399 * @param setdigits The <code>int</code> digits setting
\r
400 * for this <code>MathContext</code>.
\r
401 * @param setform The <code>int</code> form setting
\r
402 * for this <code>MathContext</code>.
\r
403 * @param setlostdigits The <code>boolean</code> lostDigits
\r
404 * setting for this <code>MathContext</code>.
\r
405 * @throws IllegalArgumentException parameter out of range.
\r
409 public MathContext(int setdigits,int setform,boolean setlostdigits){
\r
410 this(setdigits,setform,setlostdigits,DEFAULT_ROUNDINGMODE);
\r
414 * Constructs a new <code>MathContext</code> with a specified
\r
415 * precision, form, lostDigits, and roundingMode setting.
\r
417 * An <code>IllegalArgumentException</code> is thrown if the
\r
418 * <code>setdigits</code> parameter is out of range
\r
419 * (<0 or >999999999), or if the value given for the
\r
420 * <code>setform</code> or <code>setroundingmode</code> parameters is
\r
421 * not one of the appropriate constants.
\r
423 * @param setdigits The <code>int</code> digits setting
\r
424 * for this <code>MathContext</code>.
\r
425 * @param setform The <code>int</code> form setting
\r
426 * for this <code>MathContext</code>.
\r
427 * @param setlostdigits The <code>boolean</code> lostDigits
\r
428 * setting for this <code>MathContext</code>.
\r
429 * @param setroundingmode The <code>int</code> roundingMode setting
\r
430 * for this <code>MathContext</code>.
\r
431 * @throws IllegalArgumentException parameter out of range.
\r
435 public MathContext(int setdigits,int setform,boolean setlostdigits,int setroundingmode){super();
\r
438 // set values, after checking
\r
439 if (setdigits!=DEFAULT_DIGITS)
\r
441 if (setdigits<MIN_DIGITS)
\r
442 throw new java.lang.IllegalArgumentException("Digits too small:"+" "+setdigits);
\r
443 if (setdigits>MAX_DIGITS)
\r
444 throw new java.lang.IllegalArgumentException("Digits too large:"+" "+setdigits);
\r
447 if (setform==SCIENTIFIC){
\r
449 }else if (setform==ENGINEERING){
\r
450 }else if (setform==PLAIN){
\r
452 throw new java.lang.IllegalArgumentException("Bad form value:"+" "+setform);
\r
455 if ((!(isValidRound(setroundingmode))))
\r
456 throw new java.lang.IllegalArgumentException("Bad roundingMode value:"+" "+setroundingmode);
\r
459 lostDigits=setlostdigits; // [no bad value possible]
\r
460 roundingMode=setroundingmode;
\r
464 * Returns the digits setting.
\r
465 * This value is always non-negative.
\r
467 * @return an <code>int</code> which is the value of the digits
\r
472 public int getDigits(){
\r
477 * Returns the form setting.
\r
478 * This will be one of
\r
479 * {@link #ENGINEERING},
\r
480 * {@link #PLAIN}, or
\r
481 * {@link #SCIENTIFIC}.
\r
483 * @return an <code>int</code> which is the value of the form setting
\r
487 public int getForm(){
\r
492 * Returns the lostDigits setting.
\r
493 * This will be either <code>true</code> (enabled) or
\r
494 * <code>false</code> (disabled).
\r
496 * @return a <code>boolean</code> which is the value of the lostDigits
\r
501 public boolean getLostDigits(){
\r
506 * Returns the roundingMode setting.
\r
507 * This will be one of
\r
508 * {@link #ROUND_CEILING},
\r
509 * {@link #ROUND_DOWN},
\r
510 * {@link #ROUND_FLOOR},
\r
511 * {@link #ROUND_HALF_DOWN},
\r
512 * {@link #ROUND_HALF_EVEN},
\r
513 * {@link #ROUND_HALF_UP},
\r
514 * {@link #ROUND_UNNECESSARY}, or
\r
515 * {@link #ROUND_UP}.
\r
517 * @return an <code>int</code> which is the value of the roundingMode
\r
522 public int getRoundingMode(){
\r
523 return roundingMode;
\r
526 /** Returns the <code>MathContext</code> as a readable string.
\r
527 * The <code>String</code> returned represents the settings of the
\r
528 * <code>MathContext</code> object as four blank-delimited words
\r
529 * separated by a single blank and with no leading or trailing blanks,
\r
533 * <code>digits=</code>, immediately followed by
\r
534 * the value of the digits setting as a numeric word.
\r
536 * <code>form=</code>, immediately followed by
\r
537 * the value of the form setting as an uppercase word
\r
538 * (one of <code>SCIENTIFIC</code>, <code>PLAIN</code>, or
\r
539 * <code>ENGINEERING</code>).
\r
541 * <code>lostDigits=</code>, immediately followed by
\r
542 * the value of the lostDigits setting
\r
543 * (<code>1</code> if enabled, <code>0</code> if disabled).
\r
545 * <code>roundingMode=</code>, immediately followed by
\r
546 * the value of the roundingMode setting as a word.
\r
547 * This word will be the same as the name of the corresponding public
\r
553 * digits=9 form=SCIENTIFIC lostDigits=0 roundingMode=ROUND_HALF_UP
\r
556 * Additional words may be appended to the result of
\r
557 * <code>toString</code> in the future if more properties are added
\r
560 * @return a <code>String</code> representing the context settings.
\r
564 public java.lang.String toString(){
\r
565 java.lang.String formstr=null;
\r
567 java.lang.String roundword=null;
\r
569 if (form==SCIENTIFIC)
\r
570 formstr="SCIENTIFIC";
\r
571 else if (form==ENGINEERING)
\r
572 formstr="ENGINEERING";
\r
574 formstr="PLAIN";/* form=PLAIN */
\r
577 {int $1=ROUNDS.length;r=0;r:for(;$1>0;$1--,r++){
\r
578 if (roundingMode==ROUNDS[r])
\r
580 roundword=ROUNDWORDS[r];
\r
585 return "digits="+digits+" "+"form="+formstr+" "+"lostDigits="+(lostDigits?"1":"0")+" "+"roundingMode="+roundword;
\r
589 /* <sgml> Test whether round is valid. </sgml> */
\r
590 // This could be made shared for use by BigDecimal for setScale.
\r
592 private static boolean isValidRound(int testround){
\r
594 {int $2=ROUNDS.length;for(r=0;$2>0;$2--,r++){
\r
595 if (testround==ROUNDS[r])
\r