1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHP\Math\BigInteger; |
4
|
|
|
|
5
|
|
|
use GMP; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use RuntimeException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A big integer value. |
11
|
|
|
*/ |
12
|
|
|
class BigInteger |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The value represented as a string. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $value; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* A flag that indicates whether or not the state of this object can be changed. |
23
|
|
|
* |
24
|
|
|
* @var bool |
25
|
|
|
*/ |
26
|
|
|
private $mutable; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Initializes a new instance of this class. |
30
|
|
|
* |
31
|
|
|
* @param string|int|BigInteger $value The value to set. |
32
|
|
|
* @param bool $mutable Whether or not the state of this object can be changed. |
33
|
|
|
*/ |
34
|
67 |
|
public function __construct($value = 0, $mutable = true) |
35
|
|
|
{ |
36
|
67 |
|
$this->internalSetValue($value); |
37
|
|
|
|
38
|
66 |
|
$this->mutable = $mutable; |
39
|
66 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Gets the value of the big integer. |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
52 |
|
public function getValue() |
47
|
|
|
{ |
48
|
52 |
|
return gmp_strval($this->value); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Sets the value. |
53
|
|
|
* |
54
|
|
|
* @param string $value The value to set. |
55
|
|
|
* @return BigInteger |
56
|
|
|
*/ |
57
|
5 |
|
public function setValue($value) |
58
|
|
|
{ |
59
|
5 |
|
if (!$this->isMutable()) { |
60
|
1 |
|
throw new RuntimeException('Cannot set the value since the number is immutable.'); |
61
|
|
|
} |
62
|
|
|
|
63
|
4 |
|
return $this->internalSetValue($value); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Converts the value to an absolute number. |
68
|
|
|
* |
69
|
|
|
* @return BigInteger |
70
|
|
|
*/ |
71
|
5 |
|
public function abs() |
72
|
|
|
{ |
73
|
5 |
|
$value = gmp_abs($this->value); |
74
|
|
|
|
75
|
5 |
|
return $this->assignValue($value); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Adds the given value to this value. |
80
|
|
|
* |
81
|
|
|
* @param string $value The value to add. |
82
|
|
|
* @return BigInteger |
83
|
|
|
*/ |
84
|
6 |
|
public function add($value) |
85
|
|
|
{ |
86
|
6 |
|
$this->checkValue($value); |
87
|
|
|
|
88
|
5 |
|
$gmp = gmp_init($value); |
89
|
|
|
|
90
|
5 |
|
$calculatedValue = gmp_add($this->value, $gmp); |
91
|
|
|
|
92
|
5 |
|
return $this->assignValue($calculatedValue); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Compares this number and the given number. |
97
|
|
|
* |
98
|
|
|
* @return int Returns -1 is the number is less than this number. 0 if equal and 1 when greater. |
99
|
|
|
*/ |
100
|
4 |
|
public function cmp($value) |
101
|
|
|
{ |
102
|
4 |
|
$this->checkValue($value); |
103
|
|
|
|
104
|
3 |
|
$result = gmp_cmp($this->value, $value); |
105
|
|
|
|
106
|
|
|
// It could happpen that gmp_cmp returns a value greater than one (e.g. gmp_cmp('123', '-123')). That's why |
107
|
|
|
// we do an additional check to make sure to return the correct value. |
108
|
|
|
|
109
|
3 |
|
if ($result > 0) { |
110
|
1 |
|
return 1; |
111
|
2 |
|
} elseif ($result < 0) { |
112
|
1 |
|
return -1; |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
return 0; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Divides this value by the given value. |
120
|
|
|
* |
121
|
|
|
* @param string $value The value to divide by. |
122
|
|
|
* @return BigInteger |
123
|
|
|
*/ |
124
|
9 |
|
public function divide($value) |
125
|
|
|
{ |
126
|
9 |
|
$this->checkValue($value); |
127
|
|
|
|
128
|
8 |
|
$gmp = gmp_init($value); |
129
|
|
|
|
130
|
8 |
|
$calculatedValue = gmp_div($this->value, $gmp, false); |
131
|
|
|
|
132
|
8 |
|
return $this->assignValue($calculatedValue); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Calculates factorial of this value. |
137
|
|
|
* |
138
|
|
|
* @return BigInteger |
139
|
|
|
*/ |
140
|
3 |
|
public function factorial() |
141
|
|
|
{ |
142
|
3 |
|
$calculatedValue = gmp_fact($this->getValue()); |
143
|
|
|
|
144
|
3 |
|
return $this->assignValue($calculatedValue); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Performs a modulo operation with the given number. |
149
|
|
|
* |
150
|
|
|
* @param string $value The value to perform a modulo operation with. |
151
|
|
|
* @return BigInteger |
152
|
|
|
*/ |
153
|
6 |
|
public function mod($value) |
154
|
|
|
{ |
155
|
6 |
|
$this->checkValue($value); |
156
|
|
|
|
157
|
5 |
|
$gmp = gmp_init($value); |
158
|
|
|
|
159
|
5 |
|
$calculatedValue = gmp_mod($this->value, $gmp); |
160
|
|
|
|
161
|
5 |
|
return $this->assignValue($calculatedValue); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Multiplies the given value with this value. |
166
|
|
|
* |
167
|
|
|
* @param string $value The value to multiply with. |
168
|
|
|
* @return BigInteger |
169
|
|
|
*/ |
170
|
6 |
|
public function multiply($value) |
171
|
|
|
{ |
172
|
6 |
|
$this->checkValue($value); |
173
|
|
|
|
174
|
5 |
|
$gmp = gmp_init($value); |
175
|
|
|
|
176
|
5 |
|
$calculatedValue = gmp_mul($this->value, $gmp); |
177
|
|
|
|
178
|
5 |
|
return $this->assignValue($calculatedValue); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Negates the value. |
183
|
|
|
* |
184
|
|
|
* @return BigInteger |
185
|
|
|
*/ |
186
|
3 |
|
public function negate() |
187
|
|
|
{ |
188
|
3 |
|
$calculatedValue = gmp_neg($this->value); |
189
|
|
|
|
190
|
3 |
|
return $this->assignValue($calculatedValue); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Performs a power operation with the given number. |
195
|
|
|
* |
196
|
|
|
* @param string $value The value to perform a power operation with. |
197
|
|
|
* @return BigInteger |
198
|
|
|
*/ |
199
|
6 |
|
public function pow($value) |
200
|
|
|
{ |
201
|
6 |
|
$this->checkValue($value); |
202
|
|
|
|
203
|
5 |
|
$calculatedValue = gmp_pow($this->value, (string)$value); |
204
|
|
|
|
205
|
5 |
|
return $this->assignValue($calculatedValue); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Subtracts the given value from this value. |
210
|
|
|
* |
211
|
|
|
* @param string $value The value to subtract. |
212
|
|
|
* @return BigInteger |
213
|
|
|
*/ |
214
|
6 |
|
public function subtract($value) |
215
|
|
|
{ |
216
|
6 |
|
$this->checkValue($value); |
217
|
|
|
|
218
|
5 |
|
$gmp = gmp_init($value); |
219
|
|
|
|
220
|
5 |
|
$calculatedValue = gmp_sub($this->value, $gmp); |
221
|
|
|
|
222
|
5 |
|
return $this->assignValue($calculatedValue); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Checks if this object is mutable. |
227
|
|
|
* |
228
|
|
|
* @return bool |
229
|
|
|
*/ |
230
|
52 |
|
public function isMutable() |
231
|
|
|
{ |
232
|
52 |
|
return $this->mutable; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Converts this class to a string. |
237
|
|
|
* |
238
|
|
|
* @return string |
239
|
|
|
*/ |
240
|
11 |
|
public function toString() |
241
|
|
|
{ |
242
|
11 |
|
return $this->getValue(); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Converts this class to a string. |
247
|
|
|
* |
248
|
|
|
* @return string |
249
|
|
|
*/ |
250
|
11 |
|
public function __toString() |
251
|
|
|
{ |
252
|
11 |
|
return $this->toString(); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* A helper method to assign the given value. |
257
|
|
|
* |
258
|
|
|
* @param int|string|BigInteger $value The value to assign. |
259
|
|
|
* @return BigInteger |
260
|
|
|
*/ |
261
|
44 |
|
private function assignValue($value) |
262
|
|
|
{ |
263
|
44 |
|
$result = null; |
|
|
|
|
264
|
|
|
|
265
|
44 |
|
if ($this->isMutable()) { |
266
|
35 |
|
$result = $this->internalSetValue($value); |
267
|
35 |
|
} else { |
268
|
9 |
|
$result = new BigInteger($value, false); |
269
|
|
|
} |
270
|
|
|
|
271
|
44 |
|
return $result; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* A helper method to set the value on this class. |
276
|
|
|
* |
277
|
|
|
* @param int|string|BigInteger $value The value to assign. |
278
|
|
|
* @return BigInteger |
279
|
|
|
*/ |
280
|
67 |
|
private function internalSetValue($value) |
281
|
|
|
{ |
282
|
67 |
|
$this->checkValue($value); |
283
|
|
|
|
284
|
66 |
|
$this->value = gmp_init($value); |
285
|
|
|
|
286
|
66 |
|
return $this; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Checks if the given value is valid. |
291
|
|
|
* |
292
|
|
|
* @param int|string $value The value to check. |
293
|
|
|
* @throws InvalidArgumentException Thrown when the value is invalid. |
294
|
|
|
*/ |
295
|
67 |
|
private function checkValue(&$value) |
296
|
|
|
{ |
297
|
67 |
|
if ($value instanceof GMP || is_resource($value)) { |
|
|
|
|
298
|
44 |
|
$value = gmp_strval($value); |
299
|
44 |
|
} else { |
300
|
67 |
|
$value = (string)$value; |
301
|
|
|
} |
302
|
|
|
|
303
|
67 |
|
if (!preg_match('/^(-|\+)?[0-9]+$/', $value)) { |
304
|
8 |
|
throw new InvalidArgumentException('Invalid number provided: ' . $value); |
305
|
|
|
} |
306
|
66 |
|
} |
307
|
|
|
} |
308
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.