-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrecise.php
More file actions
275 lines (238 loc) · 8.86 KB
/
Precise.php
File metadata and controls
275 lines (238 loc) · 8.86 KB
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
<?php
namespace ccxt;
class Precise {
public $integer;
public $decimals;
public static $base;
public function __construct($number, $decimals = null) {
if ($decimals === null) {
$modifier = 0;
$number = strtolower($number);
if (strpos($number, 'e') > -1) {
list($number, $modifier) = explode('e', $number);
$modifier = intval($modifier);
}
$decimalIndex = strpos($number, '.');
$this->decimals = ($decimalIndex > -1) ? strlen($number) - $decimalIndex - 1 : 0;
$integerString = str_replace("+", "", str_replace('.', '', $number));
$this->integer = gmp_init($integerString, 10);
$this->decimals = $this->decimals - $modifier;
} else {
$this->integer = $number;
$this->decimals = $decimals;
}
}
public function mul($other) {
$integerResult = gmp_mul($this->integer, $other->integer);
return new Precise($integerResult, $this->decimals + $other->decimals);
}
public function div($other, $precision = 18) {
$distance = $precision - $this->decimals + $other->decimals;
if ($distance === 0) {
$numerator = $this->integer;
} elseif ($distance < 0) {
$exponent = gmp_pow(static::$base, -$distance);
$numerator = gmp_div($this->integer, $exponent);
} else {
$exponent = gmp_pow(static::$base, $distance);
$numerator = gmp_mul($this->integer, $exponent);
}
$result = gmp_div($numerator, $other->integer);
return new Precise($result, $precision);
}
public function add($other) {
if ($this->decimals === $other->decimals) {
$integerResult = gmp_add($this->integer, $other->integer);
return new Precise($integerResult, $this->decimals);
} else {
list($smaller, $bigger) =
($this->decimals > $other->decimals) ? array( $other, $this ) : array( $this, $other );
$exponent = $bigger->decimals - $smaller->decimals;
$normalised = gmp_mul($smaller->integer, gmp_pow(static::$base, $exponent));
$result = gmp_add($normalised, $bigger->integer);
return new Precise($result, $bigger->decimals);
}
}
public function sub($other) {
$negative = new Precise(gmp_neg($other->integer), $other->decimals);
return $this->add($negative);
}
public function abs() {
return new Precise(gmp_abs($this->integer), $this->decimals);
}
public function neg() {
return new Precise(gmp_neg($this->integer), $this->decimals);
}
public function mod($other) {
$rationizerNumerator = max(-$this->decimals + $other->decimals, 0);
$numerator = gmp_mul($this->integer, gmp_pow(static::$base, $rationizerNumerator));
$denominatorRationizer = max(-$other->decimals + $this->decimals, 0);
$denominator = gmp_mul($other->integer, gmp_pow(static::$base, $denominatorRationizer));
$result = gmp_mod($numerator, $denominator);
return new Precise($result, $denominatorRationizer + $other->decimals);
}
public function min($other) {
return $this->lt($other) ? $this : $other;
}
public function max($other) {
return $this->gt($other) ? $this : $other;
}
public function gt($other) {
$sum = $this->sub($other);
return gmp_cmp($sum->integer, '0') > 0;
}
public function ge($other) {
$sum = $this->sub($other);
return gmp_cmp($sum->integer, '0') > -1;
}
public function lt($other) {
return $other->gt($this);
}
public function le($other) {
return $other->ge($this);
}
public function reduce() {
$string = strval($this->integer);
$start = strlen($string) - 1;
if ($start === 0) {
if ($string === '0') {
$this->decimals = 0;
}
return $this;
}
for ($i = $start; $i >= 0; $i--) {
if ($string[$i] !== '0') {
break;
}
}
$difference = $start - $i;
if ($difference === 0) {
return $this;
}
$this->decimals -= $difference;
$this->integer = gmp_init(mb_substr($string, 0, $i + 1), 10);
}
public function equals ($other) {
$this->reduce();
$other->reduce();
return ($this->decimals === $other->decimals) && !gmp_cmp($this->integer, $other->integer);
}
public function __toString() {
$this->reduce();
$sign = gmp_sign($this->integer) === -1 ? '-' : '';
$integerArray = str_split(str_pad(gmp_abs($this->integer), $this->decimals, '0', STR_PAD_LEFT));
$index = count($integerArray) - $this->decimals;
if ($index === 0) {
// if we are adding to the front
$item = '0.';
} else if ($this->decimals < 0) {
$item = str_repeat('0', -$this->decimals);
} else if ($this->decimals === 0) {
$item = '';
} else {
$item = '.';
}
array_splice($integerArray, $index, 0, $item);
return $sign . implode('', $integerArray);
}
public static function string_mul($string1, $string2) {
if (($string1 === null) || ($string2 === null)) {
return null;
}
return strval((new Precise($string1))->mul(new Precise($string2)));
}
public static function string_div($string1, $string2, $precision = 18) {
if (($string1 === null) || ($string2 === null)) {
return null;
}
$string2_precise = new Precise($string2);
if (gmp_cmp($string2_precise->integer, '0') === 0) {
return null;
}
return strval((new Precise($string1))->div($string2_precise, $precision));
}
public static function string_add($string1, $string2) {
if (($string1 === null) && ($string2 === null)) {
return null;
}
if ($string1 === null) {
return $string2;
} elseif ($string2 === null) {
return $string1;
}
return strval((new Precise($string1))->add(new Precise($string2)));
}
public static function string_sub($string1, $string2) {
if (($string1 === null) || ($string2 === null)) {
return null;
}
return strval((new Precise($string1))->sub(new Precise($string2)));
}
public static function string_abs($string) {
if ($string === null) {
return null;
}
return strval((new Precise($string))->abs());
}
public static function string_neg($string) {
if ($string === null) {
return null;
}
return strval((new Precise($string))->neg());
}
public static function string_mod($string1, $string2) {
if (($string1 === null) || ($string2 === null)) {
return null;
}
return strval((new Precise($string1))->mod(new Precise($string2)));
}
public static function string_equals($string1, $string2) {
if (($string1 === null) || ($string2 === null)) {
return null;
}
return (new Precise($string1))->equals(new Precise($string2));
}
public static function string_eq($string1, $string2) {
if (($string1 === null) || ($string2 === null)) {
return null;
}
return (new Precise($string1))->equals(new Precise($string2));
}
public static function string_min($string1, $string2) {
if (($string1 === null) || ($string2 === null)) {
return null;
}
return strval((new Precise($string1))->min(new Precise($string2)));
}
public static function string_max($string1, $string2) {
if (($string1 === null) || ($string2 === null)) {
return null;
}
return strval((new Precise($string1))->max(new Precise($string2)));
}
public static function string_gt($string1, $string2) {
if (($string1 === null) || ($string2 === null)) {
return null;
}
return (new Precise($string1))->gt(new Precise($string2));
}
public static function string_ge($string1, $string2) {
if (($string1 === null) || ($string2 === null)) {
return null;
}
return (new Precise($string1))->ge(new Precise($string2));
}
public static function string_lt($string1, $string2) {
if (($string1 === null) || ($string2 === null)) {
return null;
}
return (new Precise($string1))->lt(new Precise($string2));
}
public static function string_le($string1, $string2) {
if (($string1 === null) || ($string2 === null)) {
return null;
}
return (new Precise($string1))->le(new Precise($string2));
}
}
Precise::$base = \gmp_init(10);