summaryrefslogtreecommitdiff
path: root/apps/web/src/math/Ratio.ts
blob: e2a1fbf5afc38e65795d736f752ae244e91c0d2e (plain)
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
import { gcd } from ".";

/** Serializable representation of a ratio. */
export type RatioData = [numerator: number, denominator: number];

/** Representation of a ratio for performing fractional artithmetic. */
export default class Ratio {
  readonly #numerator: number;
  readonly #denominator: number;

  get numerator(): number {
    return this.#numerator;
  }

  get denominator(): number {
    return this.#denominator;
  }

  constructor(numerator: number, denominator: number) {
    if (!Number.isInteger(numerator) || !Number.isInteger(denominator)) {
      throw new TypeError(
        `Ratio must have integer parts: ${numerator} / ${denominator}`,
      );
    }

    if (denominator === 0) {
      throw new RangeError("Ratio demnominator cannot be zero");
    }

    const divisor = gcd(numerator, denominator);

    this.#numerator = numerator / divisor;
    this.#denominator = denominator / divisor;
  }

  multiplyRatio(other: Ratio): Ratio {
    return new Ratio(
      this.numerator * other.numerator,
      this.denominator * other.denominator,
    );
  }

  divideRatio(other: Ratio): Ratio {
    return new Ratio(
      this.numerator * other.denominator,
      this.denominator * other.numerator,
    );
  }

  add(other: Ratio): Ratio {
    return new Ratio(
      this.numerator * other.denominator + other.numerator * this.denominator,
      this.denominator * other.denominator,
    );
  }

  subtract(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;
  }

  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);
  }

  toData(): RatioData {
    return [this.numerator, this.denominator];
  }

  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));
  }
}