File size: 1,297 Bytes
8d955a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
almost-equal
============
Checks when two floats are almost equal.

Use
===
First install using npm:

    npm install almost-equal

    

Then use as follows:


```javascript

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