From af8cf348feb8e6bb4bda4a277b06a0f41ff890d9 Mon Sep 17 00:00:00 2001 From: Josh Kingsley Date: Sun, 26 Oct 2025 19:41:01 +0200 Subject: feat(web): show selected subdivisions --- web/src/math/Ratio.ts | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'web/src/math') 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; } -- cgit v1.2.3