src/share/classes/java/math/MathContext.java

Print this page




 109     /**
 110      * The number of digits to be used for an operation.  A value of 0
 111      * indicates that unlimited precision (as many digits as are
 112      * required) will be used.  Note that leading zeros (in the
 113      * coefficient of a number) are never significant.
 114      *
 115      * <p>{@code precision} will always be non-negative.
 116      *
 117      * @serial
 118      */
 119     final int precision;
 120 
 121     /**
 122      * The rounding algorithm to be used for an operation.
 123      *
 124      * @see RoundingMode
 125      * @serial
 126      */
 127     final RoundingMode roundingMode;
 128 
 129     /**
 130      *  Lookaside for the rounding points (the numbers which determine
 131      *  whether the coefficient of a number will require rounding).
 132      *  These will be present if {@code precision > 0} and
 133      *  {@code precision <= MAX_LOOKASIDE}.  In this case they will share the
 134      *  {@code BigInteger int[]} array.  Note that the transients
 135      *  cannot be {@code final} because they are reconstructed on
 136      *  deserialization.
 137      */
 138     transient BigInteger roundingMax = null;
 139     transient BigInteger roundingMin = null;
 140     private static final int MAX_LOOKASIDE = 1000;
 141 
 142     /* ----- Constructors ----- */
 143 
 144     /**
 145      * Constructs a new {@code MathContext} with the specified
 146      * precision and the {@link RoundingMode#HALF_UP HALF_UP} rounding
 147      * mode.
 148      *
 149      * @param setPrecision The non-negative {@code int} precision setting.
 150      * @throws IllegalArgumentException if the {@code setPrecision} parameter is less
 151      *         than zero.
 152      */
 153     public MathContext(int setPrecision) {
 154         this(setPrecision, DEFAULT_ROUNDINGMODE);
 155         return;
 156     }
 157 
 158     /**
 159      * Constructs a new {@code MathContext} with a specified
 160      * precision and rounding mode.
 161      *
 162      * @param setPrecision The non-negative {@code int} precision setting.
 163      * @param setRoundingMode The rounding mode to use.
 164      * @throws IllegalArgumentException if the {@code setPrecision} parameter is less
 165      *         than zero.
 166      * @throws NullPointerException if the rounding mode argument is {@code null}
 167      */
 168     public MathContext(int setPrecision,
 169                        RoundingMode setRoundingMode) {
 170         if (setPrecision < MIN_DIGITS)
 171             throw new IllegalArgumentException("Digits < 0");
 172         if (setRoundingMode == null)
 173             throw new NullPointerException("null RoundingMode");
 174 
 175         precision = setPrecision;
 176         if (precision > 0 && precision <= MAX_LOOKASIDE) {
 177             roundingMax = BigInteger.TEN.pow(precision);
 178             roundingMin = roundingMax.negate();
 179         }
 180 
 181         roundingMode = setRoundingMode;
 182         return;
 183     }
 184 
 185     /**
 186      * Constructs a new {@code MathContext} from a string.
 187      *
 188      * The string must be in the same format as that produced by the
 189      * {@link #toString} method.
 190      *
 191      * <p>An {@code IllegalArgumentException} is thrown if the precision
 192      * section of the string is out of range ({@code < 0}) or the string is
 193      * not in the format created by the {@link #toString} method.
 194      *
 195      * @param val The string to be parsed
 196      * @throws IllegalArgumentException if the precision section is out of range
 197      * or of incorrect format
 198      * @throws NullPointerException if the argument is {@code null}
 199      */
 200     public MathContext(String val) {


 204             throw new NullPointerException("null String");
 205         try { // any error here is a string format problem
 206             if (!val.startsWith("precision=")) throw new RuntimeException();
 207             int fence = val.indexOf(' ');    // could be -1
 208             int off = 10;                     // where value starts
 209             setPrecision = Integer.parseInt(val.substring(10, fence));
 210 
 211             if (!val.startsWith("roundingMode=", fence+1))
 212                 throw new RuntimeException();
 213             off = fence + 1 + 13;
 214             String str = val.substring(off, val.length());
 215             roundingMode = RoundingMode.valueOf(str);
 216         } catch (RuntimeException re) {
 217             throw new IllegalArgumentException("bad string format");
 218         }
 219 
 220         if (setPrecision < MIN_DIGITS)
 221             throw new IllegalArgumentException("Digits < 0");
 222         // the other parameters cannot be invalid if we got here
 223         precision = setPrecision;
 224         if (precision > 0 && precision <= MAX_LOOKASIDE) {
 225             roundingMax = BigInteger.TEN.pow(precision);
 226             roundingMin = roundingMax.negate();
 227         }
 228     }
 229 
 230     /**
 231      * Returns the {@code precision} setting.
 232      * This value is always non-negative.
 233      *
 234      * @return an {@code int} which is the value of the {@code precision}
 235      *         setting
 236      */
 237     public int getPrecision() {
 238         return precision;
 239     }
 240 
 241     /**
 242      * Returns the roundingMode setting.
 243      * This will be one of
 244      * {@link  RoundingMode#CEILING},
 245      * {@link  RoundingMode#DOWN},
 246      * {@link  RoundingMode#FLOOR},
 247      * {@link  RoundingMode#HALF_DOWN},
 248      * {@link  RoundingMode#HALF_EVEN},


 326     // Private methods
 327 
 328     /**
 329      * Reconstitute the {@code MathContext} instance from a stream (that is,
 330      * deserialize it).
 331      *
 332      * @param s the stream being read.
 333      */
 334     private void readObject(java.io.ObjectInputStream s)
 335         throws java.io.IOException, ClassNotFoundException {
 336         s.defaultReadObject();     // read in all fields
 337         // validate possibly bad fields
 338         if (precision < MIN_DIGITS) {
 339             String message = "MathContext: invalid digits in stream";
 340             throw new java.io.StreamCorruptedException(message);
 341         }
 342         if (roundingMode == null) {
 343             String message = "MathContext: null roundingMode in stream";
 344             throw new java.io.StreamCorruptedException(message);
 345         }
 346         // Set the lookaside, if applicable
 347         if (precision <= MAX_LOOKASIDE) {
 348             roundingMax = BigInteger.TEN.pow(precision);
 349             roundingMin = roundingMax.negate();
 350         }
 351     }
 352 
 353 }


 109     /**
 110      * The number of digits to be used for an operation.  A value of 0
 111      * indicates that unlimited precision (as many digits as are
 112      * required) will be used.  Note that leading zeros (in the
 113      * coefficient of a number) are never significant.
 114      *
 115      * <p>{@code precision} will always be non-negative.
 116      *
 117      * @serial
 118      */
 119     final int precision;
 120 
 121     /**
 122      * The rounding algorithm to be used for an operation.
 123      *
 124      * @see RoundingMode
 125      * @serial
 126      */
 127     final RoundingMode roundingMode;
 128 













 129     /* ----- Constructors ----- */
 130 
 131     /**
 132      * Constructs a new {@code MathContext} with the specified
 133      * precision and the {@link RoundingMode#HALF_UP HALF_UP} rounding
 134      * mode.
 135      *
 136      * @param setPrecision The non-negative {@code int} precision setting.
 137      * @throws IllegalArgumentException if the {@code setPrecision} parameter is less
 138      *         than zero.
 139      */
 140     public MathContext(int setPrecision) {
 141         this(setPrecision, DEFAULT_ROUNDINGMODE);
 142         return;
 143     }
 144 
 145     /**
 146      * Constructs a new {@code MathContext} with a specified
 147      * precision and rounding mode.
 148      *
 149      * @param setPrecision The non-negative {@code int} precision setting.
 150      * @param setRoundingMode The rounding mode to use.
 151      * @throws IllegalArgumentException if the {@code setPrecision} parameter is less
 152      *         than zero.
 153      * @throws NullPointerException if the rounding mode argument is {@code null}
 154      */
 155     public MathContext(int setPrecision,
 156                        RoundingMode setRoundingMode) {
 157         if (setPrecision < MIN_DIGITS)
 158             throw new IllegalArgumentException("Digits < 0");
 159         if (setRoundingMode == null)
 160             throw new NullPointerException("null RoundingMode");
 161 
 162         precision = setPrecision;





 163         roundingMode = setRoundingMode;
 164         return;
 165     }
 166 
 167     /**
 168      * Constructs a new {@code MathContext} from a string.
 169      *
 170      * The string must be in the same format as that produced by the
 171      * {@link #toString} method.
 172      *
 173      * <p>An {@code IllegalArgumentException} is thrown if the precision
 174      * section of the string is out of range ({@code < 0}) or the string is
 175      * not in the format created by the {@link #toString} method.
 176      *
 177      * @param val The string to be parsed
 178      * @throws IllegalArgumentException if the precision section is out of range
 179      * or of incorrect format
 180      * @throws NullPointerException if the argument is {@code null}
 181      */
 182     public MathContext(String val) {


 186             throw new NullPointerException("null String");
 187         try { // any error here is a string format problem
 188             if (!val.startsWith("precision=")) throw new RuntimeException();
 189             int fence = val.indexOf(' ');    // could be -1
 190             int off = 10;                     // where value starts
 191             setPrecision = Integer.parseInt(val.substring(10, fence));
 192 
 193             if (!val.startsWith("roundingMode=", fence+1))
 194                 throw new RuntimeException();
 195             off = fence + 1 + 13;
 196             String str = val.substring(off, val.length());
 197             roundingMode = RoundingMode.valueOf(str);
 198         } catch (RuntimeException re) {
 199             throw new IllegalArgumentException("bad string format");
 200         }
 201 
 202         if (setPrecision < MIN_DIGITS)
 203             throw new IllegalArgumentException("Digits < 0");
 204         // the other parameters cannot be invalid if we got here
 205         precision = setPrecision;



 206     }

 207 
 208     /**
 209      * Returns the {@code precision} setting.
 210      * This value is always non-negative.
 211      *
 212      * @return an {@code int} which is the value of the {@code precision}
 213      *         setting
 214      */
 215     public int getPrecision() {
 216         return precision;
 217     }
 218 
 219     /**
 220      * Returns the roundingMode setting.
 221      * This will be one of
 222      * {@link  RoundingMode#CEILING},
 223      * {@link  RoundingMode#DOWN},
 224      * {@link  RoundingMode#FLOOR},
 225      * {@link  RoundingMode#HALF_DOWN},
 226      * {@link  RoundingMode#HALF_EVEN},


 304     // Private methods
 305 
 306     /**
 307      * Reconstitute the {@code MathContext} instance from a stream (that is,
 308      * deserialize it).
 309      *
 310      * @param s the stream being read.
 311      */
 312     private void readObject(java.io.ObjectInputStream s)
 313         throws java.io.IOException, ClassNotFoundException {
 314         s.defaultReadObject();     // read in all fields
 315         // validate possibly bad fields
 316         if (precision < MIN_DIGITS) {
 317             String message = "MathContext: invalid digits in stream";
 318             throw new java.io.StreamCorruptedException(message);
 319         }
 320         if (roundingMode == null) {
 321             String message = "MathContext: null roundingMode in stream";
 322             throw new java.io.StreamCorruptedException(message);
 323         }




 324     }

 325 
 326 }