| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace Dudlewebs\WPMCS\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 |
*/ |
| 15 |
final class RoundingMode |
| 16 |
{ |
| 17 |
/** |
| 18 |
* Private constructor. This class is not instantiable. |
| 19 |
* |
| 20 |
* @codeCoverageIgnore |
| 21 |
*/ |
| 22 |
private function __construct() |
| 23 |
{ |
| 24 |
} |
| 25 |
/** |
| 26 |
* Asserts that the requested operation has an exact result, hence no rounding is necessary. |
| 27 |
* |
| 28 |
* If this rounding mode is specified on an operation that yields a result that |
| 29 |
* cannot be represented at the requested scale, a RoundingNecessaryException is thrown. |
| 30 |
*/ |
| 31 |
public const UNNECESSARY = 0; |
| 32 |
/** |
| 33 |
* Rounds away from zero. |
| 34 |
* |
| 35 |
* Always increments the digit prior to a nonzero discarded fraction. |
| 36 |
* Note that this rounding mode never decreases the magnitude of the calculated value. |
| 37 |
*/ |
| 38 |
public const UP = 1; |
| 39 |
/** |
| 40 |
* Rounds towards zero. |
| 41 |
* |
| 42 |
* Never increments the digit prior to a discarded fraction (i.e., truncates). |
| 43 |
* Note that this rounding mode never increases the magnitude of the calculated value. |
| 44 |
*/ |
| 45 |
public const DOWN = 2; |
| 46 |
/** |
| 47 |
* Rounds towards positive infinity. |
| 48 |
* |
| 49 |
* If the result is positive, behaves as for UP; if negative, behaves as for DOWN. |
| 50 |
* Note that this rounding mode never decreases the calculated value. |
| 51 |
*/ |
| 52 |
public const CEILING = 3; |
| 53 |
/** |
| 54 |
* Rounds towards negative infinity. |
| 55 |
* |
| 56 |
* If the result is positive, behave as for DOWN; if negative, behave as for UP. |
| 57 |
* Note that this rounding mode never increases the calculated value. |
| 58 |
*/ |
| 59 |
public const FLOOR = 4; |
| 60 |
/** |
| 61 |
* Rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case round up. |
| 62 |
* |
| 63 |
* Behaves as for UP if the discarded fraction is >= 0.5; otherwise, behaves as for DOWN. |
| 64 |
* Note that this is the rounding mode commonly taught at school. |
| 65 |
*/ |
| 66 |
public const HALF_UP = 5; |
| 67 |
/** |
| 68 |
* Rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case round down. |
| 69 |
* |
| 70 |
* Behaves as for UP if the discarded fraction is > 0.5; otherwise, behaves as for DOWN. |
| 71 |
*/ |
| 72 |
public const HALF_DOWN = 6; |
| 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 |
public const HALF_CEILING = 7; |
| 79 |
/** |
| 80 |
* Rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case round towards negative infinity. |
| 81 |
* |
| 82 |
* If the result is positive, behaves as for HALF_DOWN; if negative, behaves as for HALF_UP. |
| 83 |
*/ |
| 84 |
public const HALF_FLOOR = 8; |
| 85 |
/** |
| 86 |
* Rounds towards the "nearest neighbor" unless both neighbors are equidistant, in which case rounds towards the even neighbor. |
| 87 |
* |
| 88 |
* Behaves as for HALF_UP if the digit to the left of the discarded fraction is odd; |
| 89 |
* behaves as for HALF_DOWN if it's even. |
| 90 |
* |
| 91 |
* Note that this is the rounding mode that statistically minimizes |
| 92 |
* cumulative error when applied repeatedly over a sequence of calculations. |
| 93 |
* It is sometimes known as "Banker's rounding", and is chiefly used in the USA. |
| 94 |
*/ |
| 95 |
public const HALF_EVEN = 9; |
| 96 |
} |
| 97 |
|