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