/
Fraction.ts
477 lines (423 loc) · 14.5 KB
/
Fraction.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
// TODO: Check the operators' names
// TODO: This class should probably be immutable?
/**
* A class representing mathematical fractions, which have a numerator and a denominator.
*/
export class Fraction {
private static maximumAllowedNumber: number = 46340; // sqrt(int.Max) --> signed int with 4 bytes (2^31)
private numerator: number = 0;
private denominator: number = 1;
private wholeValue: number = 0;
private realValue: number;
/**
* Returns the maximum of two fractions (does not clone)
* @param f1
* @param f2
* @returns {Fraction}
*/
public static max(f1: Fraction, f2: Fraction): Fraction {
if (f1.RealValue > f2.RealValue) {
return f1;
} else {
return f2;
}
}
public static Equal(f1: Fraction, f2: Fraction): boolean {
return f1.wholeValue === f2.wholeValue && f1.Denominator === f2.Denominator && f1.Numerator === f2.Numerator;
}
/**
* The same as Fraction.clone
* @param fraction
* @returns {Fraction}
*/
public static createFromFraction(fraction: Fraction): Fraction {
return new Fraction(fraction.numerator, fraction.denominator, fraction.wholeValue, false);
}
public static plus(f1: Fraction, f2: Fraction): Fraction {
const sum: Fraction = f1.clone();
sum.Add(f2);
return sum;
}
public static minus(f1: Fraction, f2: Fraction): Fraction {
const sum: Fraction = f1.clone();
sum.Sub(f2);
return sum;
}
public static multiply (f1: Fraction, f2: Fraction): Fraction {
return new Fraction ( (f1.wholeValue * f1.denominator + f1.numerator) * (f2.wholeValue * f2.denominator + f2.numerator),
f1.denominator * f2.denominator);
}
private static greatestCommonDenominator(a: number, b: number): number {
if (a === 0) {
return b;
}
if (b === 1) {
return 1;
}
while (b !== 0) {
if (a > b) {
a -= b;
} else {
b -= a;
}
}
return a;
}
/**
*
* @param numerator
* @param denominator
* @param wholeValue - the integer number, needed for values greater than 1
* @param simplify - If simplify is true, then the fraction is simplified
* to make both the numerator and denominator coprime, and less than maximumAllowedNumber.
*/
constructor(numerator: number = 0, denominator: number = 1, wholeValue: number = 0, simplify: boolean = true) {
this.numerator = numerator;
this.denominator = denominator;
this.wholeValue = wholeValue;
if (simplify) {
this.simplify();
}
this.setRealValue();
}
public toString(): string {
let result: string = this.numerator + "/" + this.denominator;
if (this.wholeValue !== 0) {
result = this.wholeValue + " " + result;
}
return result;
}
public clone(): Fraction {
return new Fraction(this.numerator, this.denominator, this.wholeValue, false);
}
public get Numerator(): number {
return this.numerator;
}
public set Numerator(value: number) {
if (this.numerator !== value) {
this.numerator = value;
this.simplify();
this.setRealValue();
}
}
public get Denominator(): number {
return this.denominator;
}
public set Denominator(value: number) {
if (this.denominator !== value) {
this.denominator = value;
// don't simplify in case of a GraceNote (need it in order to set the right symbol)
if (this.numerator !== 0) {
this.simplify();
}
this.setRealValue();
}
}
public get WholeValue(): number {
return this.wholeValue;
}
public set WholeValue(value: number) {
if (this.wholeValue !== value) {
this.wholeValue = value;
this.setRealValue();
}
}
/**
* Returns the unified numerator where the whole value will be expanded
* with the denominator and added to the existing numerator.
*/
public GetExpandedNumerator(): number {
return this.wholeValue * this.denominator + this.numerator;
}
public IsNegative(): boolean {
return this.realValue < 0;
}
public get RealValue(): number {
return this.realValue;
}
public expand(expansionValue: number): void {
this.numerator *= expansionValue;
this.denominator *= expansionValue;
if (this.wholeValue !== 0) {
this.numerator += this.wholeValue * this.denominator;
this.wholeValue = 0;
}
}
// public multiplyDenominatorWithFactor(factor: number): void {
// this.denominator *= factor;
// this.setRealValue();
// }
/**
* Adds a Fraction to this Fraction.
* Attention: This changes the already existing Fraction, which might be referenced elsewhere!
* Use Fraction.plus() for creating a new Fraction object being the sum of two Fractions.
* @param fraction the Fraction to add.
*/
public Add(fraction: Fraction): void {
// normally should check if denominator or fraction.denominator is 0 but in our case
// a zero denominator doesn't make sense
this.numerator = (this.wholeValue * this.denominator + this.numerator) * fraction.denominator +
(fraction.wholeValue * fraction.denominator + fraction.numerator) * this.denominator;
this.denominator = this.denominator * fraction.denominator;
this.wholeValue = 0;
this.simplify();
this.setRealValue();
}
/**
* Subtracts a Fraction from this Fraction.
* Attention: This changes the already existing Fraction, which might be referenced elsewhere!
* Use Fraction.minus() for creating a new Fraction object being the difference of two Fractions.
* @param fraction the Fraction to subtract.
*/
public Sub(fraction: Fraction): void {
// normally should check if denominator or fraction.denominator is 0 but in our case
// a zero denominator doesn't make sense
this.numerator = (this.wholeValue * this.denominator + this.numerator) * fraction.denominator -
(fraction.wholeValue * fraction.denominator + fraction.numerator) * this.denominator;
this.denominator = this.denominator * fraction.denominator;
this.wholeValue = 0;
this.simplify();
this.setRealValue();
}
/**
* Brute Force quanization by searching incremental with the numerator until the denominator is
* smaller/equal than the desired one.
* @param maxAllowedDenominator
*/
public Quantize(maxAllowedDenominator: number): Fraction {
if (this.denominator <= maxAllowedDenominator) {
return this;
}
const upTestFraction: Fraction = new Fraction(this.numerator + 1, this.denominator, this.wholeValue);
while (upTestFraction.Denominator > maxAllowedDenominator) {
upTestFraction.Numerator++;
}
if (this.numerator > this.denominator) {
const downTestFraction: Fraction = new Fraction(this.numerator - 1, this.denominator, this.wholeValue);
while (downTestFraction.Denominator > maxAllowedDenominator) {
downTestFraction.Numerator--;
}
if (downTestFraction.Denominator < upTestFraction.Denominator) {
return downTestFraction;
}
}
return upTestFraction;
}
public Equals(obj: Fraction): boolean {
return this.realValue === obj.realValue;
}
public CompareTo(obj: Fraction): number {
const diff: number = this.realValue - obj.realValue;
// Return the sign of diff
return diff ? diff < 0 ? -1 : 1 : 0;
}
public lt(frac: Fraction): boolean {
return this.realValue < frac.realValue;
}
public lte(frac: Fraction): boolean {
return this.realValue <= frac.realValue;
}
public gt(frac: Fraction): boolean {
return !this.lte(frac);
}
public gte(frac: Fraction): boolean {
return !this.lt(frac);
}
//public Equals(f: Fraction): boolean {
// if (ReferenceEquals(this, f))
// return true;
// if (ReferenceEquals(f, undefined))
// return false;
// return this.numerator * f.denominator === f.numerator * this.denominator;
//}
private setRealValue(): void {
this.realValue = this.wholeValue + this.numerator / this.denominator;
}
private simplify(): void {
// don't simplify in case of a GraceNote (need it in order to set the right symbol)
if (this.numerator === 0) {
this.denominator = 1;
return;
}
// normally should check if denominator or fraction.denominator is 0 but in our case a zero denominator
// doesn't make sense. Could probably be optimized
const i: number = Fraction.greatestCommonDenominator(Math.abs(this.numerator), Math.abs(this.denominator));
this.numerator /= i;
this.denominator /= i;
const whole: number = Math.floor(this.numerator / this.denominator);
if (whole !== 0) {
this.wholeValue += whole;
this.numerator -= whole * this.denominator;
if (this.numerator === 0) {
this.denominator = 1;
}
}
if (this.denominator > Fraction.maximumAllowedNumber) {
const factor: number = this.denominator / Fraction.maximumAllowedNumber;
this.numerator = Math.round(this.numerator / factor);
this.denominator = Math.round(this.denominator / factor);
}
if (this.numerator > Fraction.maximumAllowedNumber) {
const factor: number = this.numerator / Fraction.maximumAllowedNumber;
this.numerator = Math.round(this.numerator / factor);
this.denominator = Math.round(this.denominator / factor);
}
}
public static FloatInaccuracyTolerance: number = 0.0001; // inaccuracy allowed when comparing Fraction.RealValues, because of floating point inaccuracy
public isOnBeat(timeSignature: Fraction): boolean { // use sourceMeasure.ActiveTimeSignature as timeSignature
const beatDistance: number = this.distanceFromBeat(timeSignature);
return Math.abs(beatDistance) < Fraction.FloatInaccuracyTolerance;
}
public distanceFromBeat(timeSignature: Fraction): number {
const beatStep: Fraction = new Fraction(1, timeSignature.Denominator);
const distanceFromBeat: number = this.RealValue % beatStep.RealValue; // take modulo the beat value, e.g. 1/8 in a 3/8 time signature
return distanceFromBeat;
}
//private static equals(f1: Fraction, f2: Fraction): boolean {
// return f1.numerator * f2.denominator === f2.numerator * f1.denominator;
//}
//
//public static ApproximateFractionFromValue(value: number, epsilonForPrecision: number): Fraction {
// let n: number = 1;
// let d: number = 1;
// let fraction: number = n / d;
// while (Math.abs(fraction - value) > epsilonForPrecision) {
// if (fraction < value) {
// n++;
// }
// else {
// d++;
// n = Math.round(value * d);
// }
// fraction = n / d;
// }
// return new Fraction(n, d);
//}
//public static GetEarlierTimestamp(m1: Fraction, m2: Fraction): Fraction {
// if (m1 < m2)
// return m1;
// else return m2;
//}
//public static getFraction(value: number, denominatorPrecision: number): Fraction {
// let numerator: number = Math.round(value / (1.0 / denominatorPrecision));
// return new Fraction(numerator, denominatorPrecision);
//}
//public static fractionMin(f1: Fraction, f2: Fraction): Fraction {
// if (f1 < f2)
// return f1;
// else return f2;
//}
//public static GetMaxValue(): Fraction {
// return new Fraction(Fraction.maximumAllowedNumber, 1);
//}
//public static get MaxAllowedNumerator(): number {
// return Fraction.maximumAllowedNumber;
//}
//public static get MaxAllowedDenominator(): number {
// return Fraction.maximumAllowedNumber;
//}
//public ToFloatingString(): string {
// return this.RealValue.ToString();
//}
//public Compare(x: Fraction, y: Fraction): number {
// if (x > y)
// return 1;
// if (x < y)
// return -1;
// return 0;
//}
//#region operators
//
// // operator overloads must always come in pairs
// // operator overload +
// public static Fraction operator + (Fraction f1, Fraction f2)
//{
// Fraction sum = new Fraction(f1);
// sum.Add(f2);
// return sum;
//}
//
//// operator overload -
//public static Fraction operator - (Fraction f1, Fraction f2)
//{
// Fraction diff = new Fraction(f1);
// diff.Sub(f2);
// return diff;
//}
//
//// operator overloads must always come in pairs
//// operator overload >
//public static bool operator > (Fraction f1, Fraction f2)
//{
// //return (long) f1.Numerator*f2._denominator > (long) f2._numerator*f1._denominator;
// return f1.RealValue > f2.RealValue;
//}
//
//// operator overload <
//public static bool operator < (Fraction f1, Fraction f2)
//{
// //return (long) f1._numerator*f2._denominator < (long) f2._numerator*f1._denominator;
// return f1.RealValue < f2.RealValue;
//}
//
//// operator overload ==
//public static bool operator === (Fraction f1, Fraction f2)
//{
// // code enhanced for performance
// // System.Object.ReferenceEquals(f1, undefined) is better than if (f1 === undefined)
// // and comparisons between booleans are quick
// bool f1IsNull = System.Object.ReferenceEquals(f1, undefined);
// bool f2IsNull = System.Object.ReferenceEquals(f2, undefined);
//
// // method returns true when both are undefined, false when only the first is undefined, otherwise the result of equals
// if (f1IsNull !== f2IsNull)
// return false;
//
// if (f1IsNull /*&& f2IsNull*/)
// return true;
//
// return equals(f1, f2);
//}
//
//// operator overload !=
//public static bool operator !== (Fraction f1, Fraction f2)
//{
// return (!(f1 === f2));
//}
//
//// operator overload >=
//public static bool operator >= (Fraction f1, Fraction f2)
//{
// return (!(f1 < f2));
//}
//
//// operator overload <=
//public static bool operator <= (Fraction f1,Fraction f2)
//{
// return (!(f1 > f2));
//}
//
//public static Fraction operator / (Fraction f, int i)
//{
// return new Fraction(f._numerator, f._denominator *= i);
//}
//
//public static Fraction operator / (Fraction f1, Fraction f2)
//{
// let res = new Fraction(f1.Numerator*f2.Denominator, f1.Denominator*f2.Numerator);
// return res.Denominator === 0 ? new Fraction(0, 1) : res;
//}
//
//public static Fraction operator * (Fraction f1, Fraction f2)
//{
// return new Fraction(f1.Numerator*f2.Numerator, f1.Denominator*f2.Denominator);
//}
//
//public static Fraction operator % (Fraction f1, Fraction f2)
//{
// let a = f1/f2;
// return new Fraction(a.Numerator%a.Denominator, a.Denominator)*f2;
//}
//
//#endregion operators
}