almost-equal
Test if two floats are almost equal
Last updated 12 years ago by mikolalysenko .
MIT · Repository · Original npm · Tarball · package.json
$ npm install almost-equal 
SYNC missed versions from official npm registry.

almost-equal

Checks when two floats are almost equal.

Use

First install using npm:

npm install almost-equal

Then use as follows:

var almostEqual = require("almost-equal")

var a = 100
  , b = 100 + 1e-12

//Check if a == b up to float precision
console.log(almostEqual(a, b, almostEqual.FLT_EPSILON, almostEqual.FLT_EPSILON))

//Check if a == b up to double precision
console.log(almostEqual(a, b, almostEqual.DBL_EPSILON, almostEqual.DBL_EPSILON))

almostEqual(a, b[, absoluteTolerance [, relativeTolerance]])

Checks if two floats are within the given tolerances of one another using the formula:

|a - b| < max(absoluteTolerance, min(|a|, |b|) * relativeTolerance)
  • a and b are the two numbers to comapre
  • absoluteTolerance is a fixed minimal tolerance (set to 0 to ignore)
  • relativeTolerance is a tolerance that scales with a/b (set to 0 to ignore)

Returns true if a and b are approximately equal.

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

almostEqual.FLT_EPSILON

Floating point (32-bit) epsilon

almostEqual.DBL_EPSILON

Double precision (64-bit) epsilon

Credits

(c) 2013 Mikola Lysenko. MIT License

Current Tags

  • 1.1.0                                ...           latest (9 years ago)

3 Versions

  • 1.1.0                                ...           9 years ago
  • 1.0.0                                ...           10 years ago
  • 0.0.0                                ...           12 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (1)
Dependents (2)

© 2010 - cnpmjs.org x YWFE | Home | YWFE