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