Skip to content

An arbitrary length rational number library for Typescript

Notifications You must be signed in to change notification settings

nicolasLuduena/BigRational

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BigRational

npm version License: MIT

A lightweight TypeScript library for representing and manipulating rational numbers using bigint. Rational numbers are numbers that can be expressed as the quotient of two integers (numerator/denominator).

Features

  • Precision: Handles arbitrarily large numbers using bigint.
  • Arithmetic operations: Addition, subtraction, multiplication, division, and negation.
  • Comparison operations: <, <=, >, >=, ==.
  • Simplification: Automatically simplifies rational numbers to their reduced form.
  • Utilities: Absolute value, reciprocal, conversion to string, and more.

Installation

Install the package using npm:

npm install big-rational

Usage

import { BigRational } from 'big-rational';

// Create rational numbers
const r1 = new BigRational(3n, 4n);  // Represents 3/4
const r2 = new BigRational(5n, 6n);  // Represents 5/6

// Basic operations
const sum = r1.add(r2);             // 19/12
const difference = r1.subtract(r2);  // -1/12
const product = r1.mul(r2);          // 15/24 or simplified to 5/8
const quotient = r1.div(r2);         // 9/10

// Simplify a fraction
const simplified = product.reduce(); // 5/8

// Comparison
const isLess = r1.lt(r2);    // true
const isEqual = r1.eq(r2);   // false
const isGreater = r1.gt(r2); // false

// Utilities
const absoluteValue = r1.abs();  // 3/4
const inverse = r1.inverse();    // 4/3
const negation = r1.negate();    // -3/4

// String representation
console.log(r1.toString());  // "3 / 4"

About

An arbitrary length rational number library for Typescript

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published