forked from Carroted/Bimulo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassertFloatEqual.js
More file actions
36 lines (30 loc) · 821 Bytes
/
assertFloatEqual.js
File metadata and controls
36 lines (30 loc) · 821 Bytes
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
const { AssertionError } = require('assert');
/**
* Adapted from https://github.com/sindresorhus/float-equal
* License evidence: https://github.com/sindresorhus/float-equal/blob/master/license
* @author Sindre Sorhus + fork contributions from Alex Birch
* @license MIT
* @param {number} a
* @param {number} b
* @param {tolerance} b
*/
const assertFloatEqual = (a, b, tolerance = Number.EPSILON) => {
if (a === b) {
return;
}
const diff = Math.abs(a - b);
if (diff < tolerance) {
return;
}
if (diff <= tolerance * Math.min(Math.abs(a), Math.abs(b))) {
return;
}
throw new AssertionError({
operator: `within ${tolerance} of`,
expected: `${b} (difference: ${diff})`,
actual: a
});
};
module.exports = {
assertFloatEqual
};