RoundingMode.php 3.8 KB

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