Gmp.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Copyright (c) Ivan Akimov.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @see https://github.com/vinkla/hashids
  9. */
  10. namespace app\utils\Hashids\Math;
  11. class Gmp implements MathInterface
  12. {
  13. /**
  14. * Add two arbitrary-length integers.
  15. *
  16. * @param string $a
  17. * @param string $b
  18. *
  19. * @return string
  20. */
  21. public function add($a, $b)
  22. {
  23. return \gmp_add($a, $b);
  24. }
  25. /**
  26. * Multiply two arbitrary-length integers.
  27. *
  28. * @param string $a
  29. * @param string $b
  30. *
  31. * @return string
  32. */
  33. public function multiply($a, $b)
  34. {
  35. return \gmp_mul($a, $b);
  36. }
  37. /**
  38. * Divide two arbitrary-length integers.
  39. *
  40. * @param string $a
  41. * @param string $b
  42. *
  43. * @return string
  44. */
  45. public function divide($a, $b)
  46. {
  47. return \gmp_div_q($a, $b);
  48. }
  49. /**
  50. * Compute arbitrary-length integer modulo.
  51. *
  52. * @param string $n
  53. * @param string $d
  54. *
  55. * @return string
  56. */
  57. public function mod($n, $d)
  58. {
  59. return \gmp_mod($n, $d);
  60. }
  61. /**
  62. * Compares two arbitrary-length integers.
  63. *
  64. * @param string $a
  65. * @param string $b
  66. *
  67. * @return bool
  68. */
  69. public function greaterThan($a, $b)
  70. {
  71. return \gmp_cmp($a, $b) > 0;
  72. }
  73. /**
  74. * Converts arbitrary-length integer to PHP integer.
  75. *
  76. * @param string $a
  77. *
  78. * @return int
  79. */
  80. public function intval($a)
  81. {
  82. return \gmp_intval($a);
  83. }
  84. /**
  85. * Converts arbitrary-length integer to PHP string.
  86. *
  87. * @param string $a
  88. *
  89. * @return string
  90. */
  91. public function strval($a)
  92. {
  93. return \gmp_strval($a);
  94. }
  95. /**
  96. * Converts PHP integer to arbitrary-length integer.
  97. *
  98. * @param int $a
  99. *
  100. * @return string
  101. */
  102. public function get($a)
  103. {
  104. return \gmp_init($a);
  105. }
  106. }