Optionaldenominator: numberCalculates the absolute value.
Example:
new Fraction(-4).abs() // 4
Adds two rational numbers.
Example:
new Fraction({n: 2, d: 3}).add("14.9") // 467/30
Calculates the ceil of a rational number.
Example:
new Fraction("4.'3'").ceil() // 5/1
Clones the actual object.
Example:
new Fraction("-17.'345'").clone() // new Fraction("-17.'345'")
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
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.
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 floor of a rational number.
Example:
new Fraction("4.'3'").floor() // 4/1
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
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
Returns the inverse of the fraction, numerator and denominator are exchanged.
Example:
new Fraction(-3, 4).inverse() // -4/3
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
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
Returns the additive inverse of the fraction.
Example:
new Fraction(-4).neg() // 4
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
Reduce out the common factor between the numerator and denominator.
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
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
Normalize infinite denominator into 0/1.
Return a convergent of this fraction that is within the given absolute tolerance.
Absolute tolerance for error.
Return a convergent of this fraction that is within the given relative tolerance measured in cents.
Relative tolerance measured in cents.
Calculates the square root of the rational number.
Examples:
new Fraction("9/4").sqrt() // 3/2
new Fraction(-1).sqrt() // null
The positive square root if it exists as a rational number.
Subtracts two rational numbers.
Example:
new Fraction({n: 2, d: 3}).sub("14.9") // -427/30
Returns an array of continued fraction elements.
Example:
new Fraction("7/8").toContinued() // [0, 1, 7]
Returns a string-fraction representation of a Fraction object.
Example:
new Fraction("1.'3'").toFraction() // "4/3"
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.
Creates a string representation of a fraction with all digits.
Example:
new Fraction("100.'91823'").toString() // "100.'91823'"
Validate that this fraction represents the ratio of two integers.
Returns a decimal representation of the fraction.
Example:
new Fraction("100.'91823'").valueOf() // 100.91823918239183
StaticreviverRevive 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.
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