Core arithmetic operations for precise rational number calculations. All operations maintain full precision using BigInt representation and return new Fraction instances.
Adds two rational numbers together, returning a new Fraction with the result.
/**
* Adds another fraction to this fraction
* @param num - Fraction input in any supported format
* @returns New Fraction representing the sum
*/
function add(num: FractionInput): Fraction;
/**
* Adds a fraction specified by numerator and denominator
* @param numerator - Numerator of the fraction to add
* @param denominator - Denominator of the fraction to add
* @returns New Fraction representing the sum
*/
function add(numerator: number | bigint, denominator: number | bigint): Fraction;Usage Examples:
import Fraction from "fraction.js";
// Add fractions in different formats
const f1 = new Fraction(1, 3);
const f2 = new Fraction(1, 6);
const sum = f1.add(f2); // 1/3 + 1/6 = 1/2
// Add using string input
const result = f1.add("0.25"); // 1/3 + 1/4 = 7/12
// Add using numerator/denominator
const total = f1.add(2, 9); // 1/3 + 2/9 = 5/9
// Chain operations
const complex = new Fraction(1, 2)
.add(1, 4)
.add("0.125"); // 1/2 + 1/4 + 1/8 = 7/8Subtracts one rational number from another, returning a new Fraction with the result.
/**
* Subtracts another fraction from this fraction
* @param num - Fraction input in any supported format
* @returns New Fraction representing the difference
*/
function sub(num: FractionInput): Fraction;
/**
* Subtracts a fraction specified by numerator and denominator
* @param numerator - Numerator of the fraction to subtract
* @param denominator - Denominator of the fraction to subtract
* @returns New Fraction representing the difference
*/
function sub(numerator: number | bigint, denominator: number | bigint): Fraction;Usage Examples:
import Fraction from "fraction.js";
// Basic subtraction
const f1 = new Fraction(3, 4);
const f2 = new Fraction(1, 4);
const difference = f1.sub(f2); // 3/4 - 1/4 = 1/2
// Subtract using different formats
const result = f1.sub("0.5"); // 3/4 - 1/2 = 1/4
const negative = f2.sub(f1); // 1/4 - 3/4 = -1/2
// Chain operations
const complex = new Fraction(2)
.sub(1, 3)
.sub("0.25"); // 2 - 1/3 - 1/4 = 17/12Multiplies two rational numbers together, returning a new Fraction with the result.
/**
* Multiplies this fraction by another fraction
* @param num - Fraction input in any supported format
* @returns New Fraction representing the product
*/
function mul(num: FractionInput): Fraction;
/**
* Multiplies by a fraction specified by numerator and denominator
* @param numerator - Numerator of the fraction to multiply by
* @param denominator - Denominator of the fraction to multiply by
* @returns New Fraction representing the product
*/
function mul(numerator: number | bigint, denominator: number | bigint): Fraction;Usage Examples:
import Fraction from "fraction.js";
// Basic multiplication
const f1 = new Fraction(2, 3);
const f2 = new Fraction(3, 4);
const product = f1.mul(f2); // 2/3 * 3/4 = 1/2
// Multiply by integer
const scaled = f1.mul(6); // 2/3 * 6 = 4
// Multiply by decimal
const decimal = f1.mul("1.5"); // 2/3 * 1.5 = 1
// Chain multiplications
const result = new Fraction(1, 2)
.mul(2, 3)
.mul(3, 4); // 1/2 * 2/3 * 3/4 = 1/4Divides one rational number by another, returning a new Fraction with the result.
/**
* Divides this fraction by another fraction
* @param num - Fraction input in any supported format
* @returns New Fraction representing the quotient
* @throws {Error} Division by Zero if divisor is zero
*/
function div(num: FractionInput): Fraction;
/**
* Divides by a fraction specified by numerator and denominator
* @param numerator - Numerator of the divisor fraction
* @param denominator - Denominator of the divisor fraction
* @returns New Fraction representing the quotient
* @throws {Error} Division by Zero if divisor is zero
*/
function div(numerator: number | bigint, denominator: number | bigint): Fraction;Usage Examples:
import Fraction from "fraction.js";
// Basic division
const f1 = new Fraction(3, 4);
const f2 = new Fraction(1, 2);
const quotient = f1.div(f2); // 3/4 ÷ 1/2 = 3/2
// Divide by integer
const halved = f1.div(2); // 3/4 ÷ 2 = 3/8
// Divide by decimal
const result = f1.div("0.25"); // 3/4 ÷ 0.25 = 3
// Chain operations
const complex = new Fraction(8)
.div(2)
.div(1, 2); // 8 ÷ 2 ÷ 1/2 = 8Raises a fraction to the power of another rational number. Returns null if the result would be complex.
/**
* Raises this fraction to the power of another fraction
* @param exponent - Exponent as fraction input in any supported format
* @returns New Fraction representing the result, or null if result is complex
*/
function pow(exponent: FractionInput): Fraction | null;
/**
* Raises this fraction to the power specified by numerator and denominator
* @param numerator - Numerator of the exponent fraction
* @param denominator - Denominator of the exponent fraction
* @returns New Fraction representing the result, or null if result is complex
*/
function pow(numerator: number | bigint, denominator: number | bigint): Fraction | null;Usage Examples:
import Fraction from "fraction.js";
// Integer exponents
const f1 = new Fraction(2, 3);
const squared = f1.pow(2); // (2/3)² = 4/9
const cubed = f1.pow(3); // (2/3)³ = 8/27
// Negative exponents (reciprocal)
const reciprocal = f1.pow(-1); // (2/3)⁻¹ = 3/2
const invSquared = f1.pow(-2); // (2/3)⁻² = 9/4
// Fractional exponents (roots)
const f2 = new Fraction(9, 16);
const sqrt = f2.pow(1, 2); // √(9/16) = 3/4
const cbrt = new Fraction(8, 27).pow(1, 3); // ∛(8/27) = 2/3
// Complex results return null
const negative = new Fraction(-1, 4);
const complexRoot = negative.pow(1, 2); // null (complex result)All arithmetic operations accept the same flexible input formats:
// String formats
f.add("123.456"); // Decimal
f.add("22/7"); // Simple fraction
f.add("3 1/4"); // Mixed number
f.add("0.(142857)"); // Repeating decimal
// Numeric formats
f.add(3.14159); // JavaScript number
f.add(42n); // BigInt
// Array format
f.add([22, 7]); // [numerator, denominator]
// Object formats
f.add({n: 22, d: 7}); // Object with n/d properties
f.add({n: 22, d: 7, s: -1}); // Object with sign
// Another Fraction instance
f.add(otherFraction);All arithmetic operations maintain full precision using BigInt representation:
import Fraction from "fraction.js";
// Precise decimal arithmetic
const precise = new Fraction("0.1")
.add("0.2"); // Exactly 3/10, not 0.30000000000000004
// Large number support
const big1 = new Fraction("123456789012345678901234567890");
const big2 = new Fraction("987654321098765432109876543210");
const bigSum = big1.add(big2); // Handles arbitrary precision
// Repeating decimal preservation
const third = new Fraction("0.(3)"); // Exactly 1/3
const twoThirds = third.mul(2); // Exactly 2/3
console.log(twoThirds.toString()); // "0.(6)"