@strong-roots-capital/deep-float-equal
v2.0.2
Published
Check two objects for float-equality
Downloads
307
Readme
deep-float-equal
Check two objects for float-equality
Note that BigInt
s 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.