diff options
| author | Josh Kingsley <josh@joshkingsley.me> | 2025-10-26 19:41:01 +0200 |
|---|---|---|
| committer | Josh Kingsley <josh@joshkingsley.me> | 2025-10-26 19:41:01 +0200 |
| commit | af8cf348feb8e6bb4bda4a277b06a0f41ff890d9 (patch) | |
| tree | ce7df39a6edb7e6df3d3fb0d903972391333a272 /web/src/math | |
| parent | 43ba019bc0d3af502b806169dad5fcbbfc87d2b7 (diff) | |
feat(web): show selected subdivisions
Diffstat (limited to 'web/src/math')
| -rw-r--r-- | web/src/math/Ratio.ts | 25 |
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; } |
