forked from shtse8/SotiMath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Number.php
357 lines (302 loc) · 6.54 KB
/
Number.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
class Number
{
protected $internalScale = 20;
protected $iterations = 100;
protected $value = 0;
protected $humanUnits = [
3 => 'K',
6 => 'M',
9 => 'G',
12 => 'T',
15 => 'P',
'E'
];
public function __construct($value)
{
bcscale($this->internalScale);
$value = (string) $value;
$this->value = $this->normalizeFloat($value);
}
private function normalizeFloat($value)
{
$part = explode("E", $value);
if (count($part) == 2) {
$value = bcmul($part[0], bcpow(10, $part[1]));
}
return $value;
}
public function clone()
{
return new self($this->value);
}
public function add($delta)
{
$delta = $this->normalizeFloat($delta);
$newValue = bcadd($this->value, $delta);
return new self($newValue);
}
public function sub($delta)
{
$delta = $this->normalizeFloat($delta);
$newValue = bcsub($this->value, $delta);
return new self($newValue);
}
public function mul($delta)
{
$delta = $this->normalizeFloat($delta);
$newValue = bcmul($this->value, $delta);
return new self($newValue);
}
public function div($delta)
{
$delta = $this->normalizeFloat($delta);
$newValue = bcdiv($this->value, $delta);
return new self($newValue);
}
public function mod($delta)
{
$delta = $this->normalizeFloat($delta);
$newValue = bcmod($this->value, $delta);
return new self($newValue);
}
public function pow($delta)
{
$delta = $this->normalizeFloat($delta);
$newValue = bcpow($this->value, $delta);
return new self($newValue);
}
public function ln()
{
$retval = new self(0);
for ($i = 0; $i < $this->iterations; $i++) {
$pow = (new self(2))->mul($i)->add(1);
$mul = (new self(1))->div($pow);
$base = $this->sub(1)->div($this->add(1));
$fraction = $base->pow($pow)->mul($mul);
$retval = $retval->add($fraction);
}
return $retval->mul(2);
}
/**
* Gives the base 10 logarithm of the argument (uses ln $val/ln 10).
*
* @return string|integer|float
*/
public function log($base)
{
$baseLn = (new self($base))->ln();
return $this->ln()->div($baseLn);
}
public function floor()
{
$result = 0;
if ($this->isNegative()) {
$result = -1;
}
$newValue = bcadd($this->value, $result, 0);
return new self($newValue);
}
public function ceil()
{
$result = 1;
if ($this->isNegative()) {
$result = 0;
}
$newValue = bcadd($this->value, $result, 0);
return new self($newValue);
}
public function abs()
{
return new self(ltrim($this->value, "-"));
}
public function isEqual($arg)
{
$arg = $this->normalizeFloat($arg);
return bccomp($this->value, $arg) == 0;
}
public function isSmaller($arg)
{
$arg = $this->normalizeFloat($arg);
return bccomp($this->value, $arg) == -1;
}
public function isSmallerOrEqual($arg)
{
return $this->isSmaller($arg) || $this->isEqual($arg);
}
public function isGreater($arg)
{
$arg = $this->normalizeFloat($arg);
return bccomp($this->value, $arg) == 1;
}
public function isGreaterOrEqual($arg)
{
return $this->isGreater($arg) || $this->isEqual($arg);
}
public function isNegative()
{
return $this->isSmaller(0);
}
public function isPositive()
{
return $this->isGreater(0);
}
public function inc()
{
return $this->add(1);
}
public function dec()
{
return $this->sub(1);
}
public function format($decimals = 0)
{
$str = "";
if ($this->isNegative()) {
$str = "-";
}
$valueSplit = explode(".", $this->abs()->toString());
$integerLength = strlen($valueSplit[0]);
for ($i=0; $i < $integerLength; $i++) {
$str .= $valueSplit[0][$i];
$reverseI = $integerLength - $i - 1;
if ($reverseI && $reverseI % 3 == 0) {
$str .= ",";
}
}
if ($valueSplit[1]) {
$str .= ".".$valueSplit[1];
}
return $str;
}
protected function getHumanUnitIndex()
{
$unitIndex = 0;
$integerLength = strlen($this->abs()->floor());
foreach ($this->humanUnits as $index => $unit) {
if ($integerLength - 1 <= $index) {
break;
}
$unitIndex = $index;
}
return $unitIndex;
}
public function getHumanValue()
{
$base = (new self(10))->pow($this->getHumanUnitIndex());
return $this->div($base);
}
public function getHumanUnit()
{
return $this->humanUnits[$this->getHumanUnitIndex()];
}
public function toString()
{
$valueSplit = explode(".", $this->value);
$value = $valueSplit[0];
if ($valueSplit[1]) {
$value .= ".".rtrim($valueSplit[1], ".0");
}
return $value;
}
/* Overload Basic Operators */
public function __add($delta)
{
return $this->add($delta);
}
public function __sub($delta)
{
return $this->sub($delta);
}
public function __mul($delta)
{
return $this->mul($delta);
}
public function __div($delta)
{
return $this->div($delta);
}
public function __mod($delta)
{
return $this->mod($delta);
}
public function __pow($delta)
{
return $this->pow($delta);
}
/* Overload Boolean Operators */
public function __is_identical($arg)
{
return $this->isEqual($arg);
}
public function __is_smaller($arg)
{
return $this->isSmaller($arg);
}
public function __is_smaller_or_equal($arg)
{
return $this->isSmallerOrEqual($arg);
}
public function __is_greater($arg)
{
return $this->isGreater($arg);
}
public function __is_greater_or_equal($arg)
{
return $this->isGreaterOrEqual($arg);
}
/* Overload Inc && Dec */
public function __pre_inc()
{
return $this->inc();
}
public function __pre_dec()
{
return $this->dec();
}
/* TODO: Don't know how to overload post_inc */
public function __post_inc()
{
return $this->inc();
}
/* TODO: Don't know how to overload __post_dec */
public function __post_dec()
{
return $this->dec();
}
/* Overloaed Assign Operators */
public function __assign_add($delta)
{
$delta = $this->normalizeFloat($delta);
$this->value = bcadd($this->value, $delta);
}
public function __assign_sub($delta)
{
$delta = $this->normalizeFloat($delta);
$this->value = bcsub($this->value, $delta);
}
public function __assign_mul($delta)
{
$delta = $this->normalizeFloat($delta);
$this->value = bcmul($this->value, $delta);
}
public function __assign_div($delta)
{
$delta = $this->normalizeFloat($delta);
$this->value = bcdiv($this->value, $delta);
}
public function __assign_mod($delta)
{
$delta = $this->normalizeFloat($delta);
$this->value = bcmod($this->value, $delta);
}
public function __assign_pow($delta)
{
$delta = $this->normalizeFloat($delta);
$this->value = bcpow($this->value, $delta);
}
public function __toString()
{
return $this->toString();
}
}