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

is-close

v1.3.3

Published

Check if two numbers are equal within a given tolerance

Downloads

7,092

Readme

is-close

Check if either:

  1. two numbers are equal within a given tolerance
  2. the values in two arrays of numbers are equal within a given tolerance

isClose(a, b, rtol, atol, equalNaN, isCloseScaler) ⇒ boolean|Array<boolean>

Check if a is approximately equal to b.

This check uses similar logic to Python math.isClose. By default values are considered to be close if:

abs(a - b) <= max(rtol * max(abs(a), abs(b)), atol).

Returns: boolean - Returns true if both a and b are considered to be close.

| Parameter | Type | Description | | ---------------- | --- | --- | | a | number | First value to compare | | b | number | Second value to compare | | rtol | number [optional] | The relative tolerance parameter[default = 1e-09] | | atol | number [optional] | The absolute tolerance parameter[default = 0.0] | | equalNaN | boolean [optional] | Whether to compare NaNs as equal.If true, when a is NaN and b is NaN they will be considered equal.[default = false] | | isCloseScaler | IsCloseScaler [optional] | Method used to scale the absolute difference into a relative difference.[default = IsCloseScalers.maxAbsAOrB] |

Overload

Returns: Array<boolean> - Returns an array of booleans corresponding to the result of calling isClose() on the individual values in the arrays a and b.

| Parameter | Type | Description | | ---------------- | --- | --- | | a | Array<number> | First value to compare | | b | Array<number> | Second value to compare | | rtol | number [optional] | The relative tolerance parameter[default = 1e-09] | | atol | number [optional] | The absolute tolerance parameter[default = 0.0] | | equalNaN | boolean [optional] | Whether to compare NaNs as equal.[default = false] | | isCloseScaler | IsCloseScaler [optional] | Method used to scale the absolute difference into a relative difference.[default = IsCloseScalers.maxAbsAOrB] |

IsCloseScalers#maxAbsAOrB(a, b) => number

Returns: number - Relative difference scaling: Max(|a|,|b|)

IsCloseScalers#maxAOrB(a, b) => number

Returns: number - Relative difference scaling: Max(a,b)

IsCloseScalers#minAbsAOrB(a, b) => number

Returns: number - Relative difference scaling: Min(|a|,|b|)

IsCloseScalers#minAOrB(a, b) => number

Returns: number - Relative difference scaling: Min(a,b)

IsCloseScalers#meanAbs(a, b) => number

Returns: number - Relative difference scaling: Mean: (|a|+|b|)/2, using Welford's method

IsCloseScalers#mean(a, b) => number

Returns: number - Relative difference scaling: Mean: (a+b)/2, using Welford's method

IsCloseScalers#absA(a) => number

Returns: number - Relative error scaling: abs(a)

IsCloseScalers#absB(a, b) => number

Returns: number - Relative error scaling: abs(b)

Installation

npm install is-close --save

Example Usage

var isClose = require("is-close");

// Examples of checking relative difference between first two parameters
console.log(isClose.isClose(1e-7, 1e-8));
console.log(isClose.isClose(1e10, 1.0000000001e10));
console.log(isClose.isClose([1e-7, 1e10], [1e-8, 1.0000000001e10]));

// Examples of relative error relative to first parameter 
// - note how result is not symmetric
console.log(isClose.isClose(1.0, 0.9, 0.1, undefined, undefined, isClose.IsCloseScalers.absA));
console.log(isClose.isClose(0.9, 1.0, 0.1, undefined, undefined, isClose.IsCloseScalers.absA));

Output should be

false
true
[false, true]
true
false

TypeScript

import { isClose, IsCloseScalers } from 'is-close';

// Examples of checking relative difference between first two parameters
console.log(isClose(1.0, 1.000000001));
console.log(isClose(1.0, 1.0000000001));
console.log(isClose([1.0, 1.0], [1.000000001, 1.0000000001]));

// Examples of relative error relative to first parameter 
// - note how result is not symmetric
console.log(isClose(1.0, 0.9, 0.1, undefined, undefined, IsCloseScalers.absA));
console.log(isClose(0.9, 1.0, 0.1, undefined, undefined, IsCloseScalers.absA));

Output should be

false
true
[false, true]
true
false

License

MIT © Steve Bosman