@signpostmarv/intermediary-number 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,529 @@
1
+ import assert from 'assert';
2
+ import BigNumber from 'bignumber.js';
3
+ import Fraction from 'fraction.js';
4
+ import { is_string, } from './Docs.json';
5
+ import { NumberStrings, } from './NumberStrings';
6
+ import { TokenScan, } from './TokenScan';
7
+ export const regex_recurring_number = /^-?(\d+\.)(\d+r|\d*\[\d+\]r?|\d*\(\d+\)r?)$/;
8
+ function do_math(left_operand, operator, right_operand) {
9
+ return IntermediaryCalculation.maybe_reduce_operands(left_operand, operator, IntermediaryNumber.reuse_or_create(right_operand));
10
+ }
11
+ function abs(value) {
12
+ if (value.isZero()) {
13
+ return value;
14
+ }
15
+ return (value.isLessThan(0)
16
+ ? IntermediaryNumber.Zero.minus(value)
17
+ : value);
18
+ }
19
+ function compare(value, to) {
20
+ const comparable = IntermediaryNumber.reuse_or_create(value).toBigNumberOrFraction();
21
+ let result;
22
+ if (comparable instanceof BigNumber) {
23
+ result = to.toBigNumber().comparedTo(comparable);
24
+ }
25
+ else {
26
+ result = to.toFraction().compare(comparable);
27
+ }
28
+ assert.strictEqual((-1 === result
29
+ || 0 === result
30
+ || 1 === result), true, `Expecting -1, 0, or 1, receieved ${JSON.stringify(result)}`);
31
+ return result;
32
+ }
33
+ const conversion_cache = new class {
34
+ toAmountString_cache;
35
+ toBigNumber_cache;
36
+ toFraction_cache;
37
+ toString_cache;
38
+ get AmountString() {
39
+ if (!this.toAmountString_cache) {
40
+ this.toAmountString_cache = new WeakMap();
41
+ }
42
+ return this.toAmountString_cache;
43
+ }
44
+ get BigNumber() {
45
+ if (!this.toBigNumber_cache) {
46
+ this.toBigNumber_cache = new WeakMap();
47
+ }
48
+ return this.toBigNumber_cache;
49
+ }
50
+ get Fraction() {
51
+ if (!this.toFraction_cache) {
52
+ this.toFraction_cache = new WeakMap();
53
+ }
54
+ return this.toFraction_cache;
55
+ }
56
+ get String() {
57
+ if (!this.toString_cache) {
58
+ this.toString_cache = new WeakMap();
59
+ }
60
+ return this.toString_cache;
61
+ }
62
+ dispose(of) {
63
+ for (const cache of [
64
+ this.toAmountString_cache,
65
+ this.toBigNumber_cache,
66
+ this.toFraction_cache,
67
+ this.toString_cache,
68
+ ]) {
69
+ if (cache) {
70
+ cache.delete(of);
71
+ }
72
+ }
73
+ }
74
+ };
75
+ export function dispose(value) {
76
+ conversion_cache.dispose(value);
77
+ }
78
+ function max(first, second, ...remaining) {
79
+ let max = IntermediaryNumber.reuse_or_create(first);
80
+ for (const entry of [second, ...remaining]) {
81
+ const maybe = IntermediaryNumber.reuse_or_create(entry);
82
+ if (-1 === max.compare(maybe)) {
83
+ max = maybe;
84
+ }
85
+ }
86
+ return IntermediaryNumber.reuse_or_create(max);
87
+ }
88
+ export class IntermediaryNumber {
89
+ value;
90
+ static One = new this('1');
91
+ static Zero = new this('0');
92
+ constructor(value) {
93
+ this.value = value;
94
+ }
95
+ get resolve_type() {
96
+ return this.type;
97
+ }
98
+ get type() {
99
+ if (this.value instanceof BigNumber) {
100
+ return 'BigNumber';
101
+ }
102
+ else if (this.value instanceof Fraction) {
103
+ return 'Fraction';
104
+ }
105
+ else if (NumberStrings.is_amount_string(this.value)) {
106
+ return 'amount_string';
107
+ }
108
+ return 'numeric_string';
109
+ }
110
+ abs() {
111
+ return abs(this);
112
+ }
113
+ compare(value) {
114
+ return compare(value, this);
115
+ }
116
+ divide(value) {
117
+ return do_math(this, '/', value);
118
+ }
119
+ do_math_then_dispose(operator, right_operand) {
120
+ const result = this[operator](right_operand);
121
+ if (result !== this) {
122
+ dispose(this);
123
+ }
124
+ return result;
125
+ }
126
+ isGreaterThan(value) {
127
+ return 1 === this.compare(value);
128
+ }
129
+ isLessThan(value) {
130
+ return -1 === this.compare(value);
131
+ }
132
+ isOne() {
133
+ return 0 === this.compare(1);
134
+ }
135
+ isZero() {
136
+ return 0 === this.compare(0);
137
+ }
138
+ max(first, ...remaining) {
139
+ return max(this, first, ...remaining);
140
+ }
141
+ minus(value) {
142
+ return do_math(this, '-', value);
143
+ }
144
+ modulo(value) {
145
+ return do_math(this, '%', value);
146
+ }
147
+ plus(value) {
148
+ if (this.isZero()) {
149
+ return IntermediaryNumber.reuse_or_create(value);
150
+ }
151
+ return do_math(this, '+', value);
152
+ }
153
+ times(value) {
154
+ return do_math(this, 'x', value);
155
+ }
156
+ toAmountString() {
157
+ if (NumberStrings.is_amount_string(this.value)) {
158
+ return this.value;
159
+ }
160
+ return NumberStrings.round_off(this.toBigNumberOrFraction());
161
+ }
162
+ toBigNumber() {
163
+ if (this.value instanceof BigNumber) {
164
+ return this.value;
165
+ }
166
+ else if (this.value instanceof Fraction) {
167
+ return BigNumber(this.value.valueOf());
168
+ }
169
+ const cache = conversion_cache.BigNumber;
170
+ if (cache.has(this)) {
171
+ return cache.get(this);
172
+ }
173
+ const value = BigNumber(this.value);
174
+ cache.set(this, value);
175
+ return value;
176
+ }
177
+ toBigNumberOrFraction() {
178
+ return ('Fraction' === this.type)
179
+ ? this.toFraction()
180
+ : this.toBigNumber();
181
+ }
182
+ toFraction() {
183
+ if (this.value instanceof Fraction) {
184
+ return this.value;
185
+ }
186
+ const cache = conversion_cache.Fraction;
187
+ if (cache.has(this)) {
188
+ return cache.get(this);
189
+ }
190
+ const value = new Fraction(this.toString());
191
+ cache.set(this, value);
192
+ return value;
193
+ }
194
+ toJSON() {
195
+ if (this.isOne()) {
196
+ return {
197
+ type: 'IntermediaryNumber',
198
+ value: '1',
199
+ };
200
+ }
201
+ else if (this.isZero()) {
202
+ return {
203
+ type: 'IntermediaryNumber',
204
+ value: '0',
205
+ };
206
+ }
207
+ if (this.value instanceof Fraction) {
208
+ const [left, right] = this.value.toFraction().split('/');
209
+ if (undefined === right) {
210
+ return {
211
+ type: 'IntermediaryNumber',
212
+ value: left,
213
+ };
214
+ }
215
+ return {
216
+ type: 'IntermediaryCalculation',
217
+ left: {
218
+ type: 'IntermediaryNumber',
219
+ value: left,
220
+ },
221
+ operation: '/',
222
+ right: {
223
+ type: 'IntermediaryNumber',
224
+ value: right,
225
+ },
226
+ };
227
+ }
228
+ else if (this.value instanceof BigNumber) {
229
+ return {
230
+ type: 'IntermediaryNumber',
231
+ value: this.value.toFixed(),
232
+ };
233
+ }
234
+ return {
235
+ type: 'IntermediaryNumber',
236
+ value: this.value,
237
+ };
238
+ }
239
+ toString() {
240
+ if (this.value instanceof BigNumber) {
241
+ return this.value.toFixed();
242
+ }
243
+ return this.value.toString();
244
+ }
245
+ static create(input) {
246
+ if ('' === input) {
247
+ return IntermediaryNumber.Zero;
248
+ }
249
+ if (input instanceof Fraction) {
250
+ return new this(input.simplify(1 / (2 ** 52)));
251
+ }
252
+ if (input instanceof BigNumber
253
+ || NumberStrings.is_numeric_string(input)) {
254
+ return new this(input);
255
+ }
256
+ else if ('number' === typeof input) {
257
+ return new this(BigNumber(input));
258
+ }
259
+ else if (is_string(input) && regex_recurring_number.test(input)) {
260
+ let only_last_digit_recurring = false;
261
+ if (/^\d*\.\d+r$/.test(input)) {
262
+ only_last_digit_recurring = true;
263
+ }
264
+ if (input.endsWith('r')) {
265
+ input = input.substring(0, input.length - 1);
266
+ }
267
+ if (only_last_digit_recurring) {
268
+ input = input.replace(/(\d)$/, '($1)');
269
+ }
270
+ else if (input.includes('[')) {
271
+ input = input.replace(/\[(\d+)\]/, '($1)');
272
+ }
273
+ return new this(new Fraction(input));
274
+ }
275
+ throw new Error('Unsupported argument specified!');
276
+ }
277
+ static create_if_valid(input) {
278
+ const maybe = input.trim();
279
+ if (NumberStrings.is_amount_string(maybe)
280
+ || NumberStrings.is_numeric_string(maybe)) {
281
+ return IntermediaryNumber.create(maybe);
282
+ }
283
+ else if (/^(\d+|\d*\.\d+)\s*[+/*x%-]\s*(\d+|\d*\.\d+)$/.test(maybe)) {
284
+ return (new TokenScan(input)).parsed;
285
+ }
286
+ const scientific = /^(-?\d+(?:\.\d+))e([+-])(\d+)$/.exec(maybe);
287
+ if (scientific) {
288
+ const calc = new IntermediaryCalculation(IntermediaryNumber.Zero, scientific[2], IntermediaryNumber.create(scientific[3])).toBigNumber();
289
+ return IntermediaryNumber.create(scientific[1]).times((new BigNumber(10)).pow(calc));
290
+ }
291
+ try {
292
+ return IntermediaryCalculation.fromString(maybe);
293
+ }
294
+ catch (err) {
295
+ return new NotValid(maybe, err);
296
+ }
297
+ }
298
+ static fromJson(json) {
299
+ if ('IntermediaryNumber' === json.type) {
300
+ return this.create(json.value);
301
+ }
302
+ return new IntermediaryCalculation(this.fromJson(json.left), json.operation, this.fromJson(json.right));
303
+ }
304
+ static reuse_or_create(input) {
305
+ return (((input instanceof IntermediaryNumber)
306
+ || (input instanceof IntermediaryCalculation))
307
+ ? input
308
+ : this.create(input));
309
+ }
310
+ }
311
+ export class NotValid extends Error {
312
+ reason;
313
+ value;
314
+ constructor(not_valid, reason) {
315
+ super('Value given was not valid!');
316
+ this.value = not_valid;
317
+ this.reason = reason;
318
+ }
319
+ }
320
+ const BigNumber_operation_map = {
321
+ '+': (a, b) => a.plus(b),
322
+ '-': (a, b) => a.minus(b),
323
+ 'x': (a, b) => a.times(b),
324
+ '*': (a, b) => a.times(b),
325
+ '%': (a, b) => a.modulo(b),
326
+ };
327
+ const Fraction_operation_map = {
328
+ '+': (a, b) => a.add(b),
329
+ '-': (a, b) => a.sub(b),
330
+ 'x': (a, b) => a.mul(b),
331
+ '*': (a, b) => a.mul(b),
332
+ '/': (a, b) => a.div(b),
333
+ '%': (a, b) => a.mod(b),
334
+ };
335
+ export class IntermediaryCalculation {
336
+ left_operand;
337
+ operation;
338
+ right_operand;
339
+ constructor(left, operation, right) {
340
+ this.left_operand = left;
341
+ this.operation = operation;
342
+ this.right_operand = right;
343
+ }
344
+ get left_type() {
345
+ if (this.left_operand instanceof IntermediaryCalculation) {
346
+ return 'IntermediaryCalculation';
347
+ }
348
+ return this.left_operand.type;
349
+ }
350
+ get resolve_type() {
351
+ return `${this.left_type} ${this.operation} ${this.right_type}`;
352
+ }
353
+ get right_type() {
354
+ if (this.right_operand instanceof IntermediaryCalculation) {
355
+ return 'IntermediaryCalculation';
356
+ }
357
+ return this.right_operand.type;
358
+ }
359
+ get type() {
360
+ return 'IntermediaryCalculation';
361
+ }
362
+ abs() {
363
+ return abs(this);
364
+ }
365
+ compare(value) {
366
+ return compare(value, this);
367
+ }
368
+ divide(value) {
369
+ return do_math(this, '/', value);
370
+ }
371
+ do_math_then_dispose(operator, right_operand) {
372
+ const result = this[operator](right_operand);
373
+ if (result !== this) {
374
+ dispose(this);
375
+ }
376
+ return result;
377
+ }
378
+ isGreaterThan(value) {
379
+ return 1 === this.compare(value);
380
+ }
381
+ isLessThan(value) {
382
+ return -1 === this.compare(value);
383
+ }
384
+ isOne() {
385
+ return 0 === this.compare(1);
386
+ }
387
+ isZero() {
388
+ return 0 === this.compare(0);
389
+ }
390
+ max(first, ...remaining) {
391
+ return max(this, first, ...remaining);
392
+ }
393
+ minus(value) {
394
+ return do_math(this, '-', value);
395
+ }
396
+ modulo(value) {
397
+ return do_math(this, '%', value);
398
+ }
399
+ plus(value) {
400
+ if (this.isZero()) {
401
+ return IntermediaryNumber.reuse_or_create(value);
402
+ }
403
+ return do_math(this, '+', value);
404
+ }
405
+ resolve() {
406
+ const left_operand = this.operand_to_IntermediaryNumber(this.left_operand);
407
+ const right_operand = this.operand_to_IntermediaryNumber(this.right_operand);
408
+ const left = left_operand.toBigNumberOrFraction();
409
+ const right = right_operand.toBigNumberOrFraction();
410
+ if ('/' === this.operation
411
+ || left instanceof Fraction
412
+ || right instanceof Fraction) {
413
+ return IntermediaryNumber.create(Fraction_operation_map[this.operation](((left instanceof BigNumber)
414
+ ? left_operand.toFraction()
415
+ : left), ((right instanceof BigNumber)
416
+ ? right_operand.toFraction()
417
+ : right)));
418
+ }
419
+ return IntermediaryNumber.create(BigNumber_operation_map[this.operation](left, right));
420
+ }
421
+ times(value) {
422
+ return do_math(this, 'x', value);
423
+ }
424
+ toAmountString() {
425
+ const cache = conversion_cache.AmountString;
426
+ if (cache.has(this)) {
427
+ return cache.get(this);
428
+ }
429
+ const value = this.resolve().toAmountString();
430
+ cache.set(this, value);
431
+ return value;
432
+ }
433
+ toBigNumber() {
434
+ const cache = conversion_cache.BigNumber;
435
+ if (cache.has(this)) {
436
+ return cache.get(this);
437
+ }
438
+ const value = this.resolve().toBigNumber();
439
+ cache.set(this, value);
440
+ return value;
441
+ }
442
+ toBigNumberOrFraction() {
443
+ return this.resolve().toBigNumberOrFraction();
444
+ }
445
+ toFraction() {
446
+ const cache = conversion_cache.Fraction;
447
+ if (cache.has(this)) {
448
+ return cache.get(this);
449
+ }
450
+ const value = this.resolve().toFraction();
451
+ cache.set(this, value);
452
+ return value;
453
+ }
454
+ toJSON() {
455
+ const left = this.operand_to_IntermediaryNumber(this.left_operand);
456
+ const right = this.operand_to_IntermediaryNumber(this.right_operand);
457
+ const maybe = IntermediaryCalculation.maybe_short_circuit(left, this.operation, right);
458
+ if (maybe) {
459
+ return maybe.toJSON();
460
+ }
461
+ return {
462
+ type: 'IntermediaryCalculation',
463
+ left: left.toJSON(),
464
+ operation: this.operation,
465
+ right: right.toJSON(),
466
+ };
467
+ }
468
+ toString() {
469
+ const cache = conversion_cache.String;
470
+ if (cache.has(this)) {
471
+ return cache.get(this);
472
+ }
473
+ const value = this.resolve().toString();
474
+ cache.set(this, value);
475
+ return value;
476
+ }
477
+ operand_to_IntermediaryNumber(operand) {
478
+ if ((operand instanceof IntermediaryCalculation)) {
479
+ return operand.resolve();
480
+ }
481
+ else if ('amount_string' === operand.type
482
+ || 'numeric_string' === operand.type) {
483
+ return IntermediaryNumber.create('/' === this.operation
484
+ ? operand.toFraction()
485
+ : operand.toBigNumberOrFraction());
486
+ }
487
+ return operand;
488
+ }
489
+ static fromString(input) {
490
+ return (new TokenScan(input)).parsed;
491
+ }
492
+ static maybe_reduce_operands(left, operation, right) {
493
+ let value = this.maybe_short_circuit(left, operation, right);
494
+ if (undefined === value) {
495
+ value = new IntermediaryCalculation(left, operation, right);
496
+ }
497
+ if (value instanceof IntermediaryCalculation) {
498
+ return value.resolve();
499
+ }
500
+ return value;
501
+ }
502
+ static maybe_short_circuit(left, operation, right) {
503
+ let value = undefined;
504
+ if ('+' === operation) {
505
+ if (left.isZero()) {
506
+ value = right;
507
+ }
508
+ else if (right.isZero()) {
509
+ value = left;
510
+ }
511
+ }
512
+ else if ('-' === operation && right.isZero()) {
513
+ value = left;
514
+ }
515
+ else if ('*x'.includes(operation)) {
516
+ if (left.isZero() || right.isOne()) {
517
+ value = left;
518
+ }
519
+ else if (right.isZero() || left.isOne()) {
520
+ value = right;
521
+ }
522
+ }
523
+ else if ('/' === operation && right.isOne()) {
524
+ value = left;
525
+ }
526
+ return value;
527
+ }
528
+ }
529
+ //# sourceMappingURL=IntermediaryNumber.js.map
@@ -0,0 +1,6 @@
1
+ import BigNumber from "bignumber.js";
2
+ import Fraction from "fraction.js";
3
+ import { numeric_string } from './NumberStrings';
4
+ export type input_types = BigNumber | Fraction | number | string;
5
+ export type value_types = BigNumber | Fraction | numeric_string;
6
+ export type type_property_types = 'BigNumber' | 'Fraction' | 'amount_string' | 'numeric_string';
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=IntermediaryNumberTypes.js.map
@@ -0,0 +1,14 @@
1
+ import { integer_string__type, StringPassedRegExp } from './Docs.json';
2
+ import BigNumber from 'bignumber.js';
3
+ import Fraction from 'fraction.js';
4
+ import type { operand_types } from './IntermediaryNumber';
5
+ export type amount_string = StringPassedRegExp<'^\\d+(?:\\.\\d{1,6})?$'> | integer_string__type | '1' | '0';
6
+ export type numeric_string = amount_string | StringPassedRegExp<'^-?(?:\\d*\\.\\d+|\\d+(?:\\.\\d+)?)$'>;
7
+ export declare class NumberStrings {
8
+ static amount_string(maybe: string): amount_string;
9
+ static is_amount_string(maybe: unknown): maybe is amount_string;
10
+ static is_numeric_string(maybe: unknown): maybe is numeric_string;
11
+ static round_off(number: BigNumber | Fraction | operand_types): amount_string;
12
+ private static configure;
13
+ private static throw_if_not_amount_string;
14
+ }
@@ -0,0 +1,54 @@
1
+ import { is_string, NotAnAmountString, } from './Docs.json';
2
+ import BigNumber from 'bignumber.js';
3
+ import Fraction from 'fraction.js';
4
+ export class NumberStrings {
5
+ static amount_string(maybe) {
6
+ this.throw_if_not_amount_string(maybe);
7
+ return maybe;
8
+ }
9
+ static is_amount_string(maybe) {
10
+ return (is_string(maybe)
11
+ && (maybe === '0'
12
+ || /^\d+(?:\.\d{1,6})?$/.test(maybe)
13
+ || /^\d*(?:\.\d{1,6})$/.test(maybe)
14
+ || /^\d+$/.test(maybe)));
15
+ }
16
+ static is_numeric_string(maybe) {
17
+ return (this.is_amount_string(maybe)
18
+ || (is_string(maybe)
19
+ && /^-?(?:\d*\.\d+|\d+(?:\.\d+)?)$/.test(maybe)));
20
+ }
21
+ static round_off(number) {
22
+ let result;
23
+ number = ((number instanceof BigNumber)
24
+ || (number instanceof Fraction))
25
+ ? number
26
+ : number.toBigNumberOrFraction();
27
+ if (number instanceof BigNumber) {
28
+ this.configure();
29
+ result = number.toString();
30
+ }
31
+ else {
32
+ result = number.valueOf().toString();
33
+ }
34
+ if (/\.\d{7,}$/.test(result)) {
35
+ const [before, after] = result.split('.');
36
+ return `${before}.${'0' === after.substring(6, 7)
37
+ ? after.substring(0, 6).replace(/0+$/, '')
38
+ : (parseInt(after.substring(0, 6), 10) + 1).toString().padStart(Math.min(6, after.length), '0')}`.replace(/\.$/, '');
39
+ }
40
+ return result;
41
+ }
42
+ static configure() {
43
+ BigNumber.set({
44
+ DECIMAL_PLACES: 7,
45
+ ROUNDING_MODE: BigNumber.ROUND_HALF_CEIL,
46
+ });
47
+ }
48
+ static throw_if_not_amount_string(maybe) {
49
+ if (!this.is_amount_string(maybe)) {
50
+ throw new NotAnAmountString('Not a supported amount string!');
51
+ }
52
+ }
53
+ }
54
+ //# sourceMappingURL=NumberStrings.js.map
@@ -0,0 +1,15 @@
1
+ import BigNumber from 'bignumber.js';
2
+ import Fraction from 'fraction.js';
3
+ import { math_types, operand_types } from './IntermediaryNumber';
4
+ import { amount_string } from './NumberStrings';
5
+ export type number_arg = BigNumber | number | amount_string;
6
+ export declare class Numbers {
7
+ static divide_if_not_one(left: math_types, right: Fraction, require_fraction: true): Fraction;
8
+ static divide_if_not_one(left: math_types, right: Fraction, require_fraction: false): Fraction | math_types;
9
+ static least_common_multiple_deferred(numbers: [
10
+ (number_arg | operand_types),
11
+ (number_arg | operand_types),
12
+ ...(number_arg | operand_types)[]
13
+ ]): (Fraction);
14
+ static sum_series_fraction(a: Fraction, b: Fraction): Fraction;
15
+ }
package/lib/Numbers.js ADDED
@@ -0,0 +1,50 @@
1
+ import assert from 'assert';
2
+ import Fraction from 'fraction.js';
3
+ import { IntermediaryNumber, } from './IntermediaryNumber';
4
+ export class Numbers {
5
+ static divide_if_not_one(left, right, require_fraction) {
6
+ const result = (0 === right.compare(1))
7
+ ? left
8
+ : IntermediaryNumber.reuse_or_create(left).divide(right);
9
+ return require_fraction
10
+ ? ((result instanceof Fraction)
11
+ ? result
12
+ : IntermediaryNumber.reuse_or_create(result).toFraction())
13
+ : result;
14
+ }
15
+ static least_common_multiple_deferred(numbers) {
16
+ if (2 === numbers.length) {
17
+ return (IntermediaryNumber.reuse_or_create(numbers[0]).toFraction().lcm(IntermediaryNumber.reuse_or_create(numbers[1]).toFraction()));
18
+ }
19
+ return (numbers.map(e => IntermediaryNumber.reuse_or_create(e).toFraction()).reduce(
20
+ // based on https://www.npmjs.com/package/mlcm?activeTab=code
21
+ (was, is) => {
22
+ return was.mul(is).abs().div(was.gcd(is));
23
+ }));
24
+ }
25
+ static sum_series_fraction(a, b) {
26
+ assert.strictEqual(b.compare(a), -1, `Expecting ${b.toString()} to be less than ${a.toString()}`);
27
+ // adapted from @stdlib/math-base-tools-sum-series
28
+ const tolerance = (new Fraction(1)).div((new Fraction(2)).pow(52));
29
+ let counter = 10000000;
30
+ const divisor = a.div(b);
31
+ function calculate(number) {
32
+ let previous = number;
33
+ return () => {
34
+ const next = previous.div(divisor);
35
+ previous = next;
36
+ return next;
37
+ };
38
+ }
39
+ const generator = calculate(a);
40
+ let next_term;
41
+ let result = a;
42
+ do {
43
+ next_term = generator();
44
+ result = result.add(next_term.simplify(tolerance.valueOf()));
45
+ } while ((-1 === tolerance.mul(result).abs().compare(next_term.abs()))
46
+ && --counter);
47
+ return result;
48
+ }
49
+ }
50
+ //# sourceMappingURL=Numbers.js.map