-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
614 lines (489 loc) · 17.7 KB
/
index.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
import {notNull} from "@softwareventures/nullable";
import type {Comparator} from "@softwareventures/ordered";
import {Comparison} from "@softwareventures/ordered";
import {i32, iadd, iclamp, idiv, imod, imul, ineg, ipow, isub, isum} from "i32";
class StrictDecimal {
public constructor(public readonly units: number, public readonly billionths: number) {
Object.freeze(this);
}
public toString(): string {
return "Decimal " + format(this);
}
}
export interface Decimal {
readonly units: number;
readonly billionths: number;
}
export type DecimalLike = number | Partial<Decimal>;
export function decimal(value: DecimalLike): Decimal {
if (value instanceof StrictDecimal) {
return value;
} else {
const source = typeof value === "number" ? {units: value} : value;
let units = source.units == null ? 0 : Math.floor(source.units);
let billionths: number;
if (isFinite(units)) {
billionths = source.units == null ? 0 : Math.round((source.units - units) * 1e9);
if (source.billionths != null) {
billionths += Math.round(source.billionths);
}
if (isFinite(billionths)) {
if (billionths >= 1e9) {
units += Math.floor(billionths / 1e9);
billionths -= Math.floor(billionths / 1e9) * 1e9;
} else if (billionths <= -1e9) {
units += Math.ceil(billionths / 1e9);
billionths -= Math.ceil(billionths / 1e9) * 1e9;
}
if (units > 0 && billionths < 0) {
units -= 1;
billionths += 1e9;
} else if (units < 0 && billionths > 0) {
units += 1;
billionths -= 1e9;
}
} else {
units = billionths;
}
} else {
billionths = units;
}
return new StrictDecimal(i32(units), i32(billionths));
}
}
export function normalize(value: DecimalLike): Decimal {
return decimal(value);
}
export function normalizeDecimal(value: DecimalLike): Decimal {
return decimal(value);
}
export const zero: Decimal = new StrictDecimal(0, 0);
export const decimalZero = zero;
export const epsilon: Decimal = new StrictDecimal(0, 1);
export const decimalEpsilon = epsilon;
export function isInteger(value: DecimalLike): boolean {
const {billionths} = decimal(value);
return billionths === 0;
}
export function decimalIsInteger(value: DecimalLike): boolean {
return isInteger(value);
}
export function parse(text: string): Decimal | null {
const matches = /^([-+]?)(?:([0-9]{1,10})(?:\.([0-9]{0,9}))?|\.([0-9]{1,9}))$/u.exec(text);
if (matches == null) {
return null;
}
const sign = matches[1];
const units = parseInt(matches[2] ?? "0", 10);
const billionths = parseInt(`${matches[3] ?? matches[4] ?? ""}000000000`.substr(0, 9), 10);
if (units !== i32(units)) {
return null;
}
return sign === "-"
? new StrictDecimal(ineg(units), ineg(billionths))
: new StrictDecimal(i32(units), i32(billionths));
}
export function parseDecimal(text: string): Decimal | null {
return parse(text);
}
export function format(value: DecimalLike): string {
const {units, billionths} = decimal(value);
if (billionths === 0) {
return String(units);
} else if (units < 0 || billionths < 0) {
return `-${-units}.${`00000000${-billionths}`.substr(-9).replace(/0*$/u, "")}`;
} else {
return `${units}.${`00000000${billionths}`.substr(-9).replace(/0*$/u, "")}`;
}
}
export function formatDecimal(value: DecimalLike): string {
return format(value);
}
export function formatFixed(value: DecimalLike, fractionDigits = 0): string {
const n = decimal(value);
const d = iclamp(fractionDigits, 0, 9);
if (d === 0) {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
const carry = Number(n.billionths >= 500000000) || -Number(n.billionths < -500000000);
return iadd(n.units, carry).toFixed(0);
} else {
const divisor = 10 ** (9 - d);
const carryPoint = 0.5 * divisor;
const modulo = n.billionths % divisor;
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
const carry = imul(divisor, Number(modulo >= carryPoint) || -Number(modulo < -carryPoint));
const {units, billionths} = add(n, {billionths: carry});
if (units < 0 || billionths < 0) {
return `-${-units}.${`00000000${-billionths}`.substr(-9, d)}`;
} else {
return `${units}.${`00000000${billionths}`.substr(-9, d)}`;
}
}
}
export function formatFixedFn(fractionDigits = 0): (value: DecimalLike) => string {
return value => formatFixed(value, fractionDigits);
}
export function formatDecimalFixed(value: DecimalLike, fractionDigits = 0): string {
return formatFixed(value, fractionDigits);
}
export function formatDecimalFixedFn(fractionDigits = 0): (value: DecimalLike) => string {
return formatFixedFn(fractionDigits);
}
export function sign(value: DecimalLike): -1 | 0 | 1 {
const {units, billionths} = decimal(value);
if (units === 0) {
if (billionths === 0) {
return 0;
} else if (billionths < 0) {
return -1;
} else {
return 1;
}
} else if (units < 0) {
return -1;
} else {
return 1;
}
}
export function decimalSign(value: DecimalLike): -1 | 0 | 1 {
return sign(value);
}
export function negate(value: DecimalLike): Decimal {
const {units, billionths} = decimal(value);
return new StrictDecimal(ineg(units), ineg(billionths));
}
export function negateDecimal(value: DecimalLike): Decimal {
return negate(value);
}
export function add(a: DecimalLike, b: DecimalLike): Decimal {
const an = decimal(a);
const bn = decimal(b);
return decimal({
units: an.units + bn.units,
billionths: an.billionths + bn.billionths
});
}
export function addFn(b: DecimalLike): (a: DecimalLike) => Decimal {
return a => add(a, b);
}
export function addDecimal(a: DecimalLike, b: DecimalLike): Decimal {
return add(a, b);
}
export function addDecimalFn(b: DecimalLike): (a: DecimalLike) => Decimal {
return addFn(b);
}
export function subtract(a: DecimalLike, b: DecimalLike): Decimal {
return add(a, negate(b));
}
export function subtractFn(b: DecimalLike): (a: DecimalLike) => Decimal {
return a => subtract(a, b);
}
export function subtractDecimal(a: DecimalLike, b: DecimalLike): Decimal {
return subtract(a, b);
}
export function subtractDecimalFn(b: DecimalLike): (a: DecimalLike) => Decimal {
return subtractFn(b);
}
export function subtractFrom(a: DecimalLike): (b: DecimalLike) => Decimal {
return b => subtract(a, b);
}
export function subtractDecimalFrom(a: DecimalLike): (b: DecimalLike) => Decimal {
return subtractFrom(a);
}
export function multiply(a: DecimalLike, b: DecimalLike): Decimal {
const aThousandths = toThousandths(a);
const bThousandths = toThousandths(b);
const cThousandths: [number, number, number, number, number, number, number, number, number] = [
0, 0, 0, 0, 0, 0, 0, 0, 0
];
let carry = 0;
for (let i = 8; i >= 0; --i) {
let sum = carry;
for (let j = Math.max(i - 3, 0); j < 6 && j < i + 3; ++j) {
const k = i - j + 2;
sum = iadd(sum, imul(notNull(aThousandths[j]), notNull(bThousandths[k])));
}
cThousandths[i] = imod(sum, 1e3);
carry = idiv(sum, 1e3);
if (i === 6) {
if (cThousandths[6] >= 500) {
carry = iadd(carry, 1);
} else if (cThousandths[6] < -500) {
carry = iadd(carry, -1);
}
}
}
return fromThousandths(cThousandths.slice(0, 6) as Thousandths);
}
export function multiplyFn(b: DecimalLike): (a: DecimalLike) => Decimal {
return a => multiply(a, b);
}
export function multiplyDecimal(a: DecimalLike, b: DecimalLike): Decimal {
return multiply(a, b);
}
export function multiplyDecimalFn(b: DecimalLike): (a: DecimalLike) => Decimal {
return multiplyFn(b);
}
export const compare: Comparator<DecimalLike> = (a, b) => {
const an = decimal(a);
const bn = decimal(b);
if (an.units < bn.units) {
return Comparison.before;
} else if (an.units > bn.units) {
return Comparison.after;
} else if (an.billionths < bn.billionths) {
return Comparison.before;
} else if (an.billionths > bn.billionths) {
return Comparison.after;
} else if (an.billionths === bn.billionths) {
return Comparison.equal;
} else {
return Comparison.undefined;
}
};
export const compareDecimal = compare;
export function lessThan(a: DecimalLike, b: DecimalLike): boolean {
const an = decimal(a);
const bn = decimal(b);
return an.units < bn.units || (an.units === bn.units && an.billionths < bn.billionths);
}
export function lessThanFn(b: DecimalLike): (a: DecimalLike) => boolean {
return a => lessThan(a, b);
}
export function decimalLessThan(a: DecimalLike, b: DecimalLike): boolean {
return lessThan(a, b);
}
export function decimalLessThanFn(b: DecimalLike): (a: DecimalLike) => boolean {
return lessThanFn(b);
}
export function lessThanOrEqual(a: DecimalLike, b: DecimalLike): boolean {
const an = decimal(a);
const bn = decimal(b);
return an.units < bn.units || (an.units === bn.units && an.billionths <= bn.billionths);
}
export function lessThanOrEqualFn(b: DecimalLike): (a: DecimalLike) => boolean {
return a => lessThanOrEqual(a, b);
}
export function decimalLessThanOrEqual(a: DecimalLike, b: DecimalLike): boolean {
return lessThanOrEqual(a, b);
}
export function decimalLessThanOrEqualFn(b: DecimalLike): (a: DecimalLike) => boolean {
return lessThanOrEqualFn(b);
}
export function greaterThan(a: DecimalLike, b: DecimalLike): boolean {
const an = decimal(a);
const bn = decimal(b);
return an.units > bn.units || (an.units === bn.units && an.billionths > bn.billionths);
}
export function greaterThanFn(b: DecimalLike): (a: DecimalLike) => boolean {
return a => greaterThan(a, b);
}
export function decimalGreaterThan(a: DecimalLike, b: DecimalLike): boolean {
return greaterThan(a, b);
}
export function decimalGreaterThanFn(b: DecimalLike): (a: DecimalLike) => boolean {
return greaterThanFn(b);
}
export function greaterThanOrEqual(a: DecimalLike, b: DecimalLike): boolean {
const an = decimal(a);
const bn = decimal(b);
return an.units > bn.units || (an.units === bn.units && an.billionths >= bn.billionths);
}
export function greaterThanOrEqualFn(b: DecimalLike): (a: DecimalLike) => boolean {
return a => greaterThanOrEqual(a, b);
}
export function decimalGreaterThanOrEqual(a: DecimalLike, b: DecimalLike): boolean {
return greaterThanOrEqual(a, b);
}
export function decimalGreaterThanOrEqualFn(b: DecimalLike): (a: DecimalLike) => boolean {
return greaterThanOrEqualFn(b);
}
export function equal(a: DecimalLike, b: DecimalLike): boolean {
const an = decimal(a);
const bn = decimal(b);
return an.units === bn.units && an.billionths === bn.billionths;
}
export function equalFn(b: DecimalLike): (a: DecimalLike) => boolean {
return a => equal(a, b);
}
export function decimalEqual(a: DecimalLike, b: DecimalLike): boolean {
return equal(a, b);
}
export function decimalEqualFn(b: DecimalLike): (a: DecimalLike) => boolean {
return equalFn(b);
}
export function notEqual(a: DecimalLike, b: DecimalLike): boolean {
const an = decimal(a);
const bn = decimal(b);
return an.units !== bn.units || an.billionths !== bn.billionths;
}
export function notEqualFn(b: DecimalLike): (a: DecimalLike) => boolean {
return a => notEqual(a, b);
}
export function decimalNotEqual(a: DecimalLike, b: DecimalLike): boolean {
return notEqual(a, b);
}
export function decimalNotEqualFn(b: DecimalLike): (a: DecimalLike) => boolean {
return notEqualFn(b);
}
export function abs(value: DecimalLike): Decimal {
const {units, billionths} = decimal(value);
return new StrictDecimal(Math.abs(units), Math.abs(billionths));
}
export function decimalAbs(value: DecimalLike): Decimal {
return abs(value);
}
export function trunc(value: DecimalLike): Decimal {
const {units} = decimal(value);
return new StrictDecimal(units, 0);
}
export function decimalTrunc(value: DecimalLike): Decimal {
return trunc(value);
}
export function floor(value: DecimalLike, fractionDigits = 0): Decimal {
const n = decimal(value);
if (fractionDigits >= 9 || n.billionths === 0) {
return n;
}
if (fractionDigits <= 0) {
if (n.billionths === 0) {
return n;
} else if (n.billionths > 0) {
return new StrictDecimal(n.units, 0);
} else {
return new StrictDecimal(isub(n.units, 1), 0);
}
}
const modulus = imul(10, ipow(10, 8 - i32(fractionDigits)));
const modulo = imod(n.billionths, modulus);
const truncated = isub(n.billionths, modulo);
if (n.billionths < 0 && modulo !== 0) {
return new StrictDecimal(n.units, isub(truncated, modulus));
} else {
return new StrictDecimal(n.units, truncated);
}
}
export function floorFn(fractionDigits: number): (value: DecimalLike) => Decimal {
return value => floor(value, fractionDigits);
}
export function decimalFloor(value: DecimalLike, fractionDigits = 0): Decimal {
return floor(value, fractionDigits);
}
export function decimalFloorFn(fractionDigits: number): (value: DecimalLike) => Decimal {
return floorFn(fractionDigits);
}
export function ceil(value: DecimalLike, fractionDigits = 0): Decimal {
const n = decimal(value);
if (fractionDigits >= 9 || n.billionths === 0) {
return n;
}
if (fractionDigits <= 0) {
if (n.billionths === 0) {
return n;
} else if (n.billionths < 0) {
return new StrictDecimal(n.units, 0);
} else {
return new StrictDecimal(iadd(n.units, 1), 0);
}
}
const modulus = imul(10, ipow(10, 8 - i32(fractionDigits)));
const modulo = imod(n.billionths, modulus);
const truncated = isub(n.billionths, modulo);
if (n.billionths < 0 && modulo !== 0) {
return new StrictDecimal(n.units, truncated);
} else {
return new StrictDecimal(n.units, iadd(truncated, modulus));
}
}
export function ceilFn(fractionDigits: number): (value: DecimalLike) => Decimal {
return value => ceil(value, fractionDigits);
}
export function decimalCeil(value: DecimalLike, fractionDigits = 0): Decimal {
return ceil(value, fractionDigits);
}
export function decimalCeilFn(fractionDigits: number): (value: DecimalLike) => Decimal {
return ceilFn(fractionDigits);
}
export function round(value: DecimalLike, fractionDigits = 0): Decimal {
if (fractionDigits >= 9) {
return decimal(value);
}
const {units, billionths} = decimal(value);
if (fractionDigits <= 0) {
if (billionths >= 500000000) {
return new StrictDecimal(iadd(units, 1), 0);
} else if (billionths < -500000000) {
return new StrictDecimal(isub(units, 1), 0);
} else {
return new StrictDecimal(units, 0);
}
}
const pivot = imul(5, ipow(10, 8 - i32(fractionDigits)));
const modulus = imul(pivot, 2);
const modulo = imod(billionths, modulus);
const truncated = isub(billionths, modulo);
if (modulo >= pivot) {
return new StrictDecimal(units, iadd(truncated, modulus));
} else if (modulo < -pivot) {
return new StrictDecimal(units, isub(truncated, modulus));
} else {
return new StrictDecimal(units, truncated);
}
}
export function roundFn(fractionDigits = 0): (value: DecimalLike) => Decimal {
return value => round(value, fractionDigits);
}
export function decimalRound(value: Decimal, fractionDigits = 0): Decimal {
return round(value, fractionDigits);
}
export function decimalRoundFn(fractionDigits = 0): (value: DecimalLike) => Decimal {
return roundFn(fractionDigits);
}
export function max(a: DecimalLike, b: DecimalLike): Decimal {
const an = decimal(a);
const bn = decimal(b);
return lessThan(an, bn) ? bn : an;
}
export function maxFn(b: DecimalLike): (a: DecimalLike) => Decimal {
return a => max(a, b);
}
export function maxDecimal(a: DecimalLike, b: DecimalLike): Decimal {
return max(a, b);
}
export function maxDecimalFn(b: DecimalLike): (a: DecimalLike) => Decimal {
return maxFn(b);
}
export function min(a: DecimalLike, b: DecimalLike): Decimal {
const an = decimal(a);
const bn = decimal(b);
return lessThanOrEqual(an, bn) ? an : bn;
}
export function minFn(b: DecimalLike): (a: DecimalLike) => Decimal {
return a => min(a, b);
}
export function minDecimal(a: DecimalLike, b: DecimalLike): Decimal {
return min(a, b);
}
export function minDecimalFn(b: DecimalLike): (a: DecimalLike) => Decimal {
return minFn(b);
}
type Thousandths = [number, number, number, number, number, number];
/** @internal */
export function toThousandths(value: DecimalLike): Thousandths {
const {units, billionths} = decimal(value);
return [
idiv(units, 1e6),
imod(idiv(units, 1e3), 1e3),
imod(units, 1e3),
idiv(billionths, 1e6),
imod(idiv(billionths, 1e3), 1e3),
imod(billionths, 1e3)
];
}
/** @internal */
export function fromThousandths(thousandths: Thousandths): Decimal {
const [a, b, c, d, e, f] = thousandths;
const units = isum(imul(a, 1e6), imul(b, 1e3), c);
const billionths = isum(imul(d, 1e6), imul(e, 1e3), f);
return new StrictDecimal(units, billionths);
}