diff options
| author | Josh Kingsley <josh@joshkingsley.me> | 2025-11-06 22:31:12 +0200 |
|---|---|---|
| committer | Josh Kingsley <josh@joshkingsley.me> | 2025-11-06 22:31:12 +0200 |
| commit | 6ea86b1b56aebbae7edeb37b01d7bf5cd145bf60 (patch) | |
| tree | bfd0178097a56087471543950caebed5f8280040 /web/src/math/Ratio.ts | |
| parent | dad5b47f0bb480532043df8d488f5609f731b00d (diff) | |
feat(web): change subvisions
Diffstat (limited to 'web/src/math/Ratio.ts')
| -rw-r--r-- | web/src/math/Ratio.ts | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/web/src/math/Ratio.ts b/web/src/math/Ratio.ts index 0cca966..e2a1fbf 100644 --- a/web/src/math/Ratio.ts +++ b/web/src/math/Ratio.ts @@ -1,3 +1,5 @@ +import { gcd } from "."; + /** Serializable representation of a ratio. */ export type RatioData = [numerator: number, denominator: number]; @@ -25,8 +27,10 @@ export default class Ratio { throw new RangeError("Ratio demnominator cannot be zero"); } - this.#numerator = numerator; - this.#denominator = denominator; + const divisor = gcd(numerator, denominator); + + this.#numerator = numerator / divisor; + this.#denominator = denominator / divisor; } multiplyRatio(other: Ratio): Ratio { @@ -63,10 +67,22 @@ export default class Ratio { return left < right ? -1 : left > right ? 1 : 0; } + equals(other: Ratio): boolean { + return this.compare(other) === 0; + } + toNumber(): number { return this.numerator / this.denominator; } + toString(): string { + return `${this.numerator}/${this.denominator}`; + } + + [Symbol.for("nodejs.util.inspect.custom")](): string { + return `Ratio { ${this.numerator}/${this.denominator} }`; + } + static fromInteger(n: number): Ratio { return new Ratio(n, 1); } @@ -78,4 +94,12 @@ export default class Ratio { static fromData(ratio: RatioData): Ratio { return new Ratio(ratio[0], ratio[1]); } + + static min(...ratios: Ratio[]): Ratio { + return ratios.reduce((a, b) => (a.compare(b) <= 0 ? a : b)); + } + + static max(...ratios: Ratio[]): Ratio { + return ratios.reduce((a, b) => (a.compare(b) >= 0 ? a : b)); + } } |
