Open-Awesome
CategoriesAlternativesStacksSelf-HostedExplore
Open-Awesome

© 2026 Open-Awesome. Curated for the developer elite.

TermsPrivacyAboutGitHubRSS
for browsers, or use npm/yarn for Node.js: 'npm install fraction.js' and import with require or ES6 syntax as shown in the README."}},{"@type":"Question","name":"What are the browser requirements for Fraction.js?","acceptedAnswer":{"@type":"Answer","text":"It requires BigInt support, available in modern browsers like Chrome, Firefox, and Edge, but not in older ones like Internet Explorer. Check compatibility tables if targeting legacy environments."}},{"@type":"Question","name":"Can Fraction.js handle negative modulo operations?","acceptedAnswer":{"@type":"Answer","text":"Yes, it provides mathematically correct modulo for negative congruences, which standard implementations often get wrong. The README includes a workaround using mod().add(b).mod(b) for exact results."}}]}
  1. Home
  2. JavaScript
  3. Fraction.js

Fraction.js

MITJavaScript

A JavaScript library for precise rational number arithmetic using BigInt, avoiding floating-point inaccuracies.

Visit WebsiteGitHubGitHub
695 stars76 forks0 contributors

What is Fraction.js?

Fraction.js is a JavaScript library that provides a robust implementation of rational numbers (ℚ). It solves the precision problems inherent in floating-point arithmetic by representing numbers as exact fractions with BigInt numerators and denominators, making it ideal for applications requiring high mathematical accuracy.

Target Audience

Developers working on applications that require exact mathematical precision, such as financial calculations, probability simulations, educational math tools, or scientific computing where floating-point errors are unacceptable.

Value Proposition

It prioritizes precision and mathematical correctness over speed, using BigInt to ensure calculations remain exact and reliable, serving as a foundational library for other mathematical tools. It outperforms typical fmod() implementations and handles repeating decimals and complex operations with perfect accuracy.

Overview

The RAW rational numbers library written in JavaScript

Use Cases

Best For

  • Performing exact rational arithmetic (addition, subtraction, multiplication, division, modulo) without floating-point errors.
  • Converting decimal numbers, including repeating decimals, to simplified fractions with optional error tolerance.
  • Building mathematical or educational tools that require precise representation of probabilities, angles (degrees/minutes/seconds), or irrational number approximations.
  • Implementing mathematically correct modulo operations, especially for negative congruences where typical modulo implementations fail.
  • Generating exact string or LaTeX representations of fractions for display in academic, scientific, or documentation contexts.
  • Serving as a foundational precision library for other math tools, such as polynomial or advanced math libraries.

Not Ideal For

  • Real-time applications where computational speed is critical, such as game physics or high-frequency trading systems.
  • Environments lacking BigInt support, like older browsers (e.g., Internet Explorer) or legacy JavaScript runtimes.
  • Use cases involving very large floating-point numbers without pre-conversion to strings, where the double parser's approximation may fail (e.g., numbers like 1234567.9991829).
  • Projects that only need simple, fast arithmetic with built-in JavaScript numbers and can tolerate minor floating-point errors.

Pros & Cons

Pros

Exact Mathematical Precision

Uses BigInt for numerators and denominators to eliminate floating-point errors, as shown in examples where 1/98 * 98 returns exactly 1 instead of 0.9999999999999999.

Flexible Input Parsing

Accepts inputs as integers, doubles, strings (including repeating decimals like '0.(3)'), arrays, or objects, making integration with diverse data sources straightforward.

Comprehensive Math Operations

Provides functions for gcd, lcm, pow, log, and mathematically correct modulo, supporting complex scenarios like probability calculations and irrational approximations demonstrated in the README.

Rich Output Formats

Generates exact string representations, LaTeX output, and continued fractions, ideal for academic papers or educational tools where precise display is required.

Cons

Performance Overhead

Prioritizes precision over speed, so calculations with BigInt and fraction objects are slower than native floating-point arithmetic, unsuitable for performance-critical applications.

Approximation Vulnerabilities

The double parser uses Farey Sequences and can fail for very large exact numbers (e.g., 1234567.9991829), requiring manual string conversion to avoid wrong approximations, as noted in the README.

Modern Environment Dependency

Relies on BigInt support, which is not available in older JavaScript engines, limiting compatibility and increasing setup complexity for cross-platform projects.

Frequently Asked Questions

Quick Stats

Stars695
Forks76
Contributors0
Open Issues2
Last commit1 year ago
CreatedSince 2014

Tags

#numbers#rational-numbers#math#fractions#javascript#numerical-computing#math-library

Built With

J
JavaScript

Links & Resources

Website

Included in

JavaScript34.9k
Auto-fetched 1 day ago

Related Projects

Numeral-jsNumeral-js

A javascript library for formatting and manipulating numbers.

Stars9,697
Forks906
Last commit8 months ago
odometerodometer

Smoothly transitions numbers with ease. #hubspot-open-source

Stars7,287
Forks694
Last commit8 years ago
chance.jschance.js

Chance - Random generator helper for JavaScript

Stars6,531
Forks462
Last commit1 year ago
Complex.jsComplex.js

The RAW Complex.js is a complex numbers library written in JavaScript

Stars253
Forks42
Last commit11 months ago
Community-curated · Updated weekly · 100% open source

Found a gem we're missing?

Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.

Submit a projectStar on GitHub