Bc.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /*
  3. * This file is part of Hashids.
  4. *
  5. * (c) Ivan Akimov <ivan@barreleye.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Hashids\Math;
  11. /**
  12. * This is the Bc math class.
  13. *
  14. * @author Vincent Klaiber <hello@doubledip.se>
  15. * @author Jakub Kramarz <lenwe@lenwe.net>
  16. * @author Johnson Page <jwpage@gmail.com>
  17. */
  18. class Bc implements MathInterface
  19. {
  20. /**
  21. * Add two arbitrary-length integers.
  22. *
  23. * @param string $a
  24. * @param string $b
  25. *
  26. * @return string
  27. */
  28. public function add($a, $b)
  29. {
  30. return bcadd($a, $b, 0);
  31. }
  32. /**
  33. * Multiply two arbitrary-length integers.
  34. *
  35. * @param string $a
  36. * @param string $b
  37. *
  38. * @return string
  39. */
  40. public function multiply($a, $b)
  41. {
  42. return bcmul($a, $b, 0);
  43. }
  44. /**
  45. * Divide two arbitrary-length integers.
  46. *
  47. * @param string $a
  48. * @param string $b
  49. *
  50. * @return string
  51. */
  52. public function divide($a, $b)
  53. {
  54. return bcdiv($a, $b, 0);
  55. }
  56. /**
  57. * Compute arbitrary-length integer modulo.
  58. *
  59. * @param string $n
  60. * @param string $d
  61. *
  62. * @return string
  63. */
  64. public function mod($n, $d)
  65. {
  66. return bcmod($n, $d);
  67. }
  68. /**
  69. * Compares two arbitrary-length integers.
  70. *
  71. * @param string $a
  72. * @param string $b
  73. *
  74. * @return bool
  75. */
  76. public function greaterThan($a, $b)
  77. {
  78. return bccomp($a, $b, 0) > 0;
  79. }
  80. /**
  81. * Converts arbitrary-length integer to PHP integer.
  82. *
  83. * @param string $a
  84. *
  85. * @return int
  86. */
  87. public function intval($a)
  88. {
  89. return intval($a);
  90. }
  91. /**
  92. * Converts arbitrary-length integer to PHP string.
  93. *
  94. * @param string $a
  95. *
  96. * @return string
  97. */
  98. public function strval($a)
  99. {
  100. return $a;
  101. }
  102. /**
  103. * Converts PHP integer to arbitrary-length integer.
  104. *
  105. * @param int $a
  106. *
  107. * @return string
  108. */
  109. public function get($a)
  110. {
  111. return $a;
  112. }
  113. }