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 🙏

© 2024 – Pkg Stats / Ryan Hefner

js-floating-point

v1.0.0

Published

Dead-simple and lightning-fast solution for the floating-point issue in JavaScript

Downloads

147

Readme

The problem

Floating-point numbers are represented as binary fractions. Regrettably, most decimal fractions cannot be represented exactly as binary fractions. The decimal floating-point numbers you enter are only approximated by the binary floating-point numbers actually stored in the machine. That being said, you'll see that floating-point arithmetic is NOT 100% accurate.

0.2 + 0.1
0.30000000000000004

0.3 - 0.1
0.19999999999999998

1111.11 + 1111.11 + 1111.11 + 1111.11 + 1111.11
5555.549999999999

The full article https://www.avioconsulting.com/blog/overcoming-javascript-numeric-precision-issues

The solution

All existing solutions suggest us to round the number to some predefined amount of decimal digits. Simply said, to use .toFixed(N). But what if I don't know how many digits do I need?

Another solution is to use the Math.js library https://mathjs.org/docs/datatypes/bignumbers.html#roundoff-errors Really? I have to pull a huge library to resolve so common problem?

I couldn't belive that there are no some simple solution to resolve the problem so I wrote my own dead-simple and lightning-fast solution:

const [intPart, decimalPart] = `${value}`.split('.');
const matched = decimalPart.match(/(9{6,}|0{6,})(\\d)*$/gm);

if (!matched) {
  // no floating-point bug
  return value;
}

const [wrongPart] = matched;
const correctDecimalsLength = decimalPart.length - wrongPart.length;
const result = parseFloat(`${intPart}.${decimalPart}`).toFixed(
  correctDecimalsLength
);

To make it clear: the function is trying to find N repeating 0s or 9s and, if such sequence is found, it removes it rounding the last correct digit. That's all!

Just look at tests

Installation

yarn add js-floating-point

Usage

import floatingPointFix from 'js-floating-point';

0.2 + 0.1;
// WRONG: 0.30000000000000004

floatingPointFix(0.2 + 0.1);
// CORRECT: 0.3

0.3 - 0.1;
// WRONG: 0.19999999999999998

floatingPointFix(0.3 - 0.1);
// CORRECT: 0.2