Advanced mathematical operations including modulo, GCD, LCM, logarithms, and utility functions for rational numbers.
Calculates the modulo (remainder) of rational number division with high precision.
/**
* Returns the fractional part of this fraction (remainder when divided by 1)
* @returns New Fraction representing the fractional part
*/
function mod(): Fraction;
/**
* Calculates the modulo of this fraction divided by another fraction
* @param num - Divisor fraction in any supported format
* @returns New Fraction representing the remainder
* @throws {Error} Division by Zero if divisor is zero
*/
function mod(num: FractionInput): Fraction;Usage Examples:
import Fraction from "fraction.js";
// Fractional part (mod 1)
const f1 = new Fraction(7, 3);
const fractionalPart = f1.mod(); // (7/3) mod 1 = 1/3
// Check if integer
const f2 = new Fraction(10, 5);
const isInteger = f2.mod().equals(0); // true (2 is integer)
// Modulo with another fraction
const f3 = new Fraction(13, 4);
const f4 = new Fraction(7, 8);
const remainder = f3.mod(f4); // (13/4) mod (7/8) = 1/8Calculates the fractional GCD of two rational numbers.
/**
* Calculates the fractional GCD of this fraction and another
* @param num - Other fraction in any supported format
* @returns New Fraction representing the GCD
*/
function gcd(num: FractionInput): Fraction;
/**
* Calculates the fractional GCD using numerator and denominator
* @param numerator - Numerator of the other fraction
* @param denominator - Denominator of the other fraction
* @returns New Fraction representing the GCD
*/
function gcd(numerator: number | bigint, denominator: number | bigint): Fraction;Usage Examples:
import Fraction from "fraction.js";
// GCD of two fractions
const f1 = new Fraction(5, 8);
const f2 = new Fraction(3, 7);
const gcdResult = f1.gcd(f2); // GCD(5/8, 3/7) = 1/56
// GCD with integer
const f3 = new Fraction(6, 9);
const gcdInt = f3.gcd(4); // GCD(2/3, 4) = 2/3Calculates the fractional LCM of two rational numbers.
/**
* Calculates the fractional LCM of this fraction and another
* @param num - Other fraction in any supported format
* @returns New Fraction representing the LCM
*/
function lcm(num: FractionInput): Fraction;
/**
* Calculates the fractional LCM using numerator and denominator
* @param numerator - Numerator of the other fraction
* @param denominator - Denominator of the other fraction
* @returns New Fraction representing the LCM
*/
function lcm(numerator: number | bigint, denominator: number | bigint): Fraction;Usage Examples:
import Fraction from "fraction.js";
// LCM of two fractions
const f1 = new Fraction(5, 8);
const f2 = new Fraction(3, 7);
const lcmResult = f1.lcm(f2); // LCM(5/8, 3/7) = 15
// LCM with zero
const f3 = new Fraction(0);
const f4 = new Fraction(5, 6);
const lcmZero = f3.lcm(f4); // LCM(0, 5/6) = 0Calculates the logarithm of a fraction to a given rational base. Returns null if the logarithm cannot be expressed as a rational number.
/**
* Calculates the logarithm of this fraction to the given base
* @param base - Base for the logarithm in any supported format
* @returns New Fraction representing the logarithm, or null if irrational
*/
function log(base: FractionInput): Fraction | null;
/**
* Calculates the logarithm using base specified by numerator and denominator
* @param numerator - Numerator of the base fraction
* @param denominator - Denominator of the base fraction
* @returns New Fraction representing the logarithm, or null if irrational
*/
function log(numerator: number | bigint, denominator: number | bigint): Fraction | null;Usage Examples:
import Fraction from "fraction.js";
// Rational logarithms
const f1 = new Fraction(27, 8);
const base = new Fraction(9, 4);
const logResult = f1.log(base); // log₉/₄(27/8) = 3/2
// Powers of 2
const f2 = new Fraction(8);
const log2 = f2.log(2); // log₂(8) = 3
// Irrational results return null
const f3 = new Fraction(3);
const logIrrational = f3.log(2); // null (log₂(3) is irrational)Returns the absolute value of the fraction.
/**
* Returns the absolute value of this fraction
* @returns New Fraction representing the absolute value
*/
function abs(): Fraction;Usage Examples:
import Fraction from "fraction.js";
// Positive fraction
const positive = new Fraction(3, 4);
const absPositive = positive.abs(); // 3/4
// Negative fraction
const negative = new Fraction(-5, 7);
const absNegative = negative.abs(); // 5/7
// Zero
const zero = new Fraction(0);
const absZero = zero.abs(); // 0Returns the negation (opposite sign) of the fraction.
/**
* Returns the negation of this fraction
* @returns New Fraction with opposite sign
*/
function neg(): Fraction;Usage Examples:
import Fraction from "fraction.js";
// Negate positive fraction
const positive = new Fraction(2, 5);
const negated = positive.neg(); // -2/5
// Negate negative fraction
const negative = new Fraction(-3, 8);
const doubled = negative.neg(); // 3/8
// Negate zero
const zero = new Fraction(0);
const negZero = zero.neg(); // 0Returns the multiplicative inverse (reciprocal) of the fraction.
/**
* Returns the multiplicative inverse of this fraction
* @returns New Fraction representing the reciprocal
* @throws {Error} Division by Zero if this fraction is zero
*/
function inverse(): Fraction;Usage Examples:
import Fraction from "fraction.js";
// Inverse of proper fraction
const f1 = new Fraction(3, 7);
const inv1 = f1.inverse(); // 7/3
// Inverse of improper fraction
const f2 = new Fraction(5, 2);
const inv2 = f2.inverse(); // 2/5
// Inverse of negative fraction
const f3 = new Fraction(-4, 9);
const inv3 = f3.inverse(); // -9/4
// Inverse of integer
const integer = new Fraction(5);
const invInt = integer.inverse(); // 1/5
// Error case
try {
const zero = new Fraction(0);
const invZero = zero.inverse(); // Throws "Division by Zero"
} catch (error) {
console.error(error.message);
}These functions preserve important mathematical properties:
import Fraction from "fraction.js";
// Modulo properties
const f = new Fraction(13, 7);
const mod3 = f.mod(3);
console.log(mod3.lt(3)); // true (remainder < divisor)
// GCD/LCM relationship: gcd(a,b) * lcm(a,b) = a * b
const a = new Fraction(6, 8);
const b = new Fraction(9, 12);
const gcd = a.gcd(b);
const lcm = a.lcm(b);
const product = gcd.mul(lcm);
const direct = a.mul(b);
console.log(product.equals(direct)); // true
// Inverse property: f * f.inverse() = 1
const fraction = new Fraction(7, 11);
const product = fraction.mul(fraction.inverse());
console.log(product.equals(1)); // true
// Absolute value properties
const neg = new Fraction(-5, 8);
const pos = new Fraction(5, 8);
console.log(neg.abs().equals(pos)); // true
console.log(neg.neg().equals(pos)); // true