1 | <?php |
||
8 | class Gmp implements MathLibraryInterface |
||
9 | { |
||
10 | /** |
||
11 | * Add two arbitrary precision numbers. |
||
12 | * |
||
13 | * @param string $leftOperand |
||
14 | * @param string $rightOperand |
||
15 | * @param int $precision |
||
16 | * |
||
17 | * @return string |
||
18 | */ |
||
19 | 1 | public function add(string $leftOperand, string $rightOperand, int $precision = 0): string |
|
20 | { |
||
21 | 1 | return \gmp_strval(\gmp_add($leftOperand, $rightOperand)); |
|
22 | } |
||
23 | |||
24 | /** |
||
25 | * Subtract two arbitrary precision numbers. |
||
26 | * |
||
27 | * @param string $leftOperand |
||
28 | * @param string $rightOperand |
||
29 | * @param int $precision |
||
30 | * |
||
31 | * @return string |
||
32 | */ |
||
33 | 1 | public function subtract(string $leftOperand, string $rightOperand, int $precision = 0): string |
|
34 | { |
||
35 | 1 | return \gmp_strval(\gmp_sub($leftOperand, $rightOperand)); |
|
36 | } |
||
37 | |||
38 | /** |
||
39 | * Multiply two arbitrary precision numbers. |
||
40 | * |
||
41 | * @param string $leftOperand |
||
42 | * @param string $rightOperand |
||
43 | * @param int $precision |
||
44 | * |
||
45 | * @return string |
||
46 | */ |
||
47 | 1 | public function multiply(string $leftOperand, string $rightOperand, int $precision = 0): string |
|
48 | { |
||
49 | 1 | return \gmp_strval(\gmp_mul($leftOperand, $rightOperand)); |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * Divide two arbitrary precision numbers. |
||
54 | * |
||
55 | * @param string $leftOperand |
||
56 | * @param string $rightOperand |
||
57 | * @param int $precision |
||
58 | * |
||
59 | * @return string |
||
60 | */ |
||
61 | 1 | public function divide(string $leftOperand, string $rightOperand, int $precision = 0): string |
|
62 | { |
||
63 | 1 | return \gmp_strval(\gmp_div($leftOperand, $rightOperand)); |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * Compare two arbitrary precision numbers. |
||
68 | * |
||
69 | * @param string $leftOperand |
||
70 | * @param string $rightOperand |
||
71 | * @param int $precision |
||
72 | * |
||
73 | * @return string |
||
74 | */ |
||
75 | 1 | public function compare(string $leftOperand, string $rightOperand, int $precision = 0): string |
|
76 | { |
||
77 | 1 | return \gmp_strval(\gmp_cmp($leftOperand, $rightOperand)); |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * Get modulus of an arbitrary precision number. |
||
82 | * |
||
83 | * @param string $operand |
||
84 | * @param string $modulus |
||
85 | * @param int $precision |
||
86 | * |
||
87 | * @return string |
||
88 | */ |
||
89 | 1 | public function modulus(string $operand, string $modulus, int $precision = 0): string |
|
90 | { |
||
91 | 1 | return \gmp_strval(\gmp_mod($operand, $modulus)); |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * Raise an arbitrary precision number to another. |
||
96 | * |
||
97 | * @param string $leftOperand |
||
98 | * @param string $rightOperand |
||
99 | * @param int $precision |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | 1 | public function power(string $leftOperand, string $rightOperand, int $precision = 0): string |
|
104 | { |
||
105 | 1 | return \gmp_strval(\gmp_pow($leftOperand, (int) $rightOperand)); |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * Get the square root of an arbitrary precision number. |
||
110 | * |
||
111 | * @param string $operand |
||
112 | * @param int $precision |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | 1 | public function squareRoot(string $operand, int $precision = 0): string |
|
117 | { |
||
118 | 1 | return \gmp_strval(\gmp_sqrt($operand)); |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * Returns absolute value of operand. |
||
123 | * |
||
124 | * @param string $operand |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | 3 | public function absolute(string $operand): string |
|
129 | { |
||
130 | 3 | return \gmp_strval(\gmp_abs($operand)); |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * Negates a number. Opposite of absolute/abs. |
||
135 | * |
||
136 | * @param string $operand |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | 3 | public function negate(string $operand): string |
|
141 | { |
||
142 | 3 | return \gmp_strval(\gmp_neg($operand)); |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * Returns the factorial of operand. |
||
147 | * |
||
148 | * @param string $operand |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | 4 | public function factorial(string $operand): string |
|
153 | { |
||
154 | 4 | return \gmp_strval(\gmp_fact($operand)); |
|
155 | } |
||
156 | |||
157 | /** |
||
158 | * Greatest common divisor. |
||
159 | * |
||
160 | * @param string $leftOperand |
||
161 | * @param string $rightOperand |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | 4 | public function gcd(string $leftOperand, string $rightOperand): string |
|
166 | { |
||
167 | 4 | return \gmp_strval(\gmp_gcd($leftOperand, $rightOperand)); |
|
168 | } |
||
169 | |||
170 | /** |
||
171 | * Calculates to the nth root. |
||
172 | * |
||
173 | * @param string $operand |
||
174 | * @param int $nth |
||
175 | * |
||
176 | * @return string |
||
177 | */ |
||
178 | 4 | public function root(string $operand, int $nth): string |
|
179 | { |
||
180 | 4 | $operand = \gmp_init($operand, 10); |
|
181 | |||
182 | 4 | return \gmp_strval(\gmp_root($operand, $nth)); |
|
183 | } |
||
184 | |||
185 | /** |
||
186 | * Gets the next prime after operand. |
||
187 | * |
||
188 | * @param string $operand |
||
189 | * |
||
190 | * @return string |
||
191 | */ |
||
192 | 4 | public function nextPrime(string $operand): string |
|
193 | { |
||
194 | 4 | return \gmp_strval(\gmp_nextprime($operand)); |
|
195 | } |
||
196 | |||
197 | /** |
||
198 | * @param string $operand |
||
199 | * @param int $reps |
||
200 | * |
||
201 | * @return bool |
||
202 | */ |
||
203 | 4 | public function isPrime(string $operand, int $reps = 10): bool |
|
204 | { |
||
205 | 4 | return 0 < \gmp_prob_prime($operand, $reps); |
|
206 | } |
||
207 | |||
208 | /** |
||
209 | * Checks if operand is perfect square. |
||
210 | * |
||
211 | * @param string $operand |
||
212 | * @param int $precision |
||
213 | * |
||
214 | * @return bool |
||
215 | */ |
||
216 | 4 | public function isPerfectSquare(string $operand, int $precision = 0): bool |
|
217 | { |
||
218 | 4 | return \gmp_perfect_square($operand); |
|
219 | } |
||
220 | |||
221 | /** |
||
222 | * The gamma function. |
||
223 | * |
||
224 | * @param string $operand |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | 1 | public function gamma(string $operand): string |
|
229 | { |
||
230 | 1 | throw $this->createInvalidLibraryException(__FUNCTION__); |
|
231 | } |
||
232 | |||
233 | /** |
||
234 | * The log-gamma function. |
||
235 | * |
||
236 | * @param string $operand |
||
237 | * |
||
238 | * @return string |
||
239 | */ |
||
240 | 1 | public function logGamma(string $operand): string |
|
241 | { |
||
242 | 1 | throw $this->createInvalidLibraryException(__FUNCTION__); |
|
243 | } |
||
244 | |||
245 | /** |
||
246 | * @param string $type |
||
247 | * |
||
248 | * @return bool |
||
249 | */ |
||
250 | 27 | public function supportsOperationType(string $type): bool |
|
251 | { |
||
252 | //Supports only int. |
||
253 | 27 | return $type !== self::TYPE_FLOAT; |
|
254 | } |
||
255 | |||
256 | /** |
||
257 | * @return bool |
||
258 | */ |
||
259 | 27 | public function isEnabled(): bool |
|
260 | { |
||
261 | 27 | return extension_loaded('gmp'); |
|
262 | } |
||
263 | |||
264 | /** |
||
265 | * @param string $methodName |
||
266 | * |
||
267 | * @return \RuntimeException |
||
268 | */ |
||
269 | 2 | private function createInvalidLibraryException(string $methodName): \RuntimeException |
|
275 | } |
||
276 |