Optional
denominator: numberDenominator
Numerator
Sign: +1, 0 or -1
Adds two rational numbers.
Example:
new Fraction({n: 2, d: 3}).add("14.9") // 467/30
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
Divides two rational numbers
Example:
new Fraction("-17.'345'").div(3) // 5776/999
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
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
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
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
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
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
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
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
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
Perform harmonic subtraction of two rational numbers u⁻¹ = f⁻¹ - v⁻¹ (rearranged thin lens equation).
Example:
new Fraction('15/19').lensSub('3/2') // 5/3
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
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
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
Multiplies two rational numbers.
Example:
new Fraction("-17.'345'").mul(3) // 5776/111
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
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
Subtracts two rational numbers.
Example:
new Fraction({n: 2, d: 3}).sub("14.9") // -427/30
Serialize the fraction to a JSON compatible object.
An object with properties 'n', and 'd' corresponding to a signed numerator and an unsigned denominator respectively.
Static
reviverRevive a Fraction instance produced by Fraction.toJSON(). Return everything else as is.
Intended usage:
const data = JSON.parse(serializedData, Fraction.reviver);
Property name.
Property value.
Deserialized Fraction instance or other data without modifications.
An error if the numerator or denominator exceeds Number.MAX_SAFE_INTEGER
.
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
Integer form
Floating point form
String form