# Rational

#####

declaration
Rational

MathLib.Rational is the MathLib implementation of rational numbers.

#### Simple use case:

    // Create the rational number 2/3
    var r = new MathLib.Rational(2, 3);

    var Rational = (function () {
    function Rational(numerator, denominator) {
        if (typeof denominator === "undefined") { denominator = 1; }
        this.type = 'rational';
        if (MathLib.isZero(denominator)) {
            MathLib.error({ message: 'The denominator cannot be zero.', method: 'Rational.constructor' });
            throw 'The denominator cannot be zero.';
        }
        this.numerator = numerator;
        this.denominator = denominator;
    }

# compare

#####

method
Rational.prototype.compare() → number

Option name | Type | Description
--- | --- | ---
rational | Rational | The number to compare

Compares two rational numbers

    Rational.prototype.compare = function (rational) {
        return MathLib.sign(this.numerator * rational.denominator - this.denominator * rational.numerator);
    };

# divide

#####

method
Rational.prototype.divide() → Rational

Option name | Type | Description
--- | --- | ---
divisor | Rational number | The divisor

Divides rational numbers

    Rational.prototype.divide = function (divisor) {
        if (divisor.type === 'rational') {
            return new MathLib.Rational(MathLib.times(this.numerator, divisor.denominator), MathLib.times(this.denominator, divisor.numerator));
        } else if (typeof divisor === 'number') {
            return new MathLib.Rational(this.numerator, MathLib.times(this.denominator, divisor));
        } else {
            return divisor.inverse().times(this);
        }
    };

# inverse

#####

method
Rational.prototype.inverse() → Rational

Calculates the inverse of a rational number

    Rational.prototype.inverse = function () {
        if (!MathLib.isZero(this.numerator)) {
            return new MathLib.Rational(this.denominator, this.numerator);
        }
    };

# isEqual

#####

method
Rational.prototype.isEqual() → boolean

Option name | Type | Description
--- | --- | ---
number | Rational | The number to compare

Checks if the rational number is equal to an other number

    Rational.prototype.isEqual = function (number) {
        return MathLib.isEqual(MathLib.times(this.numerator, number.denominator), MathLib.times(this.denominator, number.numerator));
    };

# isZero

#####

method
Rational.prototype.isZero() → boolean

Checks if the rational number is zero

    Rational.prototype.isZero = function () {
        return MathLib.isZero(this.numerator);
    };

# minus

#####

method
Rational.prototype.minus() → Rational

Option name | Type | Description
--- | --- | ---
subtrahend | Rational number | The number to be subtracted

Subtracts rational numbers

    Rational.prototype.minus = function (subtrahend) {
        var n = this.numerator, d = this.denominator;

        if (subtrahend.type === 'rational') {
            return new MathLib.Rational(MathLib.minus(MathLib.times(n, subtrahend.denominator), MathLib.times(d, subtrahend.numerator)), MathLib.times(d, subtrahend.denominator));
        } else if (typeof subtrahend === 'number') {
            return new MathLib.Rational(MathLib.minus(n, MathLib.times(subtrahend, d)), d);
        } else {
            return subtrahend.minus(this).negative();
        }
    };

# negative

#####

method
Rational.prototype.negative() → Rational

Calculates the negative of a rational number

    Rational.prototype.negative = function () {
        return new MathLib.Rational(-this.numerator, this.denominator);
    };

# plus

#####

method
Rational.prototype.plus() → Rational

Option name | Type | Description
--- | --- | ---
summand | Rational number | The number to be added

Adds rational numbers

    Rational.prototype.plus = function (summand) {
        var n = this.numerator, d = this.denominator;

        if (summand.type === 'rational') {
            return new MathLib.Rational(MathLib.plus(MathLib.times(d, summand.numerator), MathLib.times(n, summand.denominator)), MathLib.times(d, summand.denominator));
        } else if (typeof summand === 'number') {
            return new MathLib.Rational(MathLib.plus(n, MathLib.times(summand, d)), d);
        } else {
            return summand.plus(this);
        }
    };

# reduce

#####

method
Rational.prototype.reduce() → Rational

Reduces the rational number

    Rational.prototype.reduce = function () {
        var gcd = MathLib.sign(this.denominator) * MathLib.gcd([this.numerator, this.denominator]);
        return new MathLib.Rational(this.numerator / gcd, this.denominator / gcd);
    };

# times

#####

method
Rational.prototype.times() → Rational

Option name | Type | Description
--- | --- | ---
factor | Rational number | The number to be multiplied

Multiplies rational numbers

    Rational.prototype.times = function (factor) {
        if (factor.type === 'rational') {
            return new MathLib.Rational(MathLib.times(this.numerator, factor.numerator), MathLib.times(this.denominator, factor.denominator));
        } else if (typeof factor === 'number') {
            return new MathLib.Rational(MathLib.times(this.numerator, factor), this.denominator);
        } else {
            return factor.times(this);
        }
    };

# toContentMathML

#####

method
Rational.prototype.toContentMathML() → string

Returns the Content MathML representation of the rational number

    Rational.prototype.toContentMathML = function () {
        return '<cn type="rational">' + this.numerator + '<sep/>' + this.denominator + '</cn>';
    };

# toLaTeX

#####

method
Rational.prototype.toLaTeX() → string

Returns the LaTeX representation of the rational number

    Rational.prototype.toLaTeX = function () {
        return '\\frac{' + MathLib.toLaTeX(this.numerator) + '}{' + MathLib.toLaTeX(this.denominator) + '}';
    };

# toMathML

#####

method
Rational.prototype.toMathML() → string

Returns the MathML representation of the rational number

    Rational.prototype.toMathML = function () {
        return '<mfrac>' + MathLib.toMathML(this.numerator) + MathLib.toMathML(this.denominator) + '</mfrac>';
    };

# toNumber

#####

method
Rational.prototype.toNumber() → number

Returns the number represented by the rational number

    Rational.prototype.toNumber = function () {
        return this.numerator / this.denominator;
    };

# toString

#####

method
Rational.prototype.toString() → string

Custom toString function

    Rational.prototype.toString = function () {
        return MathLib.toString(this.numerator) + '/' + MathLib.toString(this.denominator);
    };
    return Rational;
        })();
        MathLib.Rational = Rational;
    })(MathLib || (MathLib = {}));