Sha256: 33b866c63792696cdf8d864e3c76bdd4b648a34d521a6fd35ea029307d859657

Contents?: true

Size: 1.94 KB

Versions: 68

Compression:

Stored size: 1.94 KB

Contents

function Rational(numerator, denominator) {
  if (denominator === 0) {throw new Error('Denominator must not be zero.');}

  this.numerator = numerator;
  this.denominator = denominator;

  this.reduce();
  this.ensureSignInNumerator();
}

Rational.prototype.add = function (that) {
  var commonDenominator = this.denominator * that.denominator;
  return new Rational(this.numerator * that.denominator + that.numerator * this.denominator, commonDenominator);
};

Rational.prototype.sub = function (that) {
  var commonDenominator = this.denominator * that.denominator;
  return new Rational(this.numerator * that.denominator - that.numerator * this.denominator, commonDenominator);
};

Rational.prototype.mul = function (that) {
  return new Rational(this.numerator * that.numerator, this.denominator * that.denominator);
};

Rational.prototype.div = function (that) {
  return new Rational(this.numerator * that.denominator, this.denominator * that.numerator);
};

Rational.prototype.abs = function () {
  return new Rational(Math.abs(this.numerator), Math.abs(this.denominator));
};

Rational.prototype.exprational = function (n) {
  return new Rational(Math.pow(this.numerator, n), Math.pow(this.denominator, n));
};

Rational.prototype.expreal = function (base) {
  return Math.pow(10.0, Math.log10(Math.pow(base, this.numerator)) / this.denominator);
};

Rational.prototype.reduce = function () {
  var commonDivisor = this.gcd(this.numerator, this.denominator);

  this.numerator /= commonDivisor;
  this.denominator /= commonDivisor;
  this.ensureSignInNumerator();

  return this;
};

Rational.prototype.gcd = function (a, b) {
  var localA = a;
  var localB = b;
  while (localB !== 0) {
    var t = localB;
    localB = localA % localB;
    localA = t;
  }
  return localA;
};

Rational.prototype.ensureSignInNumerator = function () {
  if (this.denominator < 0) {
    this.denominator = -this.denominator;
    this.numerator = -this.numerator;
  }
};

module.exports = Rational;

Version data entries

68 entries across 68 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.138 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.137 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.136 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.135 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.134 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.133 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.132 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.131 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.130 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.129 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.128 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.127 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.126 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.125 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.124 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.123 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.122 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.121 tracks/javascript/exercises/rational-numbers/example.js
trackler-2.2.1.120 tracks/javascript/exercises/rational-numbers/example.js