1 /*
   2  * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * Portions Copyright IBM Corporation, 2001. All Rights Reserved.
  28  */
  29 package java.math;
  30 
  31 /**
  32  * Specifies a <i>rounding behavior</i> for numerical operations
  33  * capable of discarding precision. Each rounding mode indicates how
  34  * the least significant returned digit of a rounded result is to be
  35  * calculated.  If fewer digits are returned than the digits needed to
  36  * represent the exact numerical result, the discarded digits will be
  37  * referred to as the <i>discarded fraction</i> regardless the digits'
  38  * contribution to the value of the number.  In other words,
  39  * considered as a numerical value, the discarded fraction could have
  40  * an absolute value greater than one.
  41  *
  42  * <p>Each rounding mode description includes a table listing how
  43  * different two-digit decimal values would round to a one digit
  44  * decimal value under the rounding mode in question.  The result
  45  * column in the tables could be gotten by creating a
  46  * {@code BigDecimal} number with the specified value, forming a
  47  * {@link MathContext} object with the proper settings
  48  * ({@code precision} set to {@code 1}, and the
  49  * {@code roundingMode} set to the rounding mode in question), and
  50  * calling {@link BigDecimal#round round} on this number with the
  51  * proper {@code MathContext}.  A summary table showing the results
  52  * of these rounding operations for all rounding modes appears below.
  53  *
  54  *<table class="striped">
  55  * <caption><b>Summary of Rounding Operations Under Different Rounding Modes</b></caption>
  56  * <thead>
  57  * <tr><th scope="col" rowspan="2">Input Number</th><th scope="col"colspan=8>Result of rounding input to one digit with the given
  58  *                           rounding mode</th>
  59  * <tr style="vertical-align:top">
  60  *                               <th>{@code UP}</th>
  61  *                                           <th>{@code DOWN}</th>
  62  *                                                        <th>{@code CEILING}</th>
  63  *                                                                       <th>{@code FLOOR}</th>
  64  *                                                                                    <th>{@code HALF_UP}</th>
  65  *                                                                                                   <th>{@code HALF_DOWN}</th>
  66  *                                                                                                                    <th>{@code HALF_EVEN}</th>
  67  *                                                                                                                                     <th>{@code UNNECESSARY}</th>
  68  * </thead>
  69  * <tbody style="text-align:right">
  70  *
  71  * <tr><th scope="row">5.5</th>  <td>6</td>  <td>5</td>    <td>6</td>    <td>5</td>  <td>6</td>      <td>5</td>       <td>6</td>       <td>throw {@code ArithmeticException}</td>
  72  * <tr><th scope="row">2.5</th>  <td>3</td>  <td>2</td>    <td>3</td>    <td>2</td>  <td>3</td>      <td>2</td>       <td>2</td>       <td>throw {@code ArithmeticException}</td>
  73  * <tr><th scope="row">1.6</th>  <td>2</td>  <td>1</td>    <td>2</td>    <td>1</td>  <td>2</td>      <td>2</td>       <td>2</td>       <td>throw {@code ArithmeticException}</td>
  74  * <tr><th scope="row">1.1</th>  <td>2</td>  <td>1</td>    <td>2</td>    <td>1</td>  <td>1</td>      <td>1</td>       <td>1</td>       <td>throw {@code ArithmeticException}</td>
  75  * <tr><th scope="row">1.0</th>  <td>1</td>  <td>1</td>    <td>1</td>    <td>1</td>  <td>1</td>      <td>1</td>       <td>1</td>       <td>1</td>
  76  * <tr><th scope="row">-1.0</th> <td>-1</td> <td>-1</td>   <td>-1</td>   <td>-1</td> <td>-1</td>     <td>-1</td>      <td>-1</td>      <td>-1</td>
  77  * <tr><th scope="row">-1.1</th> <td>-2</td> <td>-1</td>   <td>-1</td>   <td>-2</td> <td>-1</td>     <td>-1</td>      <td>-1</td>      <td>throw {@code ArithmeticException}</td>
  78  * <tr><th scope="row">-1.6</th> <td>-2</td> <td>-1</td>   <td>-1</td>   <td>-2</td> <td>-2</td>     <td>-2</td>      <td>-2</td>      <td>throw {@code ArithmeticException}</td>
  79  * <tr><th scope="row">-2.5</th> <td>-3</td> <td>-2</td>   <td>-2</td>   <td>-3</td> <td>-3</td>     <td>-2</td>      <td>-2</td>      <td>throw {@code ArithmeticException}</td>
  80  * <tr><th scope="row">-5.5</th> <td>-6</td> <td>-5</td>   <td>-5</td>   <td>-6</td> <td>-6</td>     <td>-5</td>      <td>-6</td>      <td>throw {@code ArithmeticException}</td>
  81  * </tbody>
  82  * </table>
  83  *
  84  *
  85  * <p>This {@code enum} is intended to replace the integer-based
  86  * enumeration of rounding mode constants in {@link BigDecimal}
  87  * ({@link BigDecimal#ROUND_UP}, {@link BigDecimal#ROUND_DOWN},
  88  * etc. ).
  89  *
  90  * @apiNote
  91  * Five of the rounding modes defined in this class correspond to
  92  * rounding direction attributes defined in IEEE 754-2019. Where
  93  * present, this correspondence will be noted in the documentation of
  94  * the particular constant.
  95  *
  96  * @see     BigDecimal
  97  * @see     MathContext
  98  * @author  Josh Bloch
  99  * @author  Mike Cowlishaw
 100  * @author  Joseph D. Darcy
 101  * @since 1.5
 102  */
 103 @SuppressWarnings("deprecation") // Legacy rounding mode constants in BigDecimal
 104 public enum RoundingMode {
 105 
 106         /**
 107          * Rounding mode to round away from zero.  Always increments the
 108          * digit prior to a non-zero discarded fraction.  Note that this
 109          * rounding mode never decreases the magnitude of the calculated
 110          * value.
 111          *
 112          *<p>Example:
 113          *<table class="striped">
 114          * <caption>Rounding mode UP Examples</caption>
 115          *<thead>
 116          *<tr style="vertical-align:top"><th scope="col">Input Number</th>
 117          *    <th scope="col">Input rounded to one digit<br> with {@code UP} rounding
 118          *</thead>
 119          *<tbody style="text-align:right">
 120          *<tr><th scope="row">5.5</th>  <td>6</td>
 121          *<tr><th scope="row">2.5</th>  <td>3</td>
 122          *<tr><th scope="row">1.6</th>  <td>2</td>
 123          *<tr><th scope="row">1.1</th>  <td>2</td>
 124          *<tr><th scope="row">1.0</th>  <td>1</td>
 125          *<tr><th scope="row">-1.0</th> <td>-1</td>
 126          *<tr><th scope="row">-1.1</th> <td>-2</td>
 127          *<tr><th scope="row">-1.6</th> <td>-2</td>
 128          *<tr><th scope="row">-2.5</th> <td>-3</td>
 129          *<tr><th scope="row">-5.5</th> <td>-6</td>
 130          *</tbody>
 131          *</table>
 132          */
 133     UP(BigDecimal.ROUND_UP),
 134 
 135         /**
 136          * Rounding mode to round towards zero.  Never increments the digit
 137          * prior to a discarded fraction (i.e., truncates).  Note that this
 138          * rounding mode never increases the magnitude of the calculated value.
 139          * This mode corresponds to the IEEE 754-2019 rounding
 140          * attribute roundTowardZero.
 141          * 
 142          *<p>Example:
 143          *<table class="striped">
 144          * <caption>Rounding mode DOWN Examples</caption>
 145          *<thead>
 146          *<tr style="vertical-align:top"><th scope="col">Input Number</th>
 147          *    <th scope="col">Input rounded to one digit<br> with {@code DOWN} rounding
 148          *</thead>
 149          *<tbody style="text-align:right">
 150          *<tr><th scope="row">5.5</th>  <td>5</td>
 151          *<tr><th scope="row">2.5</th>  <td>2</td>
 152          *<tr><th scope="row">1.6</th>  <td>1</td>
 153          *<tr><th scope="row">1.1</th>  <td>1</td>
 154          *<tr><th scope="row">1.0</th>  <td>1</td>
 155          *<tr><th scope="row">-1.0</th> <td>-1</td>
 156          *<tr><th scope="row">-1.1</th> <td>-1</td>
 157          *<tr><th scope="row">-1.6</th> <td>-1</td>
 158          *<tr><th scope="row">-2.5</th> <td>-2</td>
 159          *<tr><th scope="row">-5.5</th> <td>-5</td>
 160          *</tbody>
 161          *</table>
 162          */
 163     DOWN(BigDecimal.ROUND_DOWN),
 164 
 165         /**
 166          * Rounding mode to round towards positive infinity.  If the
 167          * result is positive, behaves as for {@code RoundingMode.UP};
 168          * if negative, behaves as for {@code RoundingMode.DOWN}.  Note
 169          * that this rounding mode never decreases the calculated value.
 170          * This mode corresponds to the IEEE 754-2019 rounding
 171          * attribute roundTowardPositive.
 172          *
 173          *<p>Example:
 174          *<table class="striped">
 175          * <caption>Rounding mode CEILING Examples</caption>
 176          *<thead>
 177          *<tr style="vertical-align:top"><th>Input Number</th>
 178          *    <th>Input rounded to one digit<br> with {@code CEILING} rounding
 179          *</thead>
 180          *<tbody style="text-align:right">
 181          *<tr><th scope="row">5.5</th>  <td>6</td>
 182          *<tr><th scope="row">2.5</th>  <td>3</td>
 183          *<tr><th scope="row">1.6</th>  <td>2</td>
 184          *<tr><th scope="row">1.1</th>  <td>2</td>
 185          *<tr><th scope="row">1.0</th>  <td>1</td>
 186          *<tr><th scope="row">-1.0</th> <td>-1</td>
 187          *<tr><th scope="row">-1.1</th> <td>-1</td>
 188          *<tr><th scope="row">-1.6</th> <td>-1</td>
 189          *<tr><th scope="row">-2.5</th> <td>-2</td>
 190          *<tr><th scope="row">-5.5</th> <td>-5</td>
 191          *</tbody>
 192          *</table>
 193          */
 194     CEILING(BigDecimal.ROUND_CEILING),
 195 
 196         /**
 197          * Rounding mode to round towards negative infinity.  If the
 198          * result is positive, behave as for {@code RoundingMode.DOWN};
 199          * if negative, behave as for {@code RoundingMode.UP}.  Note that
 200          * this rounding mode never increases the calculated value.
 201          * This mode corresponds to the IEEE 754-2019 rounding
 202          * attribute roundTowardNegative.
 203          *
 204          *<p>Example:
 205          *<table class="striped">
 206          * <caption>Rounding mode FLOOR Examples</caption>
 207          *<thead>
 208          *<tr style="vertical-align:top"><th scope="col">Input Number</th>
 209          *    <th scope="col">Input rounded to one digit<br> with {@code FLOOR} rounding
 210          *</thead>
 211          *<tbody style="text-align:right">
 212          *<tr><th scope="row">5.5</th>  <td>5</td>
 213          *<tr><th scope="row">2.5</th>  <td>2</td>
 214          *<tr><th scope="row">1.6</th>  <td>1</td>
 215          *<tr><th scope="row">1.1</th>  <td>1</td>
 216          *<tr><th scope="row">1.0</th>  <td>1</td>
 217          *<tr><th scope="row">-1.0</th> <td>-1</td>
 218          *<tr><th scope="row">-1.1</th> <td>-2</td>
 219          *<tr><th scope="row">-1.6</th> <td>-2</td>
 220          *<tr><th scope="row">-2.5</th> <td>-3</td>
 221          *<tr><th scope="row">-5.5</th> <td>-6</td>
 222          *</tbody>
 223          *</table>
 224          */
 225     FLOOR(BigDecimal.ROUND_FLOOR),
 226 
 227         /**
 228          * Rounding mode to round towards {@literal "nearest neighbor"}
 229          * unless both neighbors are equidistant, in which case round up.
 230          * Behaves as for {@code RoundingMode.UP} if the discarded
 231          * fraction is &ge; 0.5; otherwise, behaves as for
 232          * {@code RoundingMode.DOWN}.  Note that this is the rounding
 233          * mode commonly taught at school.
 234          * This mode corresponds to the IEEE 754-2019 rounding
 235          * attribute roundTiesToAway.
 236          *
 237          *<p>Example:
 238          *<table class="striped">
 239          * <caption>Rounding mode HALF_UP Examples</caption>
 240          *<thead>
 241          *<tr style="vertical-align:top"><th scope="col">Input Number</th>
 242          *    <th scope="col">Input rounded to one digit<br> with {@code HALF_UP} rounding
 243          *</thead>
 244          *<tbody style="text-align:right">
 245          *<tr><th scope="row">5.5</th>  <td>6</td>
 246          *<tr><th scope="row">2.5</th>  <td>3</td>
 247          *<tr><th scope="row">1.6</th>  <td>2</td>
 248          *<tr><th scope="row">1.1</th>  <td>1</td>
 249          *<tr><th scope="row">1.0</th>  <td>1</td>
 250          *<tr><th scope="row">-1.0</th> <td>-1</td>
 251          *<tr><th scope="row">-1.1</th> <td>-1</td>
 252          *<tr><th scope="row">-1.6</th> <td>-2</td>
 253          *<tr><th scope="row">-2.5</th> <td>-3</td>
 254          *<tr><th scope="row">-5.5</th> <td>-6</td>
 255          *</tbody>
 256          *</table>
 257          */
 258     HALF_UP(BigDecimal.ROUND_HALF_UP),
 259 
 260         /**
 261          * Rounding mode to round towards {@literal "nearest neighbor"}
 262          * unless both neighbors are equidistant, in which case round
 263          * down.  Behaves as for {@code RoundingMode.UP} if the discarded
 264          * fraction is &gt; 0.5; otherwise, behaves as for
 265          * {@code RoundingMode.DOWN}.
 266          *
 267          *<p>Example:
 268          *<table class="striped">
 269          * <caption>Rounding mode HALF_DOWN Examples</caption>
 270          *<thead>
 271          *<tr style="vertical-align:top"><th scope="col">Input Number</th>
 272          *    <th scope="col">Input rounded to one digit<br> with {@code HALF_DOWN} rounding
 273          *</thead>
 274          *<tbody style="text-align:right">
 275          *<tr><th scope="row">5.5</th>  <td>5</td>
 276          *<tr><th scope="row">2.5</th>  <td>2</td>
 277          *<tr><th scope="row">1.6</th>  <td>2</td>
 278          *<tr><th scope="row">1.1</th>  <td>1</td>
 279          *<tr><th scope="row">1.0</th>  <td>1</td>
 280          *<tr><th scope="row">-1.0</th> <td>-1</td>
 281          *<tr><th scope="row">-1.1</th> <td>-1</td>
 282          *<tr><th scope="row">-1.6</th> <td>-2</td>
 283          *<tr><th scope="row">-2.5</th> <td>-2</td>
 284          *<tr><th scope="row">-5.5</th> <td>-5</td>
 285          *</tbody>
 286          *</table>
 287          */
 288     HALF_DOWN(BigDecimal.ROUND_HALF_DOWN),
 289 
 290         /**
 291          * Rounding mode to round towards the {@literal "nearest neighbor"}
 292          * unless both neighbors are equidistant, in which case, round
 293          * towards the even neighbor.  Behaves as for
 294          * {@code RoundingMode.HALF_UP} if the digit to the left of the
 295          * discarded fraction is odd; behaves as for
 296          * {@code RoundingMode.HALF_DOWN} if it's even.  Note that this
 297          * is the rounding mode that statistically minimizes cumulative
 298          * error when applied repeatedly over a sequence of calculations.
 299          * It is sometimes known as {@literal "Banker's rounding,"} and is
 300          * chiefly used in the USA.  This rounding mode is analogous to
 301          * the rounding policy used for {@code float} and {@code double}
 302          * arithmetic in Java.
 303          * This mode corresponds to the IEEE 754-2019 rounding
 304          * attribute roundTiesToEven.
 305          *
 306          *<p>Example:
 307          *<table class="striped">
 308          * <caption>Rounding mode HALF_EVEN Examples</caption>
 309          *<thead>
 310          *<tr style="vertical-align:top"><th scope="col">Input Number</th>
 311          *    <th scope="col">Input rounded to one digit<br> with {@code HALF_EVEN} rounding
 312          *</thead>
 313          *<tbody style="text-align:right">
 314          *<tr><th scope="row">5.5</th>  <td>6</td>
 315          *<tr><th scope="row">2.5</th>  <td>2</td>
 316          *<tr><th scope="row">1.6</th>  <td>2</td>
 317          *<tr><th scope="row">1.1</th>  <td>1</td>
 318          *<tr><th scope="row">1.0</th>  <td>1</td>
 319          *<tr><th scope="row">-1.0</th> <td>-1</td>
 320          *<tr><th scope="row">-1.1</th> <td>-1</td>
 321          *<tr><th scope="row">-1.6</th> <td>-2</td>
 322          *<tr><th scope="row">-2.5</th> <td>-2</td>
 323          *<tr><th scope="row">-5.5</th> <td>-6</td>
 324          *</tbody>
 325          *</table>
 326          */
 327     HALF_EVEN(BigDecimal.ROUND_HALF_EVEN),
 328 
 329         /**
 330          * Rounding mode to assert that the requested operation has an exact
 331          * result, hence no rounding is necessary.  If this rounding mode is
 332          * specified on an operation that yields an inexact result, an
 333          * {@code ArithmeticException} is thrown.
 334          *<p>Example:
 335          *<table class="striped">
 336          * <caption>Rounding mode UNNECESSARY Examples</caption>
 337          *<thead>
 338          *<tr style="vertical-align:top"><th scope="col">Input Number</th>
 339          *    <th scope="col">Input rounded to one digit<br> with {@code UNNECESSARY} rounding
 340          *</thead>
 341          *<tbody style="text-align:right">
 342          *<tr><th scope="row">5.5</th>  <td>throw {@code ArithmeticException}</td>
 343          *<tr><th scope="row">2.5</th>  <td>throw {@code ArithmeticException}</td>
 344          *<tr><th scope="row">1.6</th>  <td>throw {@code ArithmeticException}</td>
 345          *<tr><th scope="row">1.1</th>  <td>throw {@code ArithmeticException}</td>
 346          *<tr><th scope="row">1.0</th>  <td>1</td>
 347          *<tr><th scope="row">-1.0</th> <td>-1</td>
 348          *<tr><th scope="row">-1.1</th> <td>throw {@code ArithmeticException}</td>
 349          *<tr><th scope="row">-1.6</th> <td>throw {@code ArithmeticException}</td>
 350          *<tr><th scope="row">-2.5</th> <td>throw {@code ArithmeticException}</td>
 351          *<tr><th scope="row">-5.5</th> <td>throw {@code ArithmeticException}</td>
 352          *</tbody>
 353          *</table>
 354          */
 355     UNNECESSARY(BigDecimal.ROUND_UNNECESSARY);
 356 
 357     // Corresponding BigDecimal rounding constant
 358     final int oldMode;
 359 
 360     /**
 361      * Constructor
 362      *
 363      * @param oldMode The {@code BigDecimal} constant corresponding to
 364      *        this mode
 365      */
 366     private RoundingMode(int oldMode) {
 367         this.oldMode = oldMode;
 368     }
 369 
 370     /**
 371      * Returns the {@code RoundingMode} object corresponding to a
 372      * legacy integer rounding mode constant in {@link BigDecimal}.
 373      *
 374      * @param  rm legacy integer rounding mode to convert
 375      * @return {@code RoundingMode} corresponding to the given integer.
 376      * @throws IllegalArgumentException integer is out of range
 377      */
 378     public static RoundingMode valueOf(int rm) {
 379         switch(rm) {
 380 
 381         case BigDecimal.ROUND_UP:
 382             return UP;
 383 
 384         case BigDecimal.ROUND_DOWN:
 385             return DOWN;
 386 
 387         case BigDecimal.ROUND_CEILING:
 388             return CEILING;
 389 
 390         case BigDecimal.ROUND_FLOOR:
 391             return FLOOR;
 392 
 393         case BigDecimal.ROUND_HALF_UP:
 394             return HALF_UP;
 395 
 396         case BigDecimal.ROUND_HALF_DOWN:
 397             return HALF_DOWN;
 398 
 399         case BigDecimal.ROUND_HALF_EVEN:
 400             return HALF_EVEN;
 401 
 402         case BigDecimal.ROUND_UNNECESSARY:
 403             return UNNECESSARY;
 404 
 405         default:
 406             throw new IllegalArgumentException("argument out of range");
 407         }
 408     }
 409 }