• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GENERATED SOURCE. DO NOT MODIFY. */
2 // © 2016 and later: Unicode, Inc. and others.
3 // License & terms of use: http://www.unicode.org/copyright.html#License
4 /* Generated from 'MathContext.nrx' 8 Sep 2000 11:07:48 [v2.00] */
5 /* Options: Binary Comments Crossref Format Java Logo Strictargs Strictcase Trace2 Verbose3 */
6 package ohos.global.icu.math;
7 
8 /* ------------------------------------------------------------------ */
9 /* MathContext -- Math context settings                               */
10 /* ------------------------------------------------------------------ */
11 /* Copyright IBM Corporation, 1997, 2000, 2005, 2007.  All Rights Reserved. */
12 /*                                                                    */
13 /*   The MathContext object encapsulates the settings used by the     */
14 /*   BigDecimal class; it could also be used by other arithmetics.    */
15 /* ------------------------------------------------------------------ */
16 /* Notes:                                                             */
17 /*                                                                    */
18 /* 1. The properties are checked for validity on construction, so     */
19 /*    the BigDecimal class may assume that they are correct.          */
20 /* ------------------------------------------------------------------ */
21 /* Author:    Mike Cowlishaw                                          */
22 /* 1997.09.03 Initial version (edited from netrexx.lang.RexxSet)      */
23 /* 1997.09.12 Add lostDigits property                                 */
24 /* 1998.05.02 Make the class immutable and final; drop set methods    */
25 /* 1998.06.05 Add Round (rounding modes) property                     */
26 /* 1998.06.25 Rename from DecimalContext; allow digits=0              */
27 /* 1998.10.12 change to ohos.global.icu.math package                          */
28 /* 1999.02.06 add javadoc comments                                    */
29 /* 1999.03.05 simplify; changes from discussion with J. Bloch         */
30 /* 1999.03.13 1.00 release to IBM Centre for Java Technology          */
31 /* 1999.07.10 1.04 flag serialization unused                          */
32 /* 2000.01.01 1.06 copyright update                                   */
33 /* ------------------------------------------------------------------ */
34 
35 
36 
37 
38 /**
39  * The <code>MathContext</code> immutable class encapsulates the
40  * settings understood by the operator methods of the {@link BigDecimal}
41  * class (and potentially other classes).  Operator methods are those
42  * that effect an operation on a number or a pair of numbers.
43  * <p>
44  * The settings, which are not base-dependent, comprise:
45  * <ol>
46  * <li><code>digits</code>:
47  * the number of digits (precision) to be used for an operation
48  * <li><code>form</code>:
49  * the form of any exponent that results from the operation
50  * <li><code>lostDigits</code>:
51  * whether checking for lost digits is enabled
52  * <li><code>roundingMode</code>:
53  * the algorithm to be used for rounding.
54  * </ol>
55  * <p>
56  * When provided, a <code>MathContext</code> object supplies the
57  * settings for an operation directly.
58  * <p>
59  * When <code>MathContext.DEFAULT</code> is provided for a
60  * <code>MathContext</code> parameter then the default settings are used
61  * (<code>9, SCIENTIFIC, false, ROUND_HALF_UP</code>).
62  * <p>
63  * In the <code>BigDecimal</code> class, all methods which accept a
64  * <code>MathContext</code> object defaults) also have a version of the
65  * method which does not accept a MathContext parameter.  These versions
66  * carry out unlimited precision fixed point arithmetic (as though the
67  * settings were (<code>0, PLAIN, false, ROUND_HALF_UP</code>).
68  * <p>
69  * The instance variables are shared with default access (so they are
70  * directly accessible to the <code>BigDecimal</code> class), but must
71  * never be changed.
72  * <p>
73  * The rounding mode constants have the same names and values as the
74  * constants of the same name in <code>java.math.BigDecimal</code>, to
75  * maintain compatibility with earlier versions of
76  * <code>BigDecimal</code>.
77  *
78  * @see     BigDecimal
79  * @author  Mike Cowlishaw
80  */
81 
82 public final class MathContext implements java.io.Serializable{
83  //private static final java.lang.String $0="MathContext.nrx";
84 
85  /* ----- Properties ----- */
86  /* properties public constant */
87  /**
88   * Plain (fixed point) notation, without any exponent.
89   * Used as a setting to control the form of the result of a
90   * <code>BigDecimal</code> operation.
91   * A zero result in plain form may have a decimal part of one or
92   * more zeros.
93   *
94   * @see #ENGINEERING
95   * @see #SCIENTIFIC
96   */
97  public static final int PLAIN=0; // [no exponent]
98 
99  /**
100   * Standard floating point notation (with scientific exponential
101   * format, where there is one digit before any decimal point).
102   * Used as a setting to control the form of the result of a
103   * <code>BigDecimal</code> operation.
104   * A zero result in plain form may have a decimal part of one or
105   * more zeros.
106   *
107   * @see #ENGINEERING
108   * @see #PLAIN
109   */
110  public static final int SCIENTIFIC=1; // 1 digit before .
111 
112  /**
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
118   * more zeros.
119   *
120   * @see #PLAIN
121   * @see #SCIENTIFIC
122   */
123  public static final int ENGINEERING=2; // 1-3 digits before .
124 
125  // The rounding modes match the original BigDecimal class values
126  /**
127   * Rounding mode to round to a more positive number.
128   * Used as a setting to control the rounding mode used during a
129   * <code>BigDecimal</code> operation.
130   * <p>
131   * If any of the discarded digits are non-zero then the result
132   * should be rounded towards the next more positive digit.
133   */
134  public static final int ROUND_CEILING=2;
135 
136  /**
137   * Rounding mode to round towards zero.
138   * Used as a setting to control the rounding mode used during a
139   * <code>BigDecimal</code> operation.
140   * <p>
141   * All discarded digits are ignored (truncated).  The result is
142   * neither incremented nor decremented.
143   */
144  public static final int ROUND_DOWN=1;
145 
146  /**
147   * Rounding mode to round to a more negative number.
148   * Used as a setting to control the rounding mode used during a
149   * <code>BigDecimal</code> operation.
150   * <p>
151   * If any of the discarded digits are non-zero then the result
152   * should be rounded towards the next more negative digit.
153   */
154  public static final int ROUND_FLOOR=3;
155 
156  /**
157   * Rounding mode to round to nearest neighbor, where an equidistant
158   * value is rounded down.
159   * Used as a setting to control the rounding mode used during a
160   * <code>BigDecimal</code> operation.
161   * <p>
162   * If the discarded digits represent greater than half (0.5 times)
163   * the value of a one in the next position then the result should be
164   * rounded up (away from zero).  Otherwise the discarded digits are
165   * ignored.
166   */
167  public static final int ROUND_HALF_DOWN=5;
168 
169  /**
170   * Rounding mode to round to nearest neighbor, where an equidistant
171   * value is rounded to the nearest even neighbor.
172   * Used as a setting to control the rounding mode used during a
173   * <code>BigDecimal</code> operation.
174   * <p>
175   * If the discarded digits represent greater than half (0.5 times)
176   * the value of a one in the next position then the result should be
177   * rounded up (away from zero).  If they represent less than half,
178   * then the result should be rounded down.
179   * <p>
180   * Otherwise (they represent exactly half) the result is rounded
181   * down if its rightmost digit is even, or rounded up if its
182   * rightmost digit is odd (to make an even digit).
183   */
184  public static final int ROUND_HALF_EVEN=6;
185 
186  /**
187   * Rounding mode to round to nearest neighbor, where an equidistant
188   * value is rounded up.
189   * Used as a setting to control the rounding mode used during a
190   * <code>BigDecimal</code> operation.
191   * <p>
192   * If the discarded digits represent greater than or equal to half
193   * (0.5 times) the value of a one in the next position then the result
194   * should be rounded up (away from zero).  Otherwise the discarded
195   * digits are ignored.
196   */
197  public static final int ROUND_HALF_UP=4;
198 
199  /**
200   * Rounding mode to assert that no rounding is necessary.
201   * Used as a setting to control the rounding mode used during a
202   * <code>BigDecimal</code> operation.
203   * <p>
204   * Rounding (potential loss of information) is not permitted.
205   * If any of the discarded digits are non-zero then an
206   * <code>ArithmeticException</code> should be thrown.
207   */
208  public static final int ROUND_UNNECESSARY=7;
209 
210  /**
211   * Rounding mode to round away from zero.
212   * Used as a setting to control the rounding mode used during a
213   * <code>BigDecimal</code> operation.
214   * <p>
215   * If any of the discarded digits are non-zero then the result will
216   * be rounded up (away from zero).
217   */
218  public static final int ROUND_UP=0;
219 
220 
221  /* properties shared */
222  /**
223   * The number of digits (precision) to be used for an operation.
224   * A value of 0 indicates that unlimited precision (as many digits
225   * as are required) will be used.
226   * <p>
227   * The {@link BigDecimal} operator methods use this value to
228   * determine the precision of results.
229   * Note that leading zeros (in the integer part of a number) are
230   * never significant.
231   * <p>
232   * <code>digits</code> will always be non-negative.
233   *
234   * @serial
235   */
236  int digits;
237 
238  /**
239   * The form of results from an operation.
240   * <p>
241   * The {@link BigDecimal} operator methods use this value to
242   * determine the form of results, in particular whether and how
243   * exponential notation should be used.
244   *
245   * @see #ENGINEERING
246   * @see #PLAIN
247   * @see #SCIENTIFIC
248   * @serial
249   */
250  int form; // values for this must fit in a byte
251 
252  /**
253   * Controls whether lost digits checking is enabled for an
254   * operation.
255   * Set to <code>true</code> to enable checking, or
256   * to <code>false</code> to disable checking.
257   * <p>
258   * When enabled, the {@link BigDecimal} operator methods check
259   * the precision of their operand or operands, and throw an
260   * <code>ArithmeticException</code> if an operand is more precise
261   * than the digits setting (that is, digits would be lost).
262   * When disabled, operands are rounded to the specified digits.
263   *
264   * @serial
265   */
266  boolean lostDigits;
267 
268  /**
269   * The rounding algorithm to be used for an operation.
270   * <p>
271   * The {@link BigDecimal} operator methods use this value to
272   * determine the algorithm to be used when non-zero digits have to
273   * be discarded in order to reduce the precision of a result.
274   * The value must be one of the public constants whose name starts
275   * with <code>ROUND_</code>.
276   *
277   * @see #ROUND_CEILING
278   * @see #ROUND_DOWN
279   * @see #ROUND_FLOOR
280   * @see #ROUND_HALF_DOWN
281   * @see #ROUND_HALF_EVEN
282   * @see #ROUND_HALF_UP
283   * @see #ROUND_UNNECESSARY
284   * @see #ROUND_UP
285   * @serial
286   */
287  int roundingMode;
288 
289  /* properties private constant */
290  // default settings
291  private static final int DEFAULT_FORM=SCIENTIFIC;
292  private static final int DEFAULT_DIGITS=9;
293  private static final boolean DEFAULT_LOSTDIGITS=false;
294  private static final int DEFAULT_ROUNDINGMODE=ROUND_HALF_UP;
295 
296  /* properties private constant */
297 
298  private static final int MIN_DIGITS=0; // smallest value for DIGITS.
299  private static final int MAX_DIGITS=999999999; // largest value for DIGITS.  If increased,
300  // the BigDecimal class may need update.
301  // list of valid rounding mode values, most common two first
302  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};
303 
304 
305  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
306 
307 
308 
309 
310  /* properties private constant unused */
311 
312  // Serialization version
313  private static final long serialVersionUID=7163376998892515376L;
314 
315  /* properties public constant */
316  /**
317   * A <code>MathContext</code> object initialized to the default
318   * settings for general-purpose arithmetic.  That is,
319   * <code>digits=9 form=SCIENTIFIC lostDigits=false
320   * roundingMode=ROUND_HALF_UP</code>.
321   *
322   * @see #SCIENTIFIC
323   * @see #ROUND_HALF_UP
324   */
325  public static final ohos.global.icu.math.MathContext DEFAULT=new ohos.global.icu.math.MathContext(DEFAULT_DIGITS,DEFAULT_FORM,DEFAULT_LOSTDIGITS,DEFAULT_ROUNDINGMODE);
326 
327 
328 
329 
330  /* ----- Constructors ----- */
331 
332  /**
333   * Constructs a new <code>MathContext</code> with a specified
334   * precision.
335   * The other settings are set to the default values
336   * (see {@link #DEFAULT}).
337   *
338   * An <code>IllegalArgumentException</code> is thrown if the
339   * <code>setdigits</code> parameter is out of range
340   * (&lt;0 or &gt;999999999).
341   *
342   * @param setdigits     The <code>int</code> digits setting
343   *                      for this <code>MathContext</code>.
344   * @throws IllegalArgumentException parameter out of range.
345   */
346 
MathContext(int setdigits)347  public MathContext(int setdigits){
348   this(setdigits,DEFAULT_FORM,DEFAULT_LOSTDIGITS,DEFAULT_ROUNDINGMODE);
349   return;}
350 
351 
352  /**
353   * Constructs a new <code>MathContext</code> with a specified
354   * precision and form.
355   * The other settings are set to the default values
356   * (see {@link #DEFAULT}).
357   *
358   * An <code>IllegalArgumentException</code> is thrown if the
359   * <code>setdigits</code> parameter is out of range
360   * (&lt;0 or &gt;999999999), or if the value given for the
361   * <code>setform</code> parameter is not one of the appropriate
362   * constants.
363   *
364   * @param setdigits     The <code>int</code> digits setting
365   *                      for this <code>MathContext</code>.
366   * @param setform       The <code>int</code> form setting
367   *                      for this <code>MathContext</code>.
368   * @throws IllegalArgumentException parameter out of range.
369   */
370 
MathContext(int setdigits,int setform)371  public MathContext(int setdigits,int setform){
372   this(setdigits,setform,DEFAULT_LOSTDIGITS,DEFAULT_ROUNDINGMODE);
373   return;}
374 
375  /**
376   * Constructs a new <code>MathContext</code> with a specified
377   * precision, form, and lostDigits setting.
378   * The roundingMode setting is set to its default value
379   * (see {@link #DEFAULT}).
380   *
381   * An <code>IllegalArgumentException</code> is thrown if the
382   * <code>setdigits</code> parameter is out of range
383   * (&lt;0 or &gt;999999999), or if the value given for the
384   * <code>setform</code> parameter is not one of the appropriate
385   * constants.
386   *
387   * @param setdigits     The <code>int</code> digits setting
388   *                      for this <code>MathContext</code>.
389   * @param setform       The <code>int</code> form setting
390   *                      for this <code>MathContext</code>.
391   * @param setlostdigits The <code>boolean</code> lostDigits
392   *                      setting for this <code>MathContext</code>.
393   * @throws IllegalArgumentException parameter out of range.
394   */
395 
MathContext(int setdigits,int setform,boolean setlostdigits)396  public MathContext(int setdigits,int setform,boolean setlostdigits){
397   this(setdigits,setform,setlostdigits,DEFAULT_ROUNDINGMODE);
398   return;}
399 
400  /**
401   * Constructs a new <code>MathContext</code> with a specified
402   * precision, form, lostDigits, and roundingMode setting.
403   *
404   * An <code>IllegalArgumentException</code> is thrown if the
405   * <code>setdigits</code> parameter is out of range
406   * (&lt;0 or &gt;999999999), or if the value given for the
407   * <code>setform</code> or <code>setroundingmode</code> parameters is
408   * not one of the appropriate constants.
409   *
410   * @param setdigits       The <code>int</code> digits setting
411   *                        for this <code>MathContext</code>.
412   * @param setform         The <code>int</code> form setting
413   *                        for this <code>MathContext</code>.
414   * @param setlostdigits   The <code>boolean</code> lostDigits
415   *                        setting for this <code>MathContext</code>.
416   * @param setroundingmode The <code>int</code> roundingMode setting
417   *                        for this <code>MathContext</code>.
418   * @throws IllegalArgumentException parameter out of range.
419   */
420 
MathContext(int setdigits,int setform,boolean setlostdigits,int setroundingmode)421  public MathContext(int setdigits,int setform,boolean setlostdigits,int setroundingmode){super();
422 
423 
424   // set values, after checking
425   if (setdigits!=DEFAULT_DIGITS)
426    {
427     if (setdigits<MIN_DIGITS)
428      throw new java.lang.IllegalArgumentException("Digits too small:"+" "+setdigits);
429     if (setdigits>MAX_DIGITS)
430      throw new java.lang.IllegalArgumentException("Digits too large:"+" "+setdigits);
431    }
432   {/*select*/
433   if (setform==SCIENTIFIC){
434    // [most common]
435   }else if (setform==ENGINEERING){
436   }else if (setform==PLAIN){
437   }else{
438    throw new java.lang.IllegalArgumentException("Bad form value:"+" "+setform);
439   }
440   }
441   if ((!(isValidRound(setroundingmode))))
442    throw new java.lang.IllegalArgumentException("Bad roundingMode value:"+" "+setroundingmode);
443   digits=setdigits;
444   form=setform;
445   lostDigits=setlostdigits; // [no bad value possible]
446   roundingMode=setroundingmode;
447   return;}
448 
449  /**
450   * Returns the digits setting.
451   * This value is always non-negative.
452   *
453   * @return an <code>int</code> which is the value of the digits
454   *         setting
455   */
456 
getDigits()457  public int getDigits(){
458   return digits;
459   }
460 
461  /**
462   * Returns the form setting.
463   * This will be one of
464   * {@link #ENGINEERING},
465   * {@link #PLAIN}, or
466   * {@link #SCIENTIFIC}.
467   *
468   * @return an <code>int</code> which is the value of the form setting
469   */
470 
getForm()471  public int getForm(){
472   return form;
473   }
474 
475  /**
476   * Returns the lostDigits setting.
477   * This will be either <code>true</code> (enabled) or
478   * <code>false</code> (disabled).
479   *
480   * @return a <code>boolean</code> which is the value of the lostDigits
481   *           setting
482   */
483 
getLostDigits()484  public boolean getLostDigits(){
485   return lostDigits;
486   }
487 
488  /**
489   * Returns the roundingMode setting.
490   * This will be one of
491   * {@link  #ROUND_CEILING},
492   * {@link  #ROUND_DOWN},
493   * {@link  #ROUND_FLOOR},
494   * {@link  #ROUND_HALF_DOWN},
495   * {@link  #ROUND_HALF_EVEN},
496   * {@link  #ROUND_HALF_UP},
497   * {@link  #ROUND_UNNECESSARY}, or
498   * {@link  #ROUND_UP}.
499   *
500   * @return an <code>int</code> which is the value of the roundingMode
501   *         setting
502   */
503 
getRoundingMode()504  public int getRoundingMode(){
505   return roundingMode;
506   }
507 
508  /** Returns the <code>MathContext</code> as a readable string.
509   * The <code>String</code> returned represents the settings of the
510   * <code>MathContext</code> object as four blank-delimited words
511   * separated by a single blank and with no leading or trailing blanks,
512   * as follows:
513   * <ol>
514   * <li>
515   * <code>digits=</code>, immediately followed by
516   * the value of the digits setting as a numeric word.
517   * <li>
518   * <code>form=</code>, immediately followed by
519   * the value of the form setting as an uppercase word
520   * (one of <code>SCIENTIFIC</code>, <code>PLAIN</code>, or
521   * <code>ENGINEERING</code>).
522   * <li>
523   * <code>lostDigits=</code>, immediately followed by
524   * the value of the lostDigits setting
525   * (<code>1</code> if enabled, <code>0</code> if disabled).
526   * <li>
527   * <code>roundingMode=</code>, immediately followed by
528   * the value of the roundingMode setting as a word.
529   * This word will be the same as the name of the corresponding public
530   * constant.
531   * </ol>
532   * <p>
533   * For example:
534   * <br><code>
535   * digits=9 form=SCIENTIFIC lostDigits=0 roundingMode=ROUND_HALF_UP
536   * </code>
537   * <p>
538   * Additional words may be appended to the result of
539   * <code>toString</code> in the future if more properties are added
540   * to the class.
541   *
542   * @return a <code>String</code> representing the context settings.
543   */
544 
545  @Override
toString()546  public java.lang.String toString(){
547   java.lang.String formstr=null;
548   int r=0;
549   java.lang.String roundword=null;
550   {/*select*/
551   if (form==SCIENTIFIC)
552    formstr="SCIENTIFIC";
553   else if (form==ENGINEERING)
554    formstr="ENGINEERING";
555   else{
556    formstr="PLAIN";/* form=PLAIN */
557   }
558   }
559   {int $1=ROUNDS.length;r=0;r:for(;$1>0;$1--,r++){
560    if (roundingMode==ROUNDS[r])
561     {
562      roundword=ROUNDWORDS[r];
563      break r;
564     }
565    }
566   }/*r*/
567   return "digits="+digits+" "+"form="+formstr+" "+"lostDigits="+(lostDigits?"1":"0")+" "+"roundingMode="+roundword;
568   }
569 
570 
571  /* <sgml> Test whether round is valid. </sgml> */
572  // This could be made shared for use by BigDecimal for setScale.
573 
isValidRound(int testround)574  private static boolean isValidRound(int testround){
575   int r=0;
576   {int $2=ROUNDS.length;for(r=0;$2>0;$2--,r++){
577    if (testround==ROUNDS[r])
578     return true;
579    }
580   }/*r*/
581   return false;
582   }
583  }
584