calculators.rst 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. .. _reference.calculators:
  2. ===========
  3. Calculators
  4. ===========
  5. .. php:namespace:: Ramsey\Uuid\Math
  6. .. php:interface:: CalculatorInterface
  7. Provides functionality for performing mathematical calculations.
  8. .. php:method:: add($augend, ...$addends)
  9. :param Ramsey\\Uuid\\Type\\NumberInterface $augend: The first addend (the integer being added to)
  10. :param Ramsey\\Uuid\\Type\\NumberInterface ...$addends: The additional integers to a add to the augend
  11. :returns: The sum of all the parameters
  12. :returntype: Ramsey\\Uuid\\Type\\NumberInterface
  13. .. php:method:: subtract($minuend, ...$subtrahends)
  14. :param Ramsey\\Uuid\\Type\\NumberInterface $minuend: The integer being subtracted from
  15. :param Ramsey\\Uuid\\Type\\NumberInterface ...$subtrahends: The integers to subtract from the minuend
  16. :returns: The difference after subtracting all parameters
  17. :returntype: Ramsey\\Uuid\\Type\\NumberInterface
  18. .. php:method:: multiply($multiplicand, ...$multipliers)
  19. :param Ramsey\\Uuid\\Type\\NumberInterface $multiplicand: The integer to be multiplied
  20. :param Ramsey\\Uuid\\Type\\NumberInterface ...$multipliers: The factors by which to multiply the multiplicand
  21. :returns: The product of multiplying all the provided parameters
  22. :returntype: Ramsey\\Uuid\\Type\\NumberInterface
  23. .. php:method:: divide($roundingMode, $scale, $dividend, ...$divisors)
  24. :param int $roundingMode: The strategy for rounding the quotient; one of the :php:class:`Ramsey\\Uuid\\Math\\RoundingMode` constants
  25. :param int $scale: The scale to use for the operation
  26. :param Ramsey\\Uuid\\Type\\NumberInterface $dividend: The integer to be divided
  27. :param Ramsey\\Uuid\\Type\\NumberInterface ...$divisors: The integers to divide ``$dividend`` by, in the order in which the division operations should take place (left-to-right)
  28. :returns: The quotient of dividing the provided parameters left-to-right
  29. :returntype: Ramsey\\Uuid\\Type\\NumberInterface
  30. .. php:method:: fromBase($value, $base)
  31. Converts a value from an arbitrary base to a base-10 integer value.
  32. :param string $value: The value to convert
  33. :param int $base: The base to convert from (i.e., 2, 16, 32, etc.)
  34. :returns: The base-10 integer value of the converted value
  35. :returntype: Ramsey\\Uuid\\Type\\Integer
  36. .. php:method:: toBase($value, $base)
  37. Converts a base-10 integer value to an arbitrary base.
  38. :param Ramsey\\Uuid\\Type\\Integer $value: The integer value to convert
  39. :param int $base: The base to convert to (i.e., 2, 16, 32, etc.)
  40. :returns: The value represented in the specified base
  41. :returntype: ``string``
  42. .. php:method:: toHexadecimal($value)
  43. Converts an Integer instance to a Hexadecimal instance.
  44. :param Ramsey\\Uuid\\Type\\Integer $value: The Integer to convert to Hexadecimal
  45. :returntype: Ramsey\\Uuid\\Type\\Hexadecimal
  46. .. php:method:: toInteger($value)
  47. Converts a Hexadecimal instance to an Integer instance.
  48. :param Ramsey\\Uuid\\Type\\Hexadecimal $value: The Hexadecimal to convert to Integer
  49. :returntype: Ramsey\\Uuid\\Type\\Integer
  50. .. php:class:: RoundingMode
  51. .. php:const:: UNNECESSARY
  52. Asserts that the requested operation has an exact result, hence no
  53. rounding is necessary.
  54. .. php:const:: UP
  55. Rounds away from zero.
  56. Always increments the digit prior to a nonzero discarded fraction.
  57. Note that this rounding mode never decreases the magnitude of the
  58. calculated value.
  59. .. php:const:: DOWN
  60. Rounds towards zero.
  61. Never increments the digit prior to a discarded fraction (i.e.,
  62. truncates). Note that this rounding mode never increases the magnitude of
  63. the calculated value.
  64. .. php:const:: CEILING
  65. Rounds towards positive infinity.
  66. If the result is positive, behaves as for :php:const:`UP
  67. <Ramsey\\Uuid\\Math\\RoundingMode::UP>`; if negative, behaves as for
  68. :php:const:`DOWN <Ramsey\\Uuid\\Math\\RoundingMode::DOWN>`. Note that
  69. this rounding mode never decreases the calculated value.
  70. .. php:const:: FLOOR
  71. Rounds towards negative infinity.
  72. If the result is positive, behave as for :php:const:`DOWN
  73. <Ramsey\\Uuid\\Math\\RoundingMode::DOWN>`; if negative, behave as for
  74. :php:const:`UP <Ramsey\\Uuid\\Math\\RoundingMode::UP>`. Note that this
  75. rounding mode never increases the calculated value.
  76. .. php:const:: HALF_UP
  77. Rounds towards "nearest neighbor" unless both neighbors are equidistant,
  78. in which case round up.
  79. Behaves as for :php:const:`UP <Ramsey\\Uuid\\Math\\RoundingMode::UP>` if
  80. the discarded fraction is >= 0.5; otherwise, behaves as for
  81. :php:const:`DOWN <Ramsey\\Uuid\\Math\\RoundingMode::DOWN>`. Note that
  82. this is the rounding mode commonly taught at school.
  83. .. php:const:: HALF_DOWN
  84. Rounds towards "nearest neighbor" unless both neighbors are equidistant,
  85. in which case round down.
  86. Behaves as for :php:const:`UP <Ramsey\\Uuid\\Math\\RoundingMode::UP>` if
  87. the discarded fraction is > 0.5; otherwise, behaves as for
  88. :php:const:`DOWN <Ramsey\\Uuid\\Math\\RoundingMode::DOWN>`.
  89. .. php:const:: HALF_CEILING
  90. Rounds towards "nearest neighbor" unless both neighbors are equidistant,
  91. in which case round towards positive infinity.
  92. If the result is positive, behaves as for :php:const:`HALF_UP
  93. <Ramsey\\Uuid\\Math\\RoundingMode::HALF_UP>`; if negative, behaves as
  94. for :php:const:`HALF_DOWN <Ramsey\\Uuid\\Math\\RoundingMode::HALF_DOWN>`.
  95. .. php:const:: HALF_FLOOR
  96. Rounds towards "nearest neighbor" unless both neighbors are equidistant,
  97. in which case round towards negative infinity.
  98. If the result is positive, behaves as for :php:const:`HALF_DOWN
  99. <Ramsey\\Uuid\\Math\\RoundingMode::HALF_DOWN>`; if negative, behaves as
  100. for :php:const:`HALF_UP <Ramsey\\Uuid\\Math\\RoundingMode::HALF_UP>`.
  101. .. php:const:: HALF_EVEN
  102. Rounds towards the "nearest neighbor" unless both neighbors are
  103. equidistant, in which case rounds towards the even neighbor.
  104. Behaves as for :php:const:`HALF_UP <Ramsey\\Uuid\\Math\\RoundingMode::HALF_UP>`
  105. if the digit to the left of the discarded fraction is odd; behaves as
  106. for :php:const:`HALF_DOWN <Ramsey\\Uuid\\Math\\RoundingMode::HALF_DOWN>`
  107. if it's even.
  108. Note that this is the rounding mode that statistically minimizes
  109. cumulative error when applied repeatedly over a sequence of calculations.
  110. It is sometimes known as "Banker's rounding", and is chiefly used in the
  111. USA.