@gouvernathor/fraction.ts 1.0.0 → 1.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.
package/README.md CHANGED
@@ -69,3 +69,9 @@ The `asIrreducible()` parameter-less method returns an `IrreducibleFraction` wit
69
69
  The `limitDenominator()` and `simplify()` methods serve a similar purpose of reducing the value of the operands of the fraction, at the cost of potentially losing some precision. The `limitDenominator()` method takes a single bigint parameter, which must be 1 or greater, and returns a fraction whose denominator is at most that value, and as close to the original fraction as possible. The `simplify()` method takes a `FractionAble` parameter which specifies the acceptable error margin.
70
70
 
71
71
  The Fraction object can be converted or coerced to a string, a number or a bigint, either using the JavaScript coercion rules (`+f`, `${f}`, `f + ""`...) or by calling the `toString()` and `valueOf()` methods. The string representation is of the form "1.23(45)", and can always be passed back to the `Fraction.fromString()` constructor to get the same fraction back. The `valueOf()` method returns a bigint if the exact value of the fraction can be represented as such, or a number otherwise. If you want to be sure to get a number, you can either call `Number(f)` or do `+f`.
72
+
73
+ ### Non-numbers
74
+
75
+ Comparison with (and between, in the case of `Fraction.compare`) Infinity values is supported.
76
+
77
+ Otherwise, usage of Nan and Infinity values is unspecified, and will generally throw errors.
@@ -0,0 +1,10 @@
1
+ import { Fraction, FractionAble, IrreducibleFraction } from "./interface.js";
2
+ export declare function fromNumeric(num: bigint | number): IrreducibleFraction;
3
+ export declare function fromString(str: string): IrreducibleFraction;
4
+ export declare function fromPair(num: bigint | number, denom: bigint | number): Fraction;
5
+ export declare function fromTuple([num, denom]: [bigint | number, bigint | number]): Fraction;
6
+ export declare function fromObject({ numerator, denominator }: {
7
+ numerator: bigint;
8
+ denominator: bigint;
9
+ }): Fraction;
10
+ export declare function fromAny(obj: FractionAble): Fraction;
@@ -0,0 +1,65 @@
1
+ import { tupleFromNumber } from "./fromNumber.js";
2
+ import { FractionImpl } from "./implcls.js";
3
+ import { parse } from "./parse.js";
4
+ function fromBigInt(num) {
5
+ return new FractionImpl(num, 1n);
6
+ }
7
+ function fromNumber(num) {
8
+ if (Number.isInteger(num)) {
9
+ return fromBigInt(BigInt(num));
10
+ }
11
+ if (Number.isNaN(num)) {
12
+ throw new TypeError("Cannot create Fraction from NaN.");
13
+ }
14
+ if (!Number.isFinite(num)) {
15
+ throw new TypeError("Cannot create Fraction from non-finite number.");
16
+ }
17
+ return fromTuple(tupleFromNumber(num));
18
+ }
19
+ export function fromNumeric(num) {
20
+ if (typeof num === "bigint") {
21
+ return fromBigInt(num);
22
+ }
23
+ else if (typeof num === "number") {
24
+ return fromNumber(num);
25
+ }
26
+ throw new TypeError(`Value of unsupported type for Fraction creation: ${num}`);
27
+ }
28
+ export function fromString(str) {
29
+ return fromTuple(parse(str));
30
+ }
31
+ export function fromPair(num, denom) {
32
+ return fromNumeric(num).div(fromNumeric(denom));
33
+ }
34
+ export function fromTuple([num, denom]) {
35
+ return fromPair(num, denom);
36
+ }
37
+ export function fromObject({ numerator, denominator }) {
38
+ if (denominator < 0n) {
39
+ numerator = -numerator;
40
+ denominator = -denominator;
41
+ }
42
+ return new FractionImpl(numerator, denominator);
43
+ }
44
+ export function fromAny(obj) {
45
+ if (obj instanceof FractionImpl) {
46
+ return obj;
47
+ }
48
+ else if (typeof obj === "number") {
49
+ return fromNumber(obj);
50
+ }
51
+ else if (typeof obj === "bigint") {
52
+ return fromBigInt(obj);
53
+ }
54
+ else if (typeof obj === "string") {
55
+ return fromString(obj);
56
+ }
57
+ else if (Array.isArray(obj) && obj.length === 2) {
58
+ return fromTuple(obj);
59
+ }
60
+ else if (typeof obj === "object" && obj !== null) {
61
+ return fromObject(obj);
62
+ }
63
+ throw new TypeError(`Unsupported type for Fraction creation for ${obj}`);
64
+ }
65
+ //# sourceMappingURL=constructors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constructors.js","sourceRoot":"","sources":["../src/constructors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,E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2(num: number): [bigint, bigint];
2
+ export { exact2 as tupleFromNumber };
@@ -0,0 +1,145 @@
1
+ /**
2
+ * This function doesn't seem to return the most exact fraction for a float (64).
3
+ */
4
+ // @ts-expect-error
5
+ function fareySequences(num) {
6
+ let s = 1;
7
+ if (num < 0) {
8
+ s = -1;
9
+ num = -num;
10
+ }
11
+ let z = 1;
12
+ if (num >= 1) {
13
+ z = 10 ** Math.floor(1 + Math.log10(num));
14
+ num /= z;
15
+ }
16
+ let A = 0, B = 1, C = 1, D = 0;
17
+ let n = 0;
18
+ let d = 1;
19
+ const N = 10000000;
20
+ while (B <= N && D <= N) {
21
+ const M = (A + C) / (B + D);
22
+ if (num === M) {
23
+ if (B + D <= N) {
24
+ n = A + C;
25
+ d = B + D;
26
+ }
27
+ else if (D > B) {
28
+ n = C;
29
+ d = D;
30
+ }
31
+ else {
32
+ n = A;
33
+ d = B;
34
+ }
35
+ break;
36
+ }
37
+ else {
38
+ if (num > M) {
39
+ A += C;
40
+ B += D;
41
+ }
42
+ else {
43
+ C += A;
44
+ D += B;
45
+ }
46
+ if (B > N) {
47
+ n = C;
48
+ d = D;
49
+ }
50
+ else {
51
+ n = A;
52
+ d = B;
53
+ }
54
+ }
55
+ }
56
+ return [BigInt(n * z * s), BigInt(d)];
57
+ }
58
+ /**
59
+ * Supposes that `arg` is a finite number.
60
+ */
61
+ function frexp(arg) {
62
+ const absArg = Math.abs(arg);
63
+ if (absArg === 0) {
64
+ return [0, 0n];
65
+ }
66
+ let exponent = BigInt(Math.max(-1023, Math.floor(Math.log2(absArg)) + 1));
67
+ let x = absArg / Number(2n ** exponent);
68
+ while (x < 0.5) {
69
+ x *= 2;
70
+ exponent--;
71
+ }
72
+ while (x >= 1) {
73
+ x /= 2;
74
+ exponent++;
75
+ }
76
+ if (arg < 0) {
77
+ x = -x;
78
+ }
79
+ return [x, exponent];
80
+ }
81
+ /**
82
+ * Adapted from the C code for CPython's float.as_integer_ratio()
83
+ */
84
+ // @ts-expect-error
85
+ function exact(num) {
86
+ const self_double = num;
87
+ // checks for inf and nan
88
+ let [float_part, exponent] = frexp(self_double); // self_double = float_part * (2 ** exponent) exactly
89
+ // TODO make that 300 non-magic
90
+ for (let i = 0; i < 300 && float_part !== Math.floor(float_part); i++) {
91
+ float_part *= 2;
92
+ exponent--;
93
+ }
94
+ // now float_part is an integer and self_double == float_part * (2 ** exponent) exactly
95
+ let numerator = BigInt(Math.floor(float_part));
96
+ let denominator = 1n;
97
+ if (exponent > 0) {
98
+ numerator <<= exponent;
99
+ }
100
+ else {
101
+ denominator <<= -exponent;
102
+ }
103
+ return [numerator, denominator];
104
+ }
105
+ /**
106
+ * Returns m and exp such that arg = m * (2 ** exp) exactly, and m and exp are (big)integers.
107
+ */
108
+ function frexpBig(arg) {
109
+ const absArg = Math.abs(arg);
110
+ if (absArg === 0) {
111
+ return [0n, 0n];
112
+ }
113
+ let exponent = BigInt(Math.max(-1023, Math.floor(Math.log2(absArg)) + 1));
114
+ let m_float = absArg / (2 ** Number(exponent));
115
+ while (m_float < 0.5) {
116
+ m_float *= 2;
117
+ exponent--;
118
+ }
119
+ while (m_float >= 1) {
120
+ m_float /= 2;
121
+ exponent++;
122
+ }
123
+ if (arg < 0) {
124
+ m_float = -m_float;
125
+ }
126
+ for (let i = 0; i < 300 && m_float !== Math.floor(m_float); i++) {
127
+ m_float *= 2;
128
+ exponent--;
129
+ }
130
+ return [BigInt(Math.floor(m_float)), exponent];
131
+ }
132
+ function exact2(num) {
133
+ const [m, exp] = frexpBig(num);
134
+ let numerator = m;
135
+ let denominator = 1n;
136
+ if (exp > 0) {
137
+ numerator <<= exp;
138
+ }
139
+ else {
140
+ denominator <<= -exp;
141
+ }
142
+ return [numerator, denominator];
143
+ }
144
+ export { exact2 as tupleFromNumber };
145
+ //# sourceMappingURL=fromNumber.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fromNumber.js","sourceRoot":"","sources":["../src/fromNumber.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH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, FractionAble, IrreducibleFraction } from "./interface.js";
2
+ import { stringize } from "./stringize.js";
3
+ export declare class FractionImpl implements Fraction {
4
+ readonly numerator: bigint;
5
+ readonly denominator: bigint;
6
+ constructor(numerator: bigint, denominator: bigint);
7
+ abs(): FractionImpl;
8
+ neg(): FractionImpl;
9
+ invert(): FractionImpl;
10
+ add(other: FractionAble): FractionImpl;
11
+ sub(other: FractionAble): FractionImpl;
12
+ mul(other: FractionAble): FractionImpl;
13
+ div(other: FractionAble): FractionImpl;
14
+ private compare;
15
+ compareTo(other: FractionAble): 1 | 0 | -1;
16
+ equals(other: FractionAble): boolean;
17
+ lt(other: FractionAble): boolean;
18
+ lte(other: FractionAble): boolean;
19
+ gt(other: FractionAble): boolean;
20
+ gte(other: FractionAble): boolean;
21
+ mod(n?: FractionAble): FractionImpl;
22
+ ceil(): bigint;
23
+ floor(): bigint;
24
+ round(): bigint;
25
+ roundTo(multiple: Fraction): FractionImpl;
26
+ asIrreducible(): IrreducibleFractionImpl;
27
+ limitDenominator(maxDenominator: bigint): IrreducibleFractionImpl;
28
+ /**
29
+ * Must only be called on a positive fraction.
30
+ */
31
+ private absToContinued;
32
+ private generateSimplifiedAbs;
33
+ simplify(error: FractionAble): Fraction;
34
+ protected toNumberIfSafe(): number | null;
35
+ valueOf(): number | bigint;
36
+ toString(): string;
37
+ [Symbol.toPrimitive](hint: "string"): string;
38
+ [Symbol.toPrimitive](hint: "number"): number;
39
+ [Symbol.toPrimitive](hint: "default"): number | bigint;
40
+ }
41
+ declare class IrreducibleFractionImpl extends FractionImpl implements IrreducibleFraction {
42
+ constructor(numerator: bigint, denominator: bigint);
43
+ asIrreducible(): IrreducibleFractionImpl;
44
+ valueOf(): number | bigint;
45
+ toString: typeof stringize;
46
+ }
47
+ /**
48
+ * Ready to be passed to Array.prototype.sort.
49
+ */
50
+ export declare function compare(a: FractionAble, b: FractionAble): number;
51
+ export {};
@@ -0,0 +1,316 @@
1
+ import { fromAny } from "./constructors.js";
2
+ import { gcd } from "./mathUtils.js";
3
+ import { stringize } from "./stringize.js";
4
+ export class FractionImpl {
5
+ constructor(numerator, denominator) {
6
+ this.numerator = numerator;
7
+ this.denominator = denominator;
8
+ if (denominator <= 0n) {
9
+ if (denominator === 0n) {
10
+ throw new Error("Denominator cannot be zero.");
11
+ }
12
+ throw new Error("Denominator must be positive.");
13
+ }
14
+ }
15
+ abs() {
16
+ return new FractionImpl(this.numerator < 0n ? -this.numerator : this.numerator, this.denominator);
17
+ }
18
+ neg() {
19
+ return new FractionImpl(-this.numerator, this.denominator);
20
+ }
21
+ invert() {
22
+ if (this.numerator > 0n) {
23
+ return new FractionImpl(this.denominator, this.numerator);
24
+ }
25
+ else {
26
+ return new FractionImpl(-this.denominator, -this.numerator);
27
+ }
28
+ }
29
+ add(other) {
30
+ if (typeof other === "bigint") {
31
+ return new FractionImpl(this.numerator + other * this.denominator, this.denominator);
32
+ }
33
+ other = fromAny(other);
34
+ return new FractionImpl(this.numerator * other.denominator + other.numerator * this.denominator, this.denominator * other.denominator);
35
+ }
36
+ sub(other) {
37
+ if (typeof other === "bigint") {
38
+ return this.add(-other);
39
+ }
40
+ // not a call to add because inverting the other requires a call to fromAny
41
+ other = fromAny(other);
42
+ return new FractionImpl(this.numerator * other.denominator - other.numerator * this.denominator, this.denominator * other.denominator);
43
+ }
44
+ mul(other) {
45
+ if (typeof other === "bigint") {
46
+ return new FractionImpl(this.numerator * other, this.denominator);
47
+ }
48
+ other = fromAny(other);
49
+ return new FractionImpl(this.numerator * other.numerator, this.denominator * other.denominator);
50
+ }
51
+ div(other) {
52
+ if (typeof other === "bigint") {
53
+ if (other === 0n) {
54
+ throw new Error("Division by zero.");
55
+ }
56
+ if (other > 0n) {
57
+ return new FractionImpl(this.numerator, this.denominator * other);
58
+ }
59
+ else {
60
+ return new FractionImpl(-this.numerator, -this.denominator * other);
61
+ }
62
+ }
63
+ other = fromAny(other);
64
+ if (other.numerator === 0n) {
65
+ throw new Error("Division by zero.");
66
+ }
67
+ let [otherN, otherD] = [other.numerator, other.denominator];
68
+ if (otherN < 0n) {
69
+ otherN = -otherN;
70
+ otherD = -otherD;
71
+ }
72
+ return new FractionImpl(this.numerator * otherD, this.denominator * otherN);
73
+ }
74
+ compare(other) {
75
+ return this.numerator * other.denominator - other.numerator * this.denominator;
76
+ }
77
+ compareTo(other) {
78
+ if (typeof other === "number") {
79
+ if (other === Infinity) {
80
+ return -1;
81
+ }
82
+ else if (other === -Infinity) {
83
+ return 1;
84
+ }
85
+ }
86
+ const c = this.compare(fromAny(other));
87
+ return c === 0n ? 0 : c > 0n ? 1 : -1;
88
+ }
89
+ equals(other) {
90
+ if (typeof other === "number" && !Number.isFinite(other)) {
91
+ return false;
92
+ }
93
+ return this.compare(fromAny(other)) === 0n;
94
+ }
95
+ lt(other) {
96
+ return compare(this, other) < 0n;
97
+ }
98
+ lte(other) {
99
+ return compare(this, other) <= 0n;
100
+ }
101
+ gt(other) {
102
+ return compare(this, other) > 0n;
103
+ }
104
+ gte(other) {
105
+ return compare(this, other) >= 0n;
106
+ }
107
+ mod(n) {
108
+ if (n === undefined) {
109
+ return new FractionImpl(this.numerator % this.denominator, 1n);
110
+ }
111
+ if (this.numerator < 0n) {
112
+ return this.neg().mod(n).neg();
113
+ }
114
+ const nf = fromAny(n);
115
+ if (nf.numerator === 0n) {
116
+ throw new Error("Modulo by zero.");
117
+ }
118
+ if (nf.numerator < 0n) {
119
+ return this.mod(nf.neg()).neg();
120
+ }
121
+ return new FractionImpl((nf.denominator * this.numerator) % (nf.numerator * this.denominator), nf.denominator * this.denominator);
122
+ }
123
+ ceil() {
124
+ return this.numerator / this.denominator +
125
+ BigInt(this.numerator % this.denominator !== 0n && this.numerator >= 0n);
126
+ }
127
+ floor() {
128
+ return this.numerator / this.denominator -
129
+ BigInt(this.numerator % this.denominator !== 0n && this.numerator < 0n);
130
+ }
131
+ round() {
132
+ // if (this.numerator === 0n) {
133
+ // return 0n;
134
+ // }
135
+ const sign = this.numerator >= 0n ? 1n : -1n;
136
+ const twiceMod = 2n * (sign * this.numerator % this.denominator);
137
+ const roundFarZero = sign !== -1n ?
138
+ twiceMod >= this.denominator :
139
+ twiceMod > this.denominator; // JS rule : towards +Inf, not away from zero
140
+ return (this.numerator / this.denominator) + sign * BigInt(roundFarZero);
141
+ // return this.numerator / this.denominator +
142
+ // sign * (BigInt(this.numerator >= 0n) +
143
+ // BigInt((2n * ((this.numerator*sign) % this.denominator)) > this.denominator));
144
+ }
145
+ roundTo(multiple) {
146
+ const n = this.numerator * multiple.denominator;
147
+ const d = this.denominator * multiple.numerator;
148
+ const k = n / d + BigInt(2n * (n % d) >= d);
149
+ return new FractionImpl(k * multiple.numerator, multiple.denominator);
150
+ }
151
+ asIrreducible() {
152
+ const g = gcd(this.numerator, this.denominator);
153
+ if (g >= 0n) {
154
+ return new IrreducibleFractionImpl(this.numerator / g, this.denominator / g);
155
+ }
156
+ else {
157
+ return new IrreducibleFractionImpl(-this.numerator / g, -this.denominator / g);
158
+ }
159
+ }
160
+ limitDenominator(maxDenominator) {
161
+ if (maxDenominator < 1) {
162
+ throw new Error("Maximum denominator must be at least 1.");
163
+ }
164
+ if (this.denominator <= maxDenominator) {
165
+ return this;
166
+ }
167
+ const sign = this.numerator < 0n ? -1n : 1n;
168
+ let [p0, q0, p1, q1] = [0n, 1n, 1n, 0n];
169
+ let [n, d] = [sign * this.numerator, this.denominator];
170
+ while (true) {
171
+ const a = n / d;
172
+ const q2 = q0 + a * q1;
173
+ if (q2 > maxDenominator) {
174
+ break;
175
+ }
176
+ [p0, q0, p1, q1] = [p1, q1, p0 + a * p1, q2];
177
+ [n, d] = [d, n - a * d];
178
+ }
179
+ const k = (maxDenominator - q0) / q1;
180
+ if (2n * d * (q0 + k * q1) <= this.denominator) {
181
+ return new IrreducibleFractionImpl(sign * p1, q1);
182
+ }
183
+ else {
184
+ return new IrreducibleFractionImpl(sign * (p0 + k * p1), q0 + k * q1);
185
+ }
186
+ }
187
+ /**
188
+ * Must only be called on a positive fraction.
189
+ */
190
+ *absToContinued() {
191
+ let a = this.numerator;
192
+ let b = this.denominator;
193
+ do {
194
+ yield a / b;
195
+ [a, b] = [b, a % b];
196
+ } while (a !== 1n);
197
+ }
198
+ *generateSimplifiedAbs() {
199
+ const cont = Array.from(this.abs().absToContinued());
200
+ for (let i = 1; i < cont.length; i++) {
201
+ let s = new FractionImpl(cont[i - 1], 1n);
202
+ for (let j = i - 2; j >= 0; j--) {
203
+ s = s.invert().add(cont[j]);
204
+ }
205
+ yield s;
206
+ }
207
+ }
208
+ simplify(error) {
209
+ // takes the simplest functions from the continued,
210
+ // until it is within the required error gap
211
+ const eps = fromAny(error).abs();
212
+ for (const s of this.generateSimplifiedAbs()) {
213
+ const diff = s.sub(this).abs();
214
+ if (diff.lt(eps)) {
215
+ if (this.numerator < 0n) {
216
+ return s.neg();
217
+ }
218
+ return s;
219
+ }
220
+ }
221
+ return this; // if nothing found, return the absolute value
222
+ }
223
+ toNumberIfSafe() {
224
+ if (this.numerator <= Number.MAX_SAFE_INTEGER && this.denominator <= Number.MAX_SAFE_INTEGER) {
225
+ return Number(this.numerator) / Number(this.denominator);
226
+ }
227
+ return null; // not safe to convert
228
+ }
229
+ valueOf() {
230
+ if (this.denominator === 1n) {
231
+ return this.numerator;
232
+ }
233
+ if (this.numerator < 0n) {
234
+ return -this.neg().valueOf();
235
+ }
236
+ // if safe, cast the two operands then divide them.
237
+ let safeNum = this.toNumberIfSafe();
238
+ if (safeNum !== null) {
239
+ return safeNum;
240
+ }
241
+ // otherwise, first try to reduce to an irreducible fraction then work with that.
242
+ return this.asIrreducible().valueOf();
243
+ }
244
+ toString() {
245
+ return this.asIrreducible().toString();
246
+ }
247
+ [Symbol.toPrimitive](hint) {
248
+ if (hint === "string") {
249
+ return this.toString();
250
+ }
251
+ const value = this.valueOf();
252
+ if (hint === "number") { // always returns a number (incl. Infinity), not a bigint
253
+ return Number(value);
254
+ }
255
+ return value; // default : bigint or number
256
+ }
257
+ }
258
+ class IrreducibleFractionImpl extends FractionImpl {
259
+ constructor(numerator, denominator) {
260
+ super(numerator, denominator);
261
+ this.toString = stringize;
262
+ }
263
+ asIrreducible() {
264
+ return this; // already irreducible
265
+ }
266
+ valueOf() {
267
+ if (this.denominator === 1n) {
268
+ return this.numerator;
269
+ }
270
+ // if safe, cast the two operands then divide them.
271
+ let safeNum = this.toNumberIfSafe();
272
+ if (safeNum !== null) {
273
+ return safeNum;
274
+ }
275
+ if (this.numerator > this.denominator) {
276
+ // sum the integer part with the fractional part, guaranteeing that the integral part is correct.
277
+ const int = this.numerator / this.denominator;
278
+ const nDenom = Number(this.denominator);
279
+ const remainder = (Number(this.numerator) % nDenom) / nDenom;
280
+ if (remainder === 0 || !Number.isFinite(remainder)) {
281
+ return int;
282
+ }
283
+ return Number(int) + remainder;
284
+ }
285
+ // reduce the denominator by a factor of 2 and try again
286
+ if (this.denominator > 2n) {
287
+ return this.limitDenominator(this.denominator >> 1n).valueOf();
288
+ }
289
+ // if that's still out of bounds, give up
290
+ return Number(this.numerator) / Number(this.denominator);
291
+ // if one of the values is out of f64 bounds, as a number it will be infinity,
292
+ // and the result will be infinity.
293
+ // if both are, the result will be NaN.
294
+ }
295
+ }
296
+ /**
297
+ * Ready to be passed to Array.prototype.sort.
298
+ */
299
+ export function compare(a, b) {
300
+ if (typeof a === "number") {
301
+ if (a === Infinity) {
302
+ if (b === Infinity) {
303
+ return 0;
304
+ }
305
+ return 1;
306
+ }
307
+ else if (a === -Infinity) {
308
+ if (b === -Infinity) {
309
+ return 0;
310
+ }
311
+ return -1;
312
+ }
313
+ }
314
+ return fromAny(a).compareTo(b);
315
+ }
316
+ //# sourceMappingURL=implcls.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"implcls.js","sourceRoot":"","sources":["../src/implcls.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAE5C,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AACrC,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jD,OAAO,GAAG,CAAC;YACf,CAAC;YACD,OAAO,MAAM,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC;QACnC,CAAC;QAED,wDAAwD;QACxD,IAAI,IAAI,CAAC,WAAW,GAAG,EAAE,EAAE,CAAC;YACxB,OAAO,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,WAAW,IAAE,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC;QACjE,CAAC;QAED,yCAAyC;QACzC,OAAO,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;QACzD,8EAA8E;QAC9E,mCAAmC;QACnC,uCAAuC;IAC3C,CAAC;CAEJ;AAED;;GAEG;AACH,MAAM,UAAU,OAAO,CAAC,CAAe,EAAE,CAAe;IACpD,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE,CAAC;QACxB,IAAI,CAAC,KAAK,QAAQ,EAAE,CAAC;YACjB,IAAI,CAAC,KAAK,QAAQ,EAAE,CAAC;gBACjB,OAAO,CAAC,CAAC;YACb,CAAC;YACD,OAAO,CAAC,CAAC;QACb,CAAC;aAAM,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC;YACzB,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC;gBAClB,OAAO,CAAC,CAAC;YACb,CAAC;YACD,OAAO,CAAC,CAAC,CAAC;QACd,CAAC;IACL,CAAC;IAED,OAAO,OAAO,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;AACnC,CAAC"}
@@ -0,0 +1,21 @@
1
+ import type { FractionAble, Fraction as IFraction } from "./interface.js";
2
+ import { compare } from "./implcls.js";
3
+ export type { IrreducibleFraction, FractionAble } from "./interface.js";
4
+ export type Fraction = IFraction;
5
+ type MainType = {
6
+ (obj: FractionAble): IFraction;
7
+ (a: bigint | number, b: bigint | number): IFraction;
8
+ (a: FractionAble, b?: bigint | number): IFraction;
9
+ fromNumeric(num: bigint | number): IFraction;
10
+ fromString(str: string): IFraction;
11
+ fromPair(num: bigint | number, denom: bigint | number): IFraction;
12
+ fromTuple([num, denom]: [bigint | number, bigint | number]): IFraction;
13
+ fromObject({ numerator, denominator }: {
14
+ numerator: bigint;
15
+ denominator: bigint;
16
+ }): IFraction;
17
+ fromAny(obj: FractionAble): IFraction;
18
+ compare: typeof compare;
19
+ };
20
+ declare const main: MainType;
21
+ export { main as Fraction };
package/dist/index.js ADDED
@@ -0,0 +1,24 @@
1
+ import * as constructors from "./constructors.js";
2
+ import { compare } from "./implcls.js";
3
+ const main = (() => {
4
+ function mainConstructor(a, b) {
5
+ if (b === undefined) {
6
+ return constructors.fromAny(a);
7
+ }
8
+ else {
9
+ return constructors.fromPair(a, b);
10
+ }
11
+ }
12
+ mainConstructor.fromNumeric = constructors.fromNumeric;
13
+ mainConstructor.fromString = constructors.fromString;
14
+ mainConstructor.fromPair = constructors.fromPair;
15
+ mainConstructor.fromTuple = constructors.fromTuple;
16
+ mainConstructor.fromObject = constructors.fromObject;
17
+ mainConstructor.fromAny = constructors.fromAny;
18
+ mainConstructor.compare = compare;
19
+ return mainConstructor;
20
+ })();
21
+ // export const Fraction = main;
22
+ export { main as Fraction };
23
+ // export default main;
24
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,YAAY,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAwBvC,MAAM,IAAI,GAAG,CAAC,GAAa,EAAE;IAGzB,SAAS,eAAe,CAAC,CAAe,EAAE,CAAiB;QACvD,IAAI,CAAC,KAAK,SAAS,EAAE,CAAC;YAClB,OAAO,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;QACnC,CAAC;aAAM,CAAC;YACJ,OAAO,YAAY,CAAC,QAAQ,CAAC,CAAkB,EAAE,CAAC,CAAC,CAAC;QACxD,CAAC;IACL,CAAC;IACD,eAAe,CAAC,WAAW,GAAG,YAAY,CAAC,WAAW,CAAC;IACvD,eAAe,CAAC,UAAU,GAAG,YAAY,CAAC,UAAU,CAAC;IACrD,eAAe,CAAC,QAAQ,GAAG,YAAY,CAAC,QAAQ,CAAC;IACjD,eAAe,CAAC,SAAS,GAAG,YAAY,CAAC,SAAS,CAAC;IACnD,eAAe,CAAC,UAAU,GAAG,YAAY,CAAC,UAAU,CAAC;IACrD,eAAe,CAAC,OAAO,GAAG,YAAY,CAAC,OAAO,CAAC;IAE/C,eAAe,CAAC,OAAO,GAAG,OAAO,CAAC;IAClC,OAAO,eAAe,CAAC;AAC3B,CAAC,CAAC,EAAE,CAAC;AACL,gCAAgC;AAChC,OAAO,EAAE,IAAI,IAAI,QAAQ,EAAE,CAAC;AAC5B,uBAAuB"}
@@ -0,0 +1,54 @@
1
+ export type FractionAble = Fraction | bigint | number | string | [bigint | number, bigint | number] | {
2
+ numerator: bigint;
3
+ denominator: bigint;
4
+ };
5
+ export interface Fraction {
6
+ readonly numerator: bigint;
7
+ readonly denominator: bigint;
8
+ abs(): Fraction;
9
+ neg(): Fraction;
10
+ invert(): Fraction;
11
+ add(other: FractionAble): Fraction;
12
+ sub(other: FractionAble): Fraction;
13
+ mul(other: FractionAble): Fraction;
14
+ div(other: FractionAble): Fraction;
15
+ compareTo(other: FractionAble): number;
16
+ equals(other: FractionAble): boolean;
17
+ lt(other: FractionAble): boolean;
18
+ lte(other: FractionAble): boolean;
19
+ gt(other: FractionAble): boolean;
20
+ gte(other: FractionAble): boolean;
21
+ /**
22
+ * Returns the modulo of the numerator with respect to the denominator.
23
+ */
24
+ mod(): Fraction;
25
+ /**
26
+ * Returns the modulo of the fraction with respect to another fraction or number.
27
+ */
28
+ mod(n: FractionAble): Fraction;
29
+ ceil(): bigint;
30
+ floor(): bigint;
31
+ round(): bigint;
32
+ roundTo(multiple: Fraction): Fraction;
33
+ /**
34
+ * Converts the fraction to an irreducible form.
35
+ */
36
+ asIrreducible(): IrreducibleFraction;
37
+ /**
38
+ * Limits the denominator of the fraction to a maximum value.
39
+ * @param maxDenominator The maximum allowed denominator.
40
+ */
41
+ limitDenominator(maxDenominator: bigint): Fraction;
42
+ /**
43
+ * Simplifies the fraction to lower terms.
44
+ * @param precision The maximum allowed error for the simplification.
45
+ */
46
+ simplify(precision: FractionAble): Fraction;
47
+ valueOf(): number | bigint;
48
+ toString(): string;
49
+ [Symbol.toPrimitive](hint: "string"): string;
50
+ [Symbol.toPrimitive](hint: "number"): number;
51
+ [Symbol.toPrimitive](hint: "default"): number | bigint;
52
+ }
53
+ export interface IrreducibleFraction extends Fraction {
54
+ }
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=interface.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interface.js","sourceRoot":"","sources":["../src/interface.ts"],"names":[],"mappings":""}
@@ -0,0 +1 @@
1
+ export declare function gcd(a: bigint, b: bigint): bigint;
@@ -0,0 +1,15 @@
1
+ export function gcd(a, b) {
2
+ if (!a)
3
+ return b;
4
+ if (!b)
5
+ return a;
6
+ while (true) {
7
+ a %= b;
8
+ if (!a)
9
+ return b;
10
+ b %= a;
11
+ if (!b)
12
+ return a;
13
+ }
14
+ }
15
+ //# sourceMappingURL=mathUtils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mathUtils.js","sourceRoot":"","sources":["../src/mathUtils.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,GAAG,CAAC,CAAS,EAAE,CAAS;IACpC,IAAI,CAAC,CAAC;QAAE,OAAO,CAAC,CAAC;IACjB,IAAI,CAAC,CAAC;QAAE,OAAO,CAAC,CAAC;IAEjB,OAAO,IAAI,EAAE,CAAC;QACV,CAAC,IAAI,CAAC,CAAC;QACP,IAAI,CAAC,CAAC;YAAE,OAAO,CAAC,CAAC;QACjB,CAAC,IAAI,CAAC,CAAC;QACP,IAAI,CAAC,CAAC;YAAE,OAAO,CAAC,CAAC;IACrB,CAAC;AACL,CAAC"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * A less efficient version (using several regexes), but more readable and more permissive.
3
+ */
4
+ declare function parseRevamp(str: string): [bigint, bigint];
5
+ export { parseRevamp as parse };
package/dist/parse.js ADDED
@@ -0,0 +1,123 @@
1
+ // @ts-expect-error
2
+ function parseLegacy(str) {
3
+ let sign = 1n;
4
+ let ndx = 0;
5
+ let intPart = 0n, nonRepeatingPart = 0n, repeatingPart = 0n, nonRepeatingDenominator = 1n, repeatingDenominator = 1n;
6
+ const match = str.replace(/_/g, "").match(/\d+|./g);
7
+ if (!match) {
8
+ throw new TypeError(`Cannot parse "${str}" as a fraction.`);
9
+ }
10
+ if (match[0] === "-") {
11
+ sign = -1n;
12
+ ndx++;
13
+ }
14
+ else if (match[0] === "+") {
15
+ ndx++;
16
+ }
17
+ if (match.length === ndx + 1) {
18
+ nonRepeatingPart = BigInt(match[ndx++]) * sign;
19
+ }
20
+ else if (match[ndx + 1] === "." || match[ndx] === ".") {
21
+ // if it's a decimal number
22
+ if (match[ndx] !== ".") {
23
+ intPart = BigInt(match[ndx++]) * sign;
24
+ }
25
+ ndx++;
26
+ if (ndx + 1 === match.length
27
+ || match[ndx + 1] === "(" && match[ndx + 3] === ")"
28
+ || match[ndx + 1] === "'" && match[ndx + 3] === "'") {
29
+ nonRepeatingPart = BigInt(match[ndx]) * sign;
30
+ nonRepeatingDenominator = 10n ** BigInt(match[ndx].length);
31
+ ndx++;
32
+ }
33
+ if (match[ndx] === "(" || match[ndx + 2] === ")"
34
+ || match[ndx] === "'" || match[ndx + 2] === "'") {
35
+ intPart = BigInt(match[ndx]) * sign;
36
+ nonRepeatingPart = BigInt(match[ndx + 2]) * sign;
37
+ nonRepeatingDenominator = BigInt(match[ndx + 4]);
38
+ ndx += 5;
39
+ }
40
+ }
41
+ else if (match[ndx + 1] === "/" || match[ndx + 1] === ":") {
42
+ nonRepeatingPart = BigInt(match[ndx]) * sign;
43
+ nonRepeatingDenominator = BigInt(match[ndx + 2]);
44
+ ndx += 3;
45
+ }
46
+ else if (match[ndx + 3] === "/" && match[ndx + 1] === " ") {
47
+ intPart = BigInt(match[ndx]) * sign;
48
+ nonRepeatingPart = BigInt(match[ndx + 2]) * sign;
49
+ nonRepeatingDenominator = BigInt(match[ndx + 4]);
50
+ ndx += 5;
51
+ }
52
+ if (match.length <= ndx) {
53
+ const denominator = nonRepeatingDenominator * repeatingDenominator;
54
+ const numerator = repeatingPart + denominator * intPart + repeatingDenominator * nonRepeatingPart;
55
+ return [numerator, denominator];
56
+ }
57
+ else {
58
+ throw new TypeError(`Cannot parse "${str}" as a fraction.`);
59
+ }
60
+ }
61
+ // decimal forms
62
+ const decimalInt = /^(\d+)\.?$/; // 123. or 123
63
+ // decimal with repeating part
64
+ const decimalRepeatingFloat = /^(\d*)\.(\d*)(?:('|\()(\d+)('|\)))?$/;
65
+ // fraction forms
66
+ /^\d+[/:]\d+$/; // 123\456 or 123:456
67
+ // with a preceding whole number
68
+ const fractionWithWhole = /^(?:(\d+) )?(\d+)[/:](\d+)$/; // 123 456/789 or 123 456:789
69
+ /**
70
+ * A less efficient version (using several regexes), but more readable and more permissive.
71
+ */
72
+ function parseRevamp(str) {
73
+ let sign = 1n;
74
+ if (str[0] === "-") {
75
+ sign = -1n;
76
+ str = str.slice(1);
77
+ }
78
+ else if (str[0] === "+") {
79
+ str = str.slice(1);
80
+ }
81
+ let r;
82
+ if (r = decimalInt.exec(str)) {
83
+ return [sign * BigInt(r[1]), 1n];
84
+ }
85
+ if (r = decimalRepeatingFloat.exec(str)) {
86
+ const intPart = r[1] ? BigInt(r[1]) : 0n;
87
+ if (r[3] === undefined) {
88
+ // no repeating part
89
+ if (!r[2] && !r[1]) {
90
+ // just a dot, not valid
91
+ throw new TypeError(`Cannot parse "${str}" as a fraction: no digits found.`);
92
+ }
93
+ const fracPart = BigInt(r[2]);
94
+ const denominator = 10n ** BigInt(r[2].length);
95
+ return [sign * (intPart * denominator + fracPart), denominator];
96
+ }
97
+ else if ((r[3] === "'") !== (r[5] === "'")) {
98
+ // mismatched repeating enclosing
99
+ throw new TypeError(`Cannot parse "${str}" as a fraction: mismatched repeating enclosing.`);
100
+ }
101
+ else {
102
+ const nonRepeatingPart = BigInt(r[2]);
103
+ const nonRepeatingDenominator = 10n ** BigInt(r[2].length);
104
+ const repeatingPart = BigInt(r[4]);
105
+ const repeatingDenominator = 10n ** BigInt(r[4].length) - 1n;
106
+ const denominator = nonRepeatingDenominator * repeatingDenominator;
107
+ const numerator = repeatingPart + denominator * intPart + repeatingDenominator * nonRepeatingPart;
108
+ return [sign * numerator, denominator];
109
+ }
110
+ }
111
+ if (r = fractionWithWhole.exec(str)) {
112
+ const wholePart = r[1] ? BigInt(r[1]) : 0n;
113
+ const numerator = BigInt(r[2]);
114
+ const denominator = BigInt(r[3]);
115
+ if (denominator === 0n) {
116
+ throw new TypeError(`Cannot parse "${str}" as a fraction: denominator cannot be zero.`);
117
+ }
118
+ return [sign * (wholePart * denominator + numerator), denominator];
119
+ }
120
+ throw new TypeError(`Cannot parse "${str}" as a fraction: no match.`);
121
+ }
122
+ export { parseRevamp as parse };
123
+ //# sourceMappingURL=parse.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse.js","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AAAA,mBAAmB;AACnB,SAAS,WAAW,CAAC,GAAW;IAC5B,IAAI,IAAI,GAAG,EAAE,CAAC;IACd,IAAI,GAAG,GAAG,CAAC,CAAC;IAEZ,IAAI,OAAO,GAAG,EAAE,EACZ,gBAAgB,GAAG,EAAE,EACrB,aAAa,GAAG,EAAE,EAClB,uBAAuB,GAAG,EAAE,EAC5B,oBAAoB,GAAG,EAAE,CAAC;IAE9B,MAAM,KAAK,GAAG,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IAEpD,IAAI,CAAC,KAAK,EAAE,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CAAC,CAAC;YACpC,MAAM,oBAAoB,GAAG,GAAG,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC,CAAE,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC;YAC9D,MAAM,WAAW,GAAG,uBAAuB,GAAG,oBAAoB,CAAC;YACnE,MAAM,SAAS,GAAG,aAAa,GAAG,WAAW,GAAG,OAAO,GAAG,oBAAoB,GAAG,gBAAgB,CAAC;YAClG,OAAO,CAAC,IAAI,GAAE,SAAS,EAAE,WAAW,CAAC,CAAC;QAC1C,CAAC;IACL,CAAC;IAED,IAAI,CAAC,GAAG,iBAAiB,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;QAClC,MAAM,SAAS,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAC3C,MAAM,SAAS,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC,CAAE,CAAC,CAAC;QAChC,MAAM,WAAW,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC,CAAE,CAAC,CAAC;QAClC,IAAI,WAAW,KAAK,EAAE,EAAE,CAAC;YACrB,MAAM,IAAI,SAAS,CAAC,iBAAiB,GAAG,8CAA8C,CAAC,CAAC;QAC5F,CAAC;QACD,OAAO,CAAC,IAAI,GAAE,CAAC,SAAS,GAAG,WAAW,GAAG,SAAS,CAAC,EAAE,WAAW,CAAC,CAAC;IACtE,CAAC;IAED,MAAM,IAAI,SAAS,CAAC,iBAAiB,GAAG,4BAA4B,CAAC,CAAC;AAC1E,CAAC;AAED,OAAO,EAAE,WAAW,IAAI,KAAK,EAAE,CAAC"}
@@ -0,0 +1,2 @@
1
+ import { Fraction } from "./interface.js";
2
+ export declare function stringize(this: Fraction, dec?: bigint): string;
@@ -0,0 +1,78 @@
1
+ const MAX_CYCLE_LENGTH = 2000n;
2
+ function modPow(base, exp, mod) {
3
+ let result = 1n;
4
+ while (exp > 0n) {
5
+ if (exp & 1n) {
6
+ result = (result * base) % mod;
7
+ }
8
+ base = (base * base) % mod;
9
+ exp >>= 1n;
10
+ }
11
+ return result;
12
+ }
13
+ function cycleLen(denominator) {
14
+ while (denominator % 2n === 0n)
15
+ denominator /= 2n;
16
+ while (denominator % 5n === 0n)
17
+ denominator /= 5n;
18
+ if (denominator === 1n)
19
+ return 0n;
20
+ let rem = 10n % denominator;
21
+ let t = 1n;
22
+ while (rem !== 1n) {
23
+ rem = (rem * 10n) % denominator;
24
+ t++;
25
+ if (t > MAX_CYCLE_LENGTH) {
26
+ return 0n; // meaning that it's not printed as a cyclic number, the answer is likely denominator-1
27
+ }
28
+ }
29
+ return t;
30
+ }
31
+ function cycleStart(denominator, cycleLength) {
32
+ let rem1 = 1n;
33
+ let rem2 = modPow(10n, cycleLength, denominator);
34
+ for (let t = 0n; t < 300n; t++) {
35
+ if (rem1 === rem2) {
36
+ return t;
37
+ }
38
+ rem1 = (rem1 * 10n) % denominator;
39
+ rem2 = (rem2 * 10n) % denominator;
40
+ }
41
+ return 0n;
42
+ }
43
+ export function stringize(dec = 15n) {
44
+ let N = this.numerator < 0n ? -this.numerator : this.numerator;
45
+ let D = this.denominator;
46
+ const cyclen = cycleLen(D);
47
+ const cycoff = cycleStart(D, cyclen);
48
+ let str = (this.numerator < 0n ? "-" : "")
49
+ + (N / D).toString();
50
+ N %= D;
51
+ N *= 10n;
52
+ if (N) {
53
+ str += ".";
54
+ }
55
+ if (cyclen) {
56
+ for (let i = cycoff; i--;) {
57
+ str += (N / D).toString();
58
+ N %= D;
59
+ N *= 10n;
60
+ }
61
+ str += "(";
62
+ for (let i = cyclen; i--;) {
63
+ str += (N / D).toString();
64
+ N %= D;
65
+ N *= 10n;
66
+ }
67
+ str += ")";
68
+ }
69
+ else {
70
+ for (let i = dec; N && i--;) {
71
+ str += (N / D).toString();
72
+ N %= D;
73
+ N *= 10n;
74
+ }
75
+ }
76
+ return str;
77
+ }
78
+ //# s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,CAAC,IAAI,CAAC,CAAC;YACP,CAAC,IAAI,GAAG,CAAC;QACb,CAAC;QACD,GAAG,IAAI,GAAG,CAAC;QACX,KAAK,IAAI,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,GAAG,CAAC;YACxB,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC;YAC1B,CAAC,IAAI,CAAC,CAAC;YACP,CAAC,IAAI,GAAG,CAAC;QACb,CAAC;QACD,GAAG,IAAI,GAAG,CAAC;IACf,CAAC;SAAM,CAAC;QACJ,KAAK,IAAI,CAAC,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC;YAC1B,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC;YAC1B,CAAC,IAAI,CAAC,CAAC;YACP,CAAC,IAAI,GAAG,CAAC;QACb,CAAC;IACL,CAAC;IACD,OAAO,GAAG,CAAC;AACf,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gouvernathor/fraction.ts",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "A rational numbers library written in TypeScript",
5
5
  "keywords": [
6
6
  "math",