summaryrefslogtreecommitdiff
path: root/web/src/math/Ratio.ts
diff options
context:
space:
mode:
Diffstat (limited to 'web/src/math/Ratio.ts')
-rw-r--r--web/src/math/Ratio.ts25
1 files changed, 19 insertions, 6 deletions
diff --git a/web/src/math/Ratio.ts b/web/src/math/Ratio.ts
index 4973ff4..d8e1149 100644
--- a/web/src/math/Ratio.ts
+++ b/web/src/math/Ratio.ts
@@ -3,15 +3,15 @@ export type RatioData = [numerator: number, denominator: number];
/** Representation of a ratio for performing fractional artithmetic. */
export default class Ratio {
- private readonly _numerator: number;
- private readonly _denominator: number;
+ readonly #numerator: number;
+ readonly #denominator: number;
get numerator(): number {
- return this._numerator;
+ return this.#numerator;
}
get denominator(): number {
- return this._denominator;
+ return this.#denominator;
}
constructor(numerator: number, denominator: number) {
@@ -25,8 +25,8 @@ export default class Ratio {
throw new RangeError("Ratio demnominator cannot be zero");
}
- this._numerator = numerator;
- this._denominator = denominator;
+ this.#numerator = numerator;
+ this.#denominator = denominator;
}
multiplyRatio(other: Ratio): Ratio {
@@ -43,6 +43,19 @@ export default class Ratio {
);
}
+ add(other: Ratio): Ratio {
+ return new Ratio(
+ this.numerator * other.denominator + other.numerator * this.denominator,
+ this.denominator * other.denominator,
+ );
+ }
+
+ compare(other: Ratio): number {
+ const left = this.numerator * other.denominator;
+ const right = other.numerator * this.denominator;
+ return left < right ? -1 : left > right ? 1 : 0;
+ }
+
toNumber(): number {
return this.numerator / this.denominator;
}