2.0.2 • Published 5 years ago

@strong-roots-capital/deep-float-equal v2.0.2

Weekly downloads
38
License
ISC
Repository
github
Last release
5 years ago

deep-float-equal Build status npm version codecov

Check two objects for float-equality

Note that BigInts are currently not supported.

Install

npm install @strong-roots-capital/deep-float-equal

Use

deepFloatEqual<A, B>(a: A, b: B, absoluteTolerance = DBL_EPSILON, relativeTolerance = DBL_EPSILON)

Checks for normal deep-equality between objects a and b with the exception that floats are allowed to vary within the given tolerances of one another using the formula:

|x - y| < max(absoluteTolerance, min(|x|, |y|) * relativeTolerance)

  • x and y are two numbers within the objects to comapre
  • absoluteTolerance is x fixed minimal tolerance (set to 0 to ignore)
  • relativeTolerance is x tolerance that scales with x/y (set to 0 to ignore)

Returns true if a, b are deep-equal, with all corresponding floats being approximately equal.

If tolerance argument is omitted, almostEqual.DBL_EPSILON value is used by default.

Examples

import deepFloatEqual from '@strong-roots-capital/deep-float-equal'

let firstArray = [0.1 + 0.2]
let secondArray = [0.3]
console.log(deepFloatEqual(firstArray, secondArray))
//=>true

Optionally change the allowed epsilon tolerance (default DBL_EPSILON)

import deepFloatEqual, { FLT_EPSILON } from '@strong-roots-capital/deep-float-equal'

let firstArray = [0.1 + 0.2]
let secondArray = [0.3]
console.log(deepFloatEqual(firstArray, secondArray, FLT_EPSILON / 1e10, FLT_EPSILON / 1e10))
//=>false

FLT_EPSILON and DBL_EPSILON are re-exported from almost-equal.

Acknowledgments

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago