or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

arithmetic.mdcomparison-utility.mdconversion-formatting.mdindex.mdmathematical-functions.md
tile.json

comparison-utility.mddocs/

Comparison and Utility

Comparison operations and utility functions for testing equality, ordering, divisibility, and rounding operations on rational numbers.

Capabilities

Equality Comparison

Tests if two fractions are mathematically equal.

/**
 * Tests if this fraction equals another fraction
 * @param num - Fraction to compare against in any supported format
 * @returns true if fractions are equal, false otherwise
 */
function equals(num: FractionInput): boolean;

Usage Examples:

import Fraction from "fraction.js";

// Basic equality
const f1 = new Fraction(1, 2);
const f2 = new Fraction(2, 4);
console.log(f1.equals(f2));           // true (both equal 1/2)

// Different formats
const f3 = new Fraction(3, 4);
console.log(f3.equals("0.75"));       // true
console.log(f3.equals([3, 4]));       // true
console.log(f3.equals({n: 6, d: 8})); // true (simplified to 3/4)

// Precision comparison
const precise = new Fraction("0.(3)");
console.log(precise.equals(1, 3));    // true (exact 1/3)
console.log(precise.equals(0.333));   // false (0.333 ≠ 1/3)

Less Than Comparison

Tests if this fraction is less than another fraction.

/**
 * Tests if this fraction is less than another fraction
 * @param num - Fraction to compare against in any supported format
 * @returns true if this fraction is less than the other, false otherwise
 */
function lt(num: FractionInput): boolean;

Less Than or Equal Comparison

Tests if this fraction is less than or equal to another fraction.

/**
 * Tests if this fraction is less than or equal to another fraction
 * @param num - Fraction to compare against in any supported format
 * @returns true if this fraction is less than or equal to the other, false otherwise
 */
function lte(num: FractionInput): boolean;

Greater Than Comparison

Tests if this fraction is greater than another fraction.

/**
 * Tests if this fraction is greater than another fraction
 * @param num - Fraction to compare against in any supported format
 * @returns true if this fraction is greater than the other, false otherwise
 */
function gt(num: FractionInput): boolean;

Greater Than or Equal Comparison

Tests if this fraction is greater than or equal to another fraction.

/**
 * Tests if this fraction is greater than or equal to another fraction
 * @param num - Fraction to compare against in any supported format
 * @returns true if this fraction is greater than or equal to the other, false otherwise
 */
function gte(num: FractionInput): boolean;

Usage Examples:

import Fraction from "fraction.js";

// Comparison operations
const f1 = new Fraction(1, 3);
const f2 = new Fraction(1, 2);
const f3 = new Fraction(2, 6); // Same as 1/3

console.log(f1.lt(f2));               // true (1/3 < 1/2)
console.log(f2.gt(f1));               // true (1/2 > 1/3)
console.log(f1.lte(f3));              // true (1/3 <= 1/3)
console.log(f1.gte(f3));              // true (1/3 >= 1/3)

// Compare with different formats
const quarter = new Fraction(1, 4);
console.log(quarter.lt("0.5"));       // true
console.log(quarter.gt([1, 8]));      // true (1/4 > 1/8)
console.log(quarter.equals(0.25));    // true

General Comparison

Compares two fractions and returns a numeric result indicating their relationship.

/**
 * Compares this fraction with another fraction
 * @param num - Fraction to compare against in any supported format
 * @returns -1 if this < other, 0 if this = other, 1 if this > other
 */
function compare(num: FractionInput): number;

Usage Examples:

import Fraction from "fraction.js";

const f1 = new Fraction(2, 5);
const f2 = new Fraction(3, 7);

const result = f1.compare(f2);
if (result < 0) {
  console.log("f1 is less than f2");
} else if (result > 0) {
  console.log("f1 is greater than f2");
} else {
  console.log("f1 equals f2");
}

// Useful for sorting
const fractions = [
  new Fraction(3, 4),
  new Fraction(1, 2),
  new Fraction(7, 8)
];
fractions.sort((a, b) => a.compare(b));
// Now sorted: [1/2, 3/4, 7/8]

Divisibility Test

Tests if this fraction is evenly divisible by another fraction.

/**
 * Tests if this fraction is divisible by another fraction
 * @param num - Divisor fraction in any supported format
 * @returns true if division results in an integer, false otherwise
 */
function divisible(num: FractionInput): boolean;

Usage Examples:

import Fraction from "fraction.js";

// Basic divisibility
const f1 = new Fraction(6, 8);  // 3/4
const f2 = new Fraction(3, 16); // 3/16
console.log(f1.divisible(f2));   // true (3/4 ÷ 3/16 = 4)

// Not divisible
const f3 = new Fraction(1, 3);
const f4 = new Fraction(1, 7);
console.log(f3.divisible(f4));   // false (1/3 ÷ 1/7 = 7/3)

// Divisible by integer
const f5 = new Fraction(15, 4);
console.log(f5.divisible(3));    // false
console.log(f5.divisible(1, 4)); // true (15/4 ÷ 1/4 = 15)

Ceiling Function

Rounds the fraction up to the next integer or specified decimal places.

/**
 * Returns the ceiling of this fraction
 * @param places - Number of decimal places (default: 0 for integer ceiling)
 * @returns New Fraction representing the ceiling
 */
function ceil(places?: number): Fraction;

Usage Examples:

import Fraction from "fraction.js";

// Integer ceiling
const f1 = new Fraction(7, 3);      // 2.333...
const ceil1 = f1.ceil();            // 3

const f2 = new Fraction(-7, 3);     // -2.333...
const ceil2 = f2.ceil();            // -2

// Decimal places ceiling
const f3 = new Fraction(22, 7);     // 3.142857...
const ceil3 = f3.ceil(2);           // 3.15 (rounded up to 2 decimal places)

Floor Function

Rounds the fraction down to the previous integer or specified decimal places.

/**
 * Returns the floor of this fraction
 * @param places - Number of decimal places (default: 0 for integer floor)
 * @returns New Fraction representing the floor
 */
function floor(places?: number): Fraction;

Usage Examples:

import Fraction from "fraction.js";

// Integer floor
const f1 = new Fraction(7, 3);      // 2.333...
const floor1 = f1.floor();          // 2

const f2 = new Fraction(-7, 3);     // -2.333...
const floor2 = f2.floor();          // -3

// Decimal places floor
const f3 = new Fraction(22, 7);     // 3.142857...
const floor3 = f3.floor(2);         // 3.14 (rounded down to 2 decimal places)

Round Function

Rounds the fraction to the nearest integer or specified decimal places.

/**
 * Rounds this fraction to the nearest value
 * @param places - Number of decimal places (default: 0 for integer rounding)
 * @returns New Fraction representing the rounded value
 */
function round(places?: number): Fraction;

Usage Examples:

import Fraction from "fraction.js";

// Integer rounding
const f1 = new Fraction(7, 3);      // 2.333...
const round1 = f1.round();          // 2

const f2 = new Fraction(8, 3);      // 2.666...
const round2 = f2.round();          // 3

// Decimal places rounding
const f3 = new Fraction(22, 7);     // 3.142857...
const round3 = f3.round(2);         // 3.14 (rounded to 2 decimal places)
const round4 = f3.round(3);         // 3.143 (rounded to 3 decimal places)

// Negative numbers
const f4 = new Fraction(-5, 2);     // -2.5
const round5 = f4.round();          // -2 (rounds toward zero for .5)

Round To Multiple

Rounds the fraction to the nearest multiple of another fraction.

/**
 * Rounds this fraction to the nearest multiple of another fraction
 * @param num - The fraction to round to multiples of
 * @returns New Fraction representing the rounded value
 */
function roundTo(num: FractionInput): Fraction;

/**
 * Rounds to the nearest multiple specified by numerator and denominator
 * @param numerator - Numerator of the multiple fraction
 * @param denominator - Denominator of the multiple fraction
 * @returns New Fraction representing the rounded value
 */
function roundTo(numerator: number | bigint, denominator: number | bigint): Fraction;

Usage Examples:

import Fraction from "fraction.js";

// Round to nearest eighth
const f1 = new Fraction("0.9");
const rounded = f1.roundTo(1, 8);    // 7/8 (nearest eighth to 0.9)

// Round to nearest quarter
const f2 = new Fraction(7, 6);       // 1.166...
const quarter = f2.roundTo(1, 4);    // 5/4 (nearest quarter to 7/6)

// Round to nearest tenth
const f3 = new Fraction(22, 7);      // 3.142857...
const tenth = f3.roundTo("0.1");     // 3.1 (nearest tenth)

Comparison Chains

All comparison methods can be chained for complex logical operations:

import Fraction from "fraction.js";

const f = new Fraction(3, 8);

// Check if fraction is in range
const inRange = f.gt(1, 4) && f.lt(1, 2);  // true (1/4 < 3/8 < 1/2)

// Complex comparisons
const values = [
  new Fraction(1, 6),
  new Fraction(1, 4),
  new Fraction(1, 3),
  new Fraction(1, 2)
];

// Find values between 1/5 and 2/5
const filtered = values.filter(val => 
  val.gt(1, 5) && val.lt(2, 5)
);
// Result: [1/4, 1/3]

Precision in Comparisons

All comparison operations maintain full rational precision:

import Fraction from "fraction.js";

// Exact comparisons with repeating decimals
const third = new Fraction("0.(3)");     // Exactly 1/3
const approx = new Fraction(0.333333);   // Approximate

console.log(third.gt(approx));           // true (1/3 > 0.333333)
console.log(third.equals(1, 3));         // true (exact equality)

// Large number comparisons
const big1 = new Fraction("123456789012345678901234567890", "123456789012345678901234567891");
const big2 = new Fraction("123456789012345678901234567889", "123456789012345678901234567891");
console.log(big1.gt(big2));              // true (precise comparison)