1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHP\Math\BigNumber; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use RuntimeException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* A big number value. |
10
|
|
|
*/ |
11
|
|
|
class BigNumber |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The value represented as a string. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $value; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The scale that is used. |
22
|
|
|
* |
23
|
|
|
* @var int |
24
|
|
|
*/ |
25
|
|
|
private $scale; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* A flag that indicates whether or not the state of this object can be changed. |
29
|
|
|
* |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
private $mutable; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Initializes a new instance of this class. |
36
|
|
|
* |
37
|
|
|
* @param string|int|self $value The value to set. |
38
|
|
|
* @param int $scale The scale to use. |
39
|
|
|
* @param bool $mutable Whether or not the state of this object can be changed. |
40
|
|
|
*/ |
41
|
102 |
|
public function __construct($value = 0, $scale = 10, $mutable = true) |
42
|
|
|
{ |
43
|
102 |
|
$this->scale = $scale; |
44
|
102 |
|
$this->mutable = $mutable; |
45
|
|
|
|
46
|
102 |
|
$this->internalSetValue($value); |
47
|
101 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Gets the value of the big number. |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
85 |
|
public function getValue() |
55
|
|
|
{ |
56
|
|
|
// Make sure we return the value with the correct scale: |
57
|
85 |
|
return bcadd(0, $this->value, $this->getScale()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Sets the value. |
62
|
|
|
* |
63
|
|
|
* @param string $value |
64
|
|
|
*/ |
65
|
5 |
|
public function setValue($value) |
66
|
|
|
{ |
67
|
5 |
|
if (!$this->isMutable()) { |
68
|
1 |
|
throw new RuntimeException('Cannot set the value since the number is immutable.'); |
69
|
|
|
} |
70
|
|
|
|
71
|
4 |
|
return $this->internalSetValue($value); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Gets the scale of this number. |
76
|
|
|
* |
77
|
|
|
* @return int |
78
|
|
|
*/ |
79
|
101 |
|
public function getScale() |
80
|
|
|
{ |
81
|
101 |
|
return $this->scale; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Sets the scale of this number. |
86
|
|
|
* |
87
|
|
|
* @param int $scale The scale to set. |
88
|
|
|
* @return BigNumber |
89
|
|
|
*/ |
90
|
8 |
|
public function setScale($scale) |
91
|
|
|
{ |
92
|
8 |
|
if (!$this->isMutable()) { |
93
|
1 |
|
throw new RuntimeException(sprintf( |
94
|
1 |
|
'Cannot set the scale to "%s" since the numbere is immutable.', |
95
|
|
|
$scale |
96
|
1 |
|
)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// Convert to a string to make sure that the __toString method is called for objects. |
100
|
7 |
|
$scaleValue = (string)$scale; |
101
|
7 |
|
if (!ctype_digit($scaleValue)) { |
102
|
3 |
|
throw new InvalidArgumentException(sprintf( |
103
|
3 |
|
'Cannot set the scale to "%s". Invalid value.', |
104
|
|
|
$scaleValue |
105
|
3 |
|
)); |
106
|
|
|
} |
107
|
|
|
|
108
|
4 |
|
$this->scale = (int)$scaleValue; |
109
|
|
|
|
110
|
4 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Converts the number to an absolute value. |
115
|
|
|
*/ |
116
|
6 |
|
public function abs() |
117
|
|
|
{ |
118
|
6 |
|
$newValue = ltrim($this->getValue(), '-'); |
119
|
|
|
|
120
|
6 |
|
return $this->assignValue($newValue); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Adds the given value to this value. |
125
|
|
|
* |
126
|
|
|
* @param float|int|string|BigNumber $value The value to add. |
127
|
|
|
* @return BigNumber |
128
|
|
|
*/ |
129
|
8 |
|
public function add($value) |
130
|
|
|
{ |
131
|
8 |
|
if (!$value instanceof self) { |
132
|
7 |
|
$value = new self($value, $this->getScale(), false); |
133
|
6 |
|
} |
134
|
|
|
|
135
|
7 |
|
$newValue = bcadd($this->getValue(), $value->getValue(), $this->getScale()); |
136
|
|
|
|
137
|
7 |
|
return $this->assignValue($newValue); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Divides this value by the given value. |
142
|
|
|
* |
143
|
|
|
* @param float|int|string|BigNumber $value The value to divide by. |
144
|
|
|
* @return BigNumber |
145
|
|
|
*/ |
146
|
9 |
|
public function divide($value) |
147
|
|
|
{ |
148
|
9 |
|
if (!$value instanceof self) { |
149
|
8 |
|
$value = new self($value, $this->getScale(), false); |
150
|
7 |
|
} |
151
|
|
|
|
152
|
8 |
|
$rawValue = $value->getValue(); |
153
|
8 |
|
if ($rawValue == 0) { |
154
|
1 |
|
throw new InvalidArgumentException('Cannot divide by zero.'); |
155
|
|
|
} |
156
|
|
|
|
157
|
7 |
|
$newValue = bcdiv($this->getValue(), $rawValue, $this->getScale()); |
158
|
|
|
|
159
|
7 |
|
return $this->assignValue($newValue); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Multiplies the given value with this value. |
164
|
|
|
* |
165
|
|
|
* @param float|int|string|BigNumber $value The value to multiply with. |
166
|
|
|
* @return BigNumber |
167
|
|
|
*/ |
168
|
12 |
|
public function multiply($value) |
169
|
|
|
{ |
170
|
12 |
|
if (!$value instanceof self) { |
171
|
7 |
|
$value = new self($value, $this->getScale(), false); |
172
|
6 |
|
} |
173
|
|
|
|
174
|
11 |
|
$newValue = bcmul($this->getValue(), $value->getValue(), $this->getScale()); |
175
|
|
|
|
176
|
11 |
|
return $this->assignValue($newValue); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Performs a modulo operation with the given number. |
181
|
|
|
* |
182
|
|
|
* @param float|int|string|BigNumber $value The value to perform a modulo operation with. |
183
|
|
|
* @return BigNumber |
184
|
|
|
*/ |
185
|
9 |
|
public function mod($value) |
186
|
|
|
{ |
187
|
9 |
|
$bigNumber = new self($value, 0, false); |
188
|
|
|
|
189
|
7 |
|
$newValue = bcmod($this->getValue(), $bigNumber->getValue()); |
190
|
|
|
|
191
|
7 |
|
return $this->assignValue($newValue); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Performs a power operation with the given number. |
196
|
|
|
* |
197
|
|
|
* @param int|string $value The value to perform a power operation with. Should be an integer (or an int-string). |
198
|
|
|
* @return BigNumber |
199
|
|
|
*/ |
200
|
8 |
|
public function pow($value) |
201
|
|
|
{ |
202
|
8 |
|
if (!is_int($value) && !ctype_digit($value)) { |
203
|
4 |
|
throw new InvalidArgumentException(sprintf( |
204
|
4 |
|
'Invalid exponent "%s" provided. Only integers are allowed.', |
205
|
|
|
$value |
206
|
4 |
|
)); |
207
|
|
|
} |
208
|
|
|
|
209
|
4 |
|
$newValue = bcpow($this->getValue(), $value, $this->getScale()); |
210
|
|
|
|
211
|
4 |
|
return $this->assignValue($newValue); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Performs a square root operation with the given number. |
216
|
|
|
* |
217
|
|
|
* @return BigNumber |
218
|
|
|
*/ |
219
|
3 |
|
public function sqrt() |
220
|
|
|
{ |
221
|
3 |
|
$newValue = bcsqrt($this->getValue(), $this->getScale()); |
222
|
|
|
|
223
|
3 |
|
return $this->assignValue($newValue); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Subtracts the given value from this value. |
228
|
|
|
* |
229
|
|
|
* @param float|int|string|BigNumber $value The value to subtract. |
230
|
|
|
* @return BigNumber |
231
|
|
|
*/ |
232
|
8 |
|
public function subtract($value) |
233
|
|
|
{ |
234
|
8 |
|
if (!$value instanceof self) { |
235
|
7 |
|
$value = new self($value, $this->getScale(), false); |
236
|
6 |
|
} |
237
|
|
|
|
238
|
7 |
|
$newValue = bcsub($this->getValue(), $value->getValue(), $this->getScale()); |
239
|
|
|
|
240
|
7 |
|
return $this->assignValue($newValue); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Checks if this object is mutable. |
245
|
|
|
* |
246
|
|
|
* @return bool |
247
|
|
|
*/ |
248
|
68 |
|
public function isMutable() |
249
|
|
|
{ |
250
|
68 |
|
return $this->mutable; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Converts this class to a string. |
255
|
|
|
* |
256
|
|
|
* @return string |
257
|
|
|
*/ |
258
|
10 |
|
public function toString() |
259
|
|
|
{ |
260
|
10 |
|
return $this->getValue(); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Converts this class to a string. |
265
|
|
|
* |
266
|
|
|
* @return string |
267
|
|
|
*/ |
268
|
10 |
|
public function __toString() |
269
|
|
|
{ |
270
|
10 |
|
return $this->toString(); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* A helper method to assign the given value. |
275
|
|
|
* |
276
|
|
|
* @param int|string|BigNumber $value The value to assign. |
277
|
|
|
* @return BigNumber |
278
|
|
|
*/ |
279
|
52 |
|
private function assignValue($value) |
280
|
|
|
{ |
281
|
52 |
|
if ($this->isMutable()) { |
282
|
43 |
|
$result = $this->internalSetValue($value); |
283
|
43 |
|
} else { |
284
|
9 |
|
$result = new BigNumber($value, $this->getScale(), false); |
285
|
|
|
} |
286
|
|
|
|
287
|
52 |
|
return $result; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* A helper method to set the value on this class. |
292
|
|
|
* |
293
|
|
|
* @param int|string|BigNumber $value The value to assign. |
294
|
|
|
* @return BigNumber |
295
|
|
|
*/ |
296
|
102 |
|
private function internalSetValue($value) |
297
|
|
|
{ |
298
|
102 |
|
$valueToSet = (string)$value; |
299
|
|
|
|
300
|
102 |
|
if (!is_numeric($valueToSet)) { |
301
|
7 |
|
throw new InvalidArgumentException('Invalid number provided: ' . $valueToSet); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
// We use a slick trick to make sure the scale is respected. |
305
|
101 |
|
$this->value = bcadd(0, $valueToSet, $this->getScale()); |
306
|
|
|
|
307
|
101 |
|
return $this; |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|