All files / src/math Fraction.ts

100% Statements 20/20
87.5% Branches 7/8
100% Functions 8/8
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 106 107 108 109 110 111 112 113 114 115 116 117 118 119      1x                           478x 478x 478x                       118x                               265x                         1x                       52x 52x 52x             98x               582x 582x 582x 582x                               2092x 582x     1510x 582x   928x      
/**
 * A fraction with a numerator and a denominator.
 */
export class Fraction {
  private _numerator: number;
  private _denominator: number;
 
  /**
   * Creates and immediately reduce the fraction using the greatest common
   * divider of the numerator and denominator.
   *
   * @param {number} numerator
   *   The numerator of this fraction.
   * @param {number} denominator
   *   The denominator of this fraction.
   */
  constructor(numerator: number, denominator: number) {
    this._numerator = numerator;
    this._denominator = denominator;
    this._reduce();
  }
 
  /**
   * Return a new fraction by adding the given fraction to this fraction.
   *
   * @param {Fraction} other
   *   The fraction to add to this one.
   * @return {Fraction}
   *   A new fraction obtained by adding the 2 fractions.
   */
  public add(other: Fraction): Fraction {
    return new Fraction(
      this._numerator * other._denominator +
        other._numerator * this._denominator,
      this._denominator * other._denominator
    );
  }
 
  /**
   * Return a new fraction by multiplying the given fraction by this fraction.
   *
   * @param {Fraction} other
   *   The fraction to multply by this one.
   * @return {Fraction}
   *   A new fraction obtained by multiplying the 2 fractions.
   */
  public multiply(other: Fraction): Fraction {
    return new Fraction(
      this._numerator * other._numerator,
      this._denominator * other._denominator
    );
  }
 
  /**
   * Returns the number value of this fraction.
   *
   * @return {number}
   *   The number value of this fraction
   */
  public valueOf(): number {
    return this._numerator / this._denominator;
  }
 
  /**
   * Checks if this fraction is equal to the provided fraction.
   *
   * @param {Fraction} other
   *   The fraction to compare to.
   * @return {boolean}
   *   `true` if the 2 fractions are equal, `false` else.
   */
  public sameAs(other: Fraction): boolean {
    this._reduce();
    other._reduce();
    return (
      this._numerator === other._numerator &&
      this._denominator === other._denominator
    );
  }
 
  public toString(): string {
    return `${this._numerator} / ${this._denominator}`;
  }
 
  /**
   * Reduce the fraction using the greatest common divider of the numerator
   * and the denominator.
   */
  private _reduce(): void {
    const div = gcd(this._numerator, this._denominator);
    Eif (div) {
      this._numerator = this._numerator / div;
      this._denominator = this._denominator / div;
    }
  }
}
 
/**
 * Computes the greatest common divider of 2 integers.
 *
 * @param {number} a
 *   First integer.
 * @param {number} b
 *   Second integer.
 * @returns {number}
 *   Greatest common divider of `a` and `b`.
 */
function gcd(a: number, b: number): number {
  if (b > a) {
    return gcd(b, a);
  }
 
  if (b === 0) {
    return a;
  } else {
    return gcd(b, a % b);
  }
}