Class Fraction

This class offers the possibility to calculate fractions. You can pass a fraction in different formats: either as two integers, an integer, a floating point number or a string.

Numerator, denominator form

new Fraction(numerator, denominator);

Integer form

new Fraction(numerator);

Floating point form

new Fraction(value);

String form

new Fraction("123.456");  // a simple decimal
new Fraction("123/456"); // a string fraction
new Fraction("13e-3"); // scientific notation

Constructors

Properties

d: number

Denominator

n: number

Numerator

s: number

Sign: +1, 0 or -1

Methods

  • Compare two rational numbers, returns (this - other) which has the correct sign for Array.sort().

    Examples:

    new Fraction("19.7").compare("98/5") // 0.1
    new Fraction("19.6").compare("98/5") // 0
    new Fraction("19.5").compare("98/5") // -0.1

    Parameters

    Returns number

  • IEEE floats always have a denominator of a power of two. Reduce it out. If the process would produce integers too large for the Number type an approximation is used.

    Returns void

  • Check if two rational numbers are divisible (i.e. this is an integer multiple of other)

    Examples:

    new Fraction("7.6").divisible("5/2") // false
    new Fraction("7.5").divisible("5/2") // true
    new Fraction("7/4").divisible("5/2") // false

    Parameters

    Returns boolean

  • Check if two rational numbers are the same

    Examples:

    new Fraction("19.7").equals("98/5") // false
    new Fraction("19.6").equals("98/5") // true
    new Fraction("19.5").equals("98/5") // false

    Parameters

    Returns boolean

  • Calculates the geometric absolute value. Discards sign.

    Examples:

    new Fraction(3, 2).gabs()   // 3/2
    new Fraction(2, 3).gabs() // 2/3
    new Fraction(-1, 2).gabs() // 2/1

    Returns Fraction

  • Calculates the fractional gcd of two rational numbers. (i.e. both this and other is divisible by the result)

    Always returns a non-negative result.

    Example:

    new Fraction(5,8).gcd("3/7")  // 1/56
    

    Parameters

    Returns Fraction

  • Calculate the greatest common radical between two rational numbers if it exists.

    Never returns a subunitary result.

    Treats unity as the identity element: gcr(1, x) = gcr(x, 1) = x

    Examples:

    new Fraction(8).gcr(4)          // 2
    new Fraction(81).gcr(6561) // 9
    new Fraction("1/2").gcr("1/3") // null

    Parameters

    Returns null | Fraction

  • Geometrically reduce a rational number until its absolute value is between 1 and the absolute value of other, i.e. geometric modulo. Note: Returns a positive result for a negative modulo if the required number of divisions is even.

    Examples:

    new Fraction(5, 1).geoMod(2)        // 5/4
    new Fraction(1, 11).geoMod(3) // 27/11
    new Fraction(1, 11).geoMod("-1/3") // 9/11

    Parameters

    Returns Fraction

  • Rounds a rational number to a power of another rational number.

    Examples:

    new Fraction('5/4').geoRoundTo("9/8")     // 81/64
    new Fraction('5/4').geoRoundTo("1/1") // 1/1

    // handling negative values

    new Fraction('5/4').geoRoundTo("-9/8") // 81/64

    new Fraction('10/7').geoRoundTo("9/8") // 729/512
    new Fraction('10/7').geoRoundTo("-9/8") // 6561/4096

    new Fraction('-5/4').geoRoundTo("9/8") // null

    new Fraction('-5/4').geoRoundTo("-9/8") // -9/8

    Parameters

    Returns null | Fraction

  • Returns the inverse of the fraction, numerator and denominator are exchanged.

    Example:

    new Fraction(-3, 4).inverse()  // -4/3
    

    Returns Fraction

  • Check if the rational number is 1.

    Examples:

    new Fraction(9, 9).isUnity()      // true
    new Fraction("0.01e2").isUnity() // true
    new Fraction(7, 6).isUnity() // false

    Returns boolean

  • Calculates the fractional lcm of two rational numbers. (i.e. the result is divisible by both this and other)

    Has the same sign as the product of the rational numbers.

    Example:

    new Fraction(5,8).gcd("3/7")  // 15
    

    Parameters

    Returns Fraction

  • Calculate the least common radicand between two rational numbers if it exists.

    If either of the inputs is unitary returns unity (1).

    Returns a subunitary result if only one of the inputs is subunitary, superunitary otherwise.

    Examples:

    new Fraction(8).lcr(4)          // 64
    new Fraction("1/2").lcr("1/3") // null

    Parameters

    Returns null | Fraction

  • Perform harmonic addition of two rational numbers according to the thin lens equation f⁻¹ = u⁻¹ + v⁻¹.

    Example:

    new Fraction('5/3').lensAdd('3/2')  // 15/19
    

    Parameters

    Returns Fraction

  • Perform harmonic subtraction of two rational numbers u⁻¹ = f⁻¹ - v⁻¹ (rearranged thin lens equation).

    Example:

    new Fraction('15/19').lensSub('3/2')  // 5/3
    

    Parameters

    Returns Fraction

  • Calculate the logarithm of a rational number in the base of another, i.e. logdivision if the result exists as a rational number.

    Examples:

    new Fraction(4).log(2)              // 2
    new Fraction(64,27).log("16/9") // 3/2
    new Fraction(64,27).log("1/1") // null
    new Fraction(64,27).log(7) // null

    new Fraction(64,27).log("-16/9") // null
    new Fraction(-64,27).log("16/9") // null
    new Fraction(-64,27).log("-16/9") // null

    Parameters

    Returns null | Fraction

  • Calculates the mathematical modulo of two rational numbers. Correctly processes signs.

    Examples:

    new Fraction("5/1").mod("3/1")   // (5/1) % (3/1)  = 2/1
    new Fraction("-5/1").mod("3/1") // (-5/1) % (3/1) = (1/1) % (3/1) = 1/1

    Parameters

    Returns Fraction

  • Calculates the computational modulo of two rational numbers - a more precise fmod. Incorrectly processes signs.

    Examples:

    new Fraction("5/1").mod("3/1")   //   (5/1) % (3/1)  = 2/1
    new Fraction("-5/1").mod("3/1") // -((5/1) % (3/1)) = -2/1

    Parameters

    Returns Fraction

  • Calculates the fraction to some rational exponent, if possible.

    Examples:

    new Fraction("1/2").pow(2)      // 1/4
    new Fraction("-1/2").pow(-3) // -8
    new Fraction("9/4").pow("3/2") // 27/8
    new Fraction("2/1").pow("1/2") // null

    Parameters

    Returns null | Fraction

  • Reduce out the common factor between the numerator and denominator.

    Returns void

  • Rounds a rational number.

    Examples:

    new Fraction("4.'3'").floor()  // 4/1
    new Fraction("4.5").floor() // 5/1
    new Fraction("4.'6'").floor() // 5/1

    Returns Fraction

  • Rounds a rational number to a multiple of another rational number.

    Examples:

    new Fraction("0.'7'").roundTo("1/9")   // 7/9
    new Fraction("0.78").roundTo("1/9") // 7/9
    new Fraction("0.8'3'").roundTo("1/9") // 8/9
    new Fraction("0.85").roundTo("1/9") // 8/9

    Parameters

    Returns Fraction

  • Normalize infinite denominator into 0/1.

    Returns void

  • Return a convergent of this fraction that is within the given absolute tolerance.

    Parameters

    • epsilon: number = 0.001

      Absolute tolerance for error.

    Returns Fraction

  • Return a convergent of this fraction that is within the given relative tolerance measured in cents.

    Parameters

    • tolerance: number = 3.5

      Relative tolerance measured in cents.

    Returns Fraction

  • Calculates the square root of the rational number.

    Examples:

    new Fraction("9/4").sqrt() // 3/2
    new Fraction(-1).sqrt() // null

    Returns null | Fraction

    The positive square root if it exists as a rational number.

  • Returns an array of continued fraction elements.

    Example:

    new Fraction("7/8").toContinued()  // [0, 1, 7]
    

    Returns number[]

  • Returns a string-fraction representation of a Fraction object.

    Example:

    new Fraction("1.'3'").toFraction()  // "4/3"
    

    Returns string

  • Creates a string representation of a fraction with all digits.

    Example:

    new Fraction("100.'91823'").toString()  // "100.'91823'"
    

    Returns string

  • Validate that this fraction represents the ratio of two integers.

    Returns void

  • Returns a decimal representation of the fraction.

    Example:

    new Fraction("100.'91823'").valueOf()  // 100.91823918239183
    

    Returns number

  • Revive a Fraction instance produced by Fraction.toJSON(). Return everything else as is.

    Intended usage:

    const data = JSON.parse(serializedData, Fraction.reviver);
    

    Parameters

    • key: string

      Property name.

    • value: any

      Property value.

    Returns any

    Deserialized Fraction instance or other data without modifications.

    Throws

    An error if the numerator or denominator exceeds Number.MAX_SAFE_INTEGER.