-
Notifications
You must be signed in to change notification settings - Fork 2
/
mod.ts
323 lines (276 loc) · 7.59 KB
/
mod.ts
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
import {
bigIntAbs,
countTrailingZeros,
extractExp,
getDecimals,
} from "./util.ts";
export interface BigDenaryRaw {
base: bigint;
decimals: number;
}
export type BDNumberInput = number | string | bigint | BigDenary | BigDenaryRaw;
export enum BDCompare {
Greater = 1,
Less = -1,
Equal = 0,
}
export class BigDenary {
base: bigint;
private _decimals: number;
constructor(n: BDNumberInput) {
if (n instanceof BigDenary) {
this.base = n.base;
this._decimals = n.decimals;
} else if (typeof n === "number") {
this._decimals = getDecimals(n);
this.base = BigInt(n * Math.pow(10, this._decimals));
} else if (typeof n === "string") {
const [mul, exp] = extractExp(n);
const mulDec = getDecimals(mul);
if (exp > mulDec) {
this.base = BigInt(mul.replace(".", "")) *
BigInt(Math.pow(10, (exp - mulDec)));
this._decimals = 0;
} else {
this.base = BigInt(mul.replace(".", ""));
this._decimals = mulDec - exp;
}
} else if (typeof n === "bigint") {
this.base = n * this.decimalMultiplier;
this._decimals = 0;
} else if (_isBigDenaryRaw(n)) {
if (n.decimals < 0) {
throw new Error("InvalidBigDenaryRaw");
}
this.base = n.base;
this._decimals = n.decimals;
} else {
throw new Error("UnsupportedInput");
}
this.trimTrailingZeros();
}
toString(): string {
if (this.base === 0n) {
return "0";
}
const negative: boolean = (this.base < 0);
let base = this.base;
if (negative) {
base = base * -1n;
}
const baseStr = base.toString();
const position = baseStr.length - this._decimals;
let pre: string;
let post: string;
if (position < 0) {
pre = "";
post = `${_strOfZeros(position * -1)}${baseStr}`;
} else {
pre = baseStr.substr(0, position);
post = baseStr.substr(position);
}
let result: string;
if (pre.length === 0) {
result = `0.${post}`;
} else if (post.length === 0) {
result = `${pre}`;
} else {
result = `${pre}.${post}`;
}
if (negative) {
return `-${result}`;
}
return result;
}
valueOf(): number {
return Number.parseFloat(this.toString());
}
toFixed(digits?: number): string {
if (!digits) {
return this.toString();
}
const temp = new BigDenary(this);
temp.scaleDecimalsTo(digits);
return temp.toString();
}
get decimals(): number {
return this._decimals;
}
/**
* Alters the decimal places, actual underlying value does not change
*/
scaleDecimalsTo(_decimals: number): void {
if (_decimals > this._decimals) {
this.base = this.base *
BigDenary.getDecimalMultiplier(_decimals - this._decimals);
} else if (_decimals < this._decimals) {
const adjust = this._decimals - _decimals;
const multiplier = BigDenary.getDecimalMultiplier(adjust);
const remainder = this.base % multiplier;
this.base = this.base / multiplier;
if (bigIntAbs(remainder * 2n) >= multiplier) {
if (this.base >= 0) {
this.base += 1n;
} else {
this.base -= 1n;
}
}
}
this._decimals = _decimals;
}
get decimalMultiplier(): bigint {
return BigDenary.getDecimalMultiplier(this._decimals);
}
static getDecimalMultiplier(decimals: number): bigint {
let multiplierStr: string = "1";
for (let i = 0; i < decimals; i += 1) {
multiplierStr += "0";
}
return BigInt(multiplierStr);
}
trimTrailingZeros(): void {
const trailingZerosCount = countTrailingZeros(this.base, this.decimals);
if (trailingZerosCount > 0) {
this.scaleDecimalsTo(this.decimals - trailingZerosCount);
}
}
/**
* Operations
*/
plus(operand: BDNumberInput): BigDenary {
const curr = new BigDenary(this);
const oper = new BigDenary(operand);
const targetDecs = Math.max(curr.decimals, oper.decimals);
curr.scaleDecimalsTo(targetDecs);
oper.scaleDecimalsTo(targetDecs);
return new BigDenary({
base: curr.base + oper.base,
decimals: targetDecs,
});
}
minus(operand: BDNumberInput): BigDenary {
return this.plus((new BigDenary(operand)).negated());
}
multipliedBy(operand: BDNumberInput): BigDenary {
const curr = new BigDenary(this);
const oper = new BigDenary(operand);
const targetDecs = curr.decimals + oper.decimals;
return new BigDenary({
base: curr.base * oper.base,
decimals: targetDecs,
});
}
dividedBy(operand: BDNumberInput): BigDenary {
const MIN_DIVIDE_DECIMALS = 20;
const curr = new BigDenary(this);
const oper = new BigDenary(operand);
const targetDecs = Math.max(
curr.decimals * 2,
oper.decimals * 2,
MIN_DIVIDE_DECIMALS,
);
curr.scaleDecimalsTo(targetDecs);
return new BigDenary({
base: curr.base / oper.base,
decimals: curr.decimals - oper.decimals,
});
}
negated(): BigDenary {
return new BigDenary({
base: this.base * -1n,
decimals: this.decimals,
});
}
absoluteValue(): BigDenary {
if (this.base >= 0n) {
return this;
}
return this.negated();
}
/**
* Comparisons
*/
comparedTo(comparator: BDNumberInput): BDCompare {
const curr = new BigDenary(this);
const comp = new BigDenary(comparator);
const targetDecs = Math.max(curr.decimals, comp.decimals);
curr.scaleDecimalsTo(targetDecs);
comp.scaleDecimalsTo(targetDecs);
if (curr.base > comp.base) {
return BDCompare.Greater;
} else if (curr.base < comp.base) {
return BDCompare.Less;
}
return BDCompare.Equal;
}
equals(comparator: BDNumberInput): boolean {
return (this.comparedTo(comparator) === BDCompare.Equal);
}
greaterThan(comparator: BDNumberInput): boolean {
return (this.comparedTo(comparator) === BDCompare.Greater);
}
greaterThanOrEqualTo(comparator: BDNumberInput): boolean {
return (
(this.comparedTo(comparator) === BDCompare.Greater) ||
(this.comparedTo(comparator) === BDCompare.Equal)
);
}
lessThan(comparator: BDNumberInput): boolean {
return (this.comparedTo(comparator) === BDCompare.Less);
}
lessThanOrEqualTo(comparator: BDNumberInput): boolean {
return (
(this.comparedTo(comparator) === BDCompare.Less) ||
(this.comparedTo(comparator) === BDCompare.Equal)
);
}
/**
* Shortforms
*/
add(operand: BDNumberInput): BigDenary {
return this.plus(operand);
}
sub(operand: BDNumberInput): BigDenary {
return this.minus(operand);
}
mul(operand: BDNumberInput): BigDenary {
return this.multipliedBy(operand);
}
div(operand: BDNumberInput): BigDenary {
return this.dividedBy(operand);
}
neg(): BigDenary {
return this.negated();
}
abs(): BigDenary {
return this.absoluteValue();
}
cmp(comparator: BDNumberInput): BDCompare {
return this.comparedTo(comparator);
}
eq(comparator: BDNumberInput): boolean {
return this.equals(comparator);
}
gt(comparator: BDNumberInput): boolean {
return this.greaterThan(comparator);
}
gte(comparator: BDNumberInput): boolean {
return this.greaterThanOrEqualTo(comparator);
}
lt(comparator: BDNumberInput): boolean {
return this.lessThan(comparator);
}
lte(comparator: BDNumberInput): boolean {
return this.lessThanOrEqualTo(comparator);
}
}
function _isBigDenaryRaw(input: BigDenaryRaw): input is BigDenaryRaw {
return true;
}
function _strOfZeros(count: number): string {
let result = "";
for (let i = 0; i < count; i += 1) {
result += "0";
}
return result;
}