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

epsilon-equal

v0.0.2

Published

Compares two numbers taking the standard epsilon value for double precision into consideration.

Downloads

10

Readme

epsilon-equal

Compares two numbers taking the standard epsilon value for double precision into consideration.

Why

Numbers in Javascript are double-precision 64-bit format IEEE 754 values so when adding decimals number such as 0.1 + 0.2 we get 0.30000000000000004 because rational numbers, such as 0.1, which is 1/10), numbers whose denominators is not a power of two cannot be exactly represented. Computers can only natively store integers, so they need some way of representing decimal numbers. This representation comes with some degree of inaccuracy.

source

Internally computers use a format (binary floating-point) that cannot accurately represent a number like 0.1, 0.2 or 0.3 at all.

The exact number 0.1 in floating point is
0.10000 00000 00000 00555 11151 23125 78270 21181 58340 45410 15625

The exact number 0.2 in floating point is
0.20000 00000 00000 01110 22302 46251 56540 42363 16680 90820 3125

The exact number 0.3 in floating point is
0.29999 99999 99999 98889 77697 53748 43459 57636 83319 09179 6875

0.1 + 0.3
0.10000 00000 00000 00555 11151 23125 78270 21181 58340 45410 1 +
0.20000 00000 00000 01110 22302 46251 56540 42363 16680 90820 3 =
0.3000000000000000444089209850062616169452667236328125

01 + 0.3 !== 0.3
0.29999 99999 99999 98889 77697 53748 43459 57636 83319 09179 6875 !==
0.3000000000000000444089209850062616169452667236328125

Exact float calculator

So due to rounding errors, you should not compare non-integers directly. Instead, take an upper bound for rounding errors into consideration. This upper bound is called the machine epsilon. Machine epsilon gives an upper bound on the relative error due to rounding in floating point arithmetic. The standard epsilon value for double precision is 2−53.

source

Install

npm install epsilon-equal
bower install epsilon-equal

Usage

var epsEqual = require('epsilon-equal');

console.log(0.1 + 0.2 === 0.3); // false
console.log(0.1 + 0.2); // 0.30000000000000004

epsEqual(0.1 + 0.2, 0.3) // true

console.log(0.1 + 1 - 1, 0.1); // false
console.log(0.1 + 1 - 1); // 0.10000000000000009

epsEqual(0.1 + 1 - 1, 0.1) //true

License

MIT