| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Brick\Math; |
| 6 | |
| 7 | /** |
| 8 | * Specifies a rounding behavior for numerical operations capable of discarding precision. |
| 9 | * |
| 10 | * Each rounding mode indicates how the least significant returned digit of a rounded result |
| 11 | * is to be calculated. If fewer digits are returned than the digits needed to represent the |
| 12 | * exact numerical result, the discarded digits will be referred to as the discarded fraction |
| 13 | * regardless the digits' contribution to the value of the number. In other words, considered |
| 14 | * as a numerical value, the discarded fraction could have an absolute value greater than one. |
| 15 | */ |
| 16 | enum RoundingMode |
| 17 | { |
| 18 | /** |
| 19 | * Asserts that the requested operation has an exact result, hence no rounding is necessary. |
| 20 | * |
| 21 | * If this rounding mode is specified on an operation that yields a result that |
| 22 | * cannot be represented at the requested scale, a RoundingNecessaryException is thrown. |
| 23 | */ |
| 24 | case UNNECESSARY; |
| 25 | |
| 26 | /** |
| 27 | * Rounds away from zero. |
| 28 | * |
| 29 | * Always increments the digit prior to a nonzero discarded fraction. |
| 30 | * Note that this rounding mode never decreases the magnitude of the calculated value. |
| 31 | */ |
| 32 | case UP; |
| 33 | |
| 34 | /** |
| 35 | * Rounds towards zero. |
| 36 | * |
| 37 | * Never increments the digit prior to a discarded fraction (i.e., truncates). |
| 38 | * Note that this rounding mode never increases the magnitude of the calculated value. |
| 39 | */ |
| 40 | case DOWN; |
| 41 | |
| 42 | /** |
| 43 | * Rounds towards positive infinity. |
| 44 | * |
| 45 | * If the result is positive, behaves as for UP; if negative, behaves as for DOWN. |
| 46 | * Note that this rounding mode never decreases the calculated value. |
| 47 | */ |
| 48 | case CEILING; |
| 49 | |
| 50 | /** |
| 51 | * Rounds towards negative infinity. |
| 52 | * |
| 53 | * If the result is positive, behave as for DOWN; if negative, behave as for UP. |
| 54 | * Note that this rounding mode never increases the calculated value. |
| 55 | */ |
| 56 | case FLOOR; |
| 57 | |
| 58 | /** |
| 59 | * Rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case round up. |
| 60 | * |
| 61 | * Behaves as for UP if the discarded fraction is >= 0.5; otherwise, behaves as for DOWN. |
| 62 | * Note that this is the rounding mode commonly taught at school. |
| 63 | */ |
| 64 | case HALF_UP; |
| 65 | |
| 66 | /** |
| 67 | * Rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case round down. |
| 68 | * |
| 69 | * Behaves as for UP if the discarded fraction is > 0.5; otherwise, behaves as for DOWN. |
| 70 | */ |
| 71 | case HALF_DOWN; |
| 72 | |
| 73 | /** |
| 74 | * Rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case round towards positive infinity. |
| 75 | * |
| 76 | * If the result is positive, behaves as for HALF_UP; if negative, behaves as for HALF_DOWN. |
| 77 | */ |
| 78 | case HALF_CEILING; |
| 79 | |
| 80 | /** |
| 81 | * Rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case round towards negative infinity. |
| 82 | * |
| 83 | * If the result is positive, behaves as for HALF_DOWN; if negative, behaves as for HALF_UP. |
| 84 | */ |
| 85 | case HALF_FLOOR; |
| 86 | |
| 87 | /** |
| 88 | * Rounds towards the "nearest neighbor" unless both neighbors are equidistant, in which case rounds towards the even neighbor. |
| 89 | * |
| 90 | * Behaves as for HALF_UP if the digit to the left of the discarded fraction is odd; |
| 91 | * behaves as for HALF_DOWN if it's even. |
| 92 | * |
| 93 | * Note that this is the rounding mode that statistically minimizes |
| 94 | * cumulative error when applied repeatedly over a sequence of calculations. |
| 95 | * It is sometimes known as "Banker's rounding", and is chiefly used in the USA. |
| 96 | */ |
| 97 | case HALF_EVEN; |
| 98 | } |