A JavaScript library for precise rational number arithmetic using BigInt, avoiding floating-point inaccuracies.
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.
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.
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.
The RAW rational numbers library written in JavaScript
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
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.
Accepts inputs as integers, doubles, strings (including repeating decimals like '0.(3)'), arrays, or objects, making integration with diverse data sources straightforward.
Provides functions for gcd, lcm, pow, log, and mathematically correct modulo, supporting complex scenarios like probability calculations and irrational approximations demonstrated in the README.
Generates exact string representations, LaTeX output, and continued fractions, ideal for academic papers or educational tools where precise display is required.
Prioritizes precision over speed, so calculations with BigInt and fraction objects are slower than native floating-point arithmetic, unsuitable for performance-critical applications.
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.
Relies on BigInt support, which is not available in older JavaScript engines, limiting compatibility and increasing setup complexity for cross-platform projects.