RoundingMode.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. enum RoundingMode
  14. {
  15. /**
  16. * Asserts that the requested operation has an exact result, hence no rounding is necessary.
  17. *
  18. * If this rounding mode is specified on an operation that yields a result that
  19. * cannot be represented at the requested scale, a RoundingNecessaryException is thrown.
  20. */
  21. case UNNECESSARY;
  22. /**
  23. * Rounds away from zero.
  24. *
  25. * Always increments the digit prior to a nonzero discarded fraction.
  26. * Note that this rounding mode never decreases the magnitude of the calculated value.
  27. */
  28. case UP;
  29. /**
  30. * Rounds towards zero.
  31. *
  32. * Never increments the digit prior to a discarded fraction (i.e., truncates).
  33. * Note that this rounding mode never increases the magnitude of the calculated value.
  34. */
  35. case DOWN;
  36. /**
  37. * Rounds towards positive infinity.
  38. *
  39. * If the result is positive, behaves as for UP; if negative, behaves as for DOWN.
  40. * Note that this rounding mode never decreases the calculated value.
  41. */
  42. case CEILING;
  43. /**
  44. * Rounds towards negative infinity.
  45. *
  46. * If the result is positive, behave as for DOWN; if negative, behave as for UP.
  47. * Note that this rounding mode never increases the calculated value.
  48. */
  49. case FLOOR;
  50. /**
  51. * Rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.
  52. *
  53. * Behaves as for UP if the discarded fraction is >= 0.5; otherwise, behaves as for DOWN.
  54. * Note that this is the rounding mode commonly taught at school.
  55. */
  56. case HALF_UP;
  57. /**
  58. * Rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case round down.
  59. *
  60. * Behaves as for UP if the discarded fraction is > 0.5; otherwise, behaves as for DOWN.
  61. */
  62. case HALF_DOWN;
  63. /**
  64. * Rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case round towards positive infinity.
  65. *
  66. * If the result is positive, behaves as for HALF_UP; if negative, behaves as for HALF_DOWN.
  67. */
  68. case HALF_CEILING;
  69. /**
  70. * Rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case round towards negative infinity.
  71. *
  72. * If the result is positive, behaves as for HALF_DOWN; if negative, behaves as for HALF_UP.
  73. */
  74. case HALF_FLOOR;
  75. /**
  76. * Rounds towards the "nearest neighbor" unless both neighbors are equidistant, in which case rounds towards the even neighbor.
  77. *
  78. * Behaves as for HALF_UP if the digit to the left of the discarded fraction is odd;
  79. * behaves as for HALF_DOWN if it's even.
  80. *
  81. * Note that this is the rounding mode that statistically minimizes
  82. * cumulative error when applied repeatedly over a sequence of calculations.
  83. * It is sometimes known as "Banker's rounding", and is chiefly used in the USA.
  84. */
  85. case HALF_EVEN;
  86. }