npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@lvlte/rationalize

v1.0.3

Published

Represent a floating point number as a rational number

Readme

rationalize

Inspired by Julia's rationalize

Represent a floating point number x as a rational number [p, q] where |x - p/q| ≤ tol (the result will differ from x by no more than the given tolerance).

function rationalize(x: number, tol: number = eps(x)): [number, number]

Install

npm install @lvlte/rationalize

Usage

// ESM
import { rationalize } from '@lvlte/rationalize';
// CJS
const { rationalize } = require('@lvlte/rationalize');
const [p1, q1] = rationalize(0.1);            // [1, 10]
const [p2, q2] = rationalize(Math.PI);        // [165707065, 52746197]
const [p3, q3] = rationalize(Math.PI, 0.01);  // [22, 7]

// The sign of x always goes to the numerator
const [p4, q4] = rationalize(-13.78);         // [-689, 50]
const [p5, q5] = rationalize(-0);             // [-0, 1]

Safe Integers

The output ratio's components are guaranteed to be safe integers (float64 integers in the range [-2^53 + 1, +2^53 - 1]). In case one of the component goes out of this range, a RangeError is thrown, which does not happen as long as the tolerance is set to a value greater than or equal to eps(x) (default). The exception to this rule concerns absolute values of x less than 1/(2^53 - 1) or greater than 2^53 - 1. To rationalize these tiny/huge numbers with the best precision, the numerator and denominator needs to be represented with bigint (coming soon).

Tolerance

The default tolerance is the ULP of x, which allows the best accuracy knowing that most of the floating point numbers we are using, such as 0.1, are already approximations. In most cases, you don't need to change that except if you want to reduce the precision of the output. In case you want to increase it, note that using a zero tolerance will yield the exact (binary) representation of x as a decimal ratio. One legitimate case of using tolerance less than eps(x) exists though : ensuring the result of the floating point division p/q equals x, in this case you need to set the tolerance to eps(x)/2 :

import { rationalize, eps } from '@lvlte/rationalize';

let x = 0.8;    // 0.8000000000000000(444089209850062616169452667236328125)
const [p1, q1] = rationalize(x);          // [4, 5]
const [p2, q2] = rationalize(x, 0);       // [3602879701896397, 4503599627370496]

x = 0.1 + 0.2;  // 0.30000000000000004(44089209850062616169452667236328125)
const [p3, q3] = rationalize(x);            // [3, 10]
const [p4, q4] = rationalize(x, eps(x)/2);  // [415716888680356, 1385722962267853]
console.log(p3/q3 === x)                    // false
console.log(p4/q4 === x)                    // true