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