-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
242 lines (219 loc) · 6.95 KB
/
index.js
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
class Decimal {
constructor(data) {
if (data.constructor == Number) {
data = data.toString();
}
if (data.constructor == String) {
data = data.toLowerCase();
if (data == 'inf') {
data = {
numerator: Infinity,
denominator: 1n,
}
}
else if (data == '-inf') {
data = {
numerator: -Infinity,
denominator: 1n,
}
}
else {
var dot = data.indexOf('.');
if (dot == -1) {
data = {
numerator: BigInt(data),
denominator: 1n
}
} else {
data = {
numerator: BigInt(data.replace(/\./, '')),
denominator: 10n ** BigInt(data.length - dot - 1)
}
}
}
}
this.numerator = data.numerator;
this.denominator = data.denominator;
}
normalize() {
// find greatest common divisor and divide
var _gcd = gcd(this.numerator, this.denominator);
this.numerator /= _gcd;
this.denominator /= _gcd;
return this;
}
add(n) {
/*
b / c + B / C = (bC + Bc) / cC
*/
var numerator = this.numerator * n.denominator + n.numerator * this.denominator;
var denominator = this.denominator * n.denominator;
return new Decimal({numerator, denominator});
}
sub(n) {
/*
b / c - B / C = (bC - Bc) / cC
*/
var numerator = this.numerator * n.denominator - n.numerator * this.denominator;
var denominator = this.denominator * n.denominator;
return new Decimal({numerator, denominator});
}
mul(n) {
/*
b / c * B / C = bB / cC
*/
var numerator = this.numerator * n.numerator;
var denominator = this.denominator * n.denominator;
return new Decimal({numerator, denominator});
}
div(n) {
/*
(b / c) / (B / C) = bC / Bc
*/
var numerator = this.numerator * n.denominator;
var denominator = n.numerator * this.denominator;
return new Decimal({numerator, denominator});
}
abs() {
/*
abs(a / b) = abs(a) / abs(b)
| or when both have the same sign
*/
var numerator = this.numerator > 0n ? this.numerator : this.numerator * -1n;
var denominator = this.denominator > 0n ? this.denominator : this.denominator * -1n;
return new Decimal({numerator, denominator});
}
gt(n) {
/*
a/b > A/B -> aB > Ab
*/
return (this.numerator * n.denominator) > (n.numerator * this.denominator)
}
gte(n) {
/*
a/b >= A/B -> aB > Ab
*/
return (this.numerator * n.denominator) >= (n.numerator * this.denominator)
}
lt(n) {
/*
a < b -> b > a
*/
return n.gt(this)
}
lte(n) {
/*
a <= b -> b >= a
*/
return n.gte(this)
}
eq(n) {
/*
a/b == A/B -> aB == Ab
*/
return (this.numerator * n.denominator) == (n.numerator * this.denominator)
}
pow(n) {
/*
(a / b) ^ (c / d) = a ^ (c / d) / b ^ (c / d)
*/
var numerator = this.numerator ** (n.numerator / n.denominator)
var denominator = this.denominator ** (n.numerator / n.denominator)
return new Decimal({numerator, denominator});
}
tpow(n, it) {
/*
Taylor series power approximation
a^b.c = a^b * a^0.c
a ^ n = e ^ (n * ln(a))
e ^ (n * ln(a)) = [1 + 1/k! (n ln(a)) ** k]
*/
var left = n.numerator / n.denominator;
var right = n.numerator - (left * n.denominator);
left = new Decimal({numerator: left, denominator: 1n});
right = new Decimal({numerator: right, denominator: n.denominator});
it = it || 34; // good accuracy without killing much performance
// taylor series approximation of the right side
var result = new Decimal('1');
var rln = this.ln().mul(right).normalize();
var rln_pow = rln;
var fact = new Decimal('1');
for (let i = 1; i < it; i++) {
var k = new Decimal(`${i}`);
fact = fact.mul(k).normalize();
result = result.add(rln_pow.div(fact));
rln_pow = rln_pow.mul(rln).normalize();
}
return this.pow(left).mul(result).normalize();
}
fact() {
/*
n! = n * (n-1)!
*/
var one = new Decimal('1');
if (!this.gt(new Decimal('0'))) {
return one;
}
return this.mul(this.sub(one).fact())
}
ln(it) {
/*
Area hyperbolic tangent function
https://en.wikipedia.org/wiki/Logarithm#Calculation
This isn't fast, nor accurate
We need a better approximation
*/
it = it || 10; // max iterations
var one = new Decimal('1');
var ln = new Decimal('0');
var z = this;
for (let i = 1; i < it; i += 2) {
var _i = new Decimal(`${i}`)
ln = ln.add((z.sub(one).div(z.add(one))).pow(_i).div(_i));
}
return ln.mul(new Decimal('2')).normalize();
}
floor() {
return new Decimal({numerator: this.toBigInt(), denominator: 1n});
}
toBigInt() {
return this.numerator / this.denominator;
}
toNumber() {
return Number(this.numerator) / Number(this.denominator);
}
toString() {
var left = this.numerator / this.denominator;
var right = (this.numerator * (10n**56n)) / this.denominator;
var left = left.toString();
var right = right.toString().replace(/0+$/, '');
if (left != '0') {
right = right.slice(left.length)
}
if (right.length > 54) {
right = right.slice(0, 54) + '...'
}
return `${left}.${right}`
}
}
// https://gist.github.com/bellbind/5468385accdee9df0d88
var gcd = function (a, b) {
// fast GCD aka Binary GCD
if (a === 0n) return b;
if (b === 0n) return a;
if (a === b) return a;
// remove even divisors
var sa = 0n;
while (!(a & 1n)) sa++, a >>= 1n;
var sb = 0n;
while (!(b & 1n)) sb++, b >>= 1n;
var p = sa < sb ? sa : sb; // Power part of 2^p Common Divisor
// euclidean algorithm: limited only odd numbers
while (a !== b) {// both a and b should be odd
if (b > a) {var t = a; a = b; b = t;} // swap as a > b
a -= b; // a is even because of odd - odd
do a >>= 1n; while (!(a & 1n)); // a become odd
}
return a << p; // Odd-Common-Divisor * 2^p
};
module.exports = Decimal;