Add approximatelyEqual
This commit is contained in:
14
snippets/approximatelyEqual.md
Normal file
14
snippets/approximatelyEqual.md
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
### approximatelyEqual
|
||||||
|
|
||||||
|
Checks if two numbers are approximately equal to each other.
|
||||||
|
|
||||||
|
Use `Math.abs()` to compare the absolute difference of the two values to `epsilon`.
|
||||||
|
Omit the third parameter, `epsilon`, to use a default value of `0.001`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const approximatelyEqual = (v1, v2, epsilon = 0.001) => Math.abs(v1 - v2) < epsilon;
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
approximatelyEqual(Math.PI / 2.0 , 1.5708); // true
|
||||||
|
```
|
||||||
@ -1,4 +1,5 @@
|
|||||||
anagrams:string,recursion
|
anagrams:string,recursion
|
||||||
|
approximatelyEqual:math
|
||||||
arrayToHtmlList:browser,array
|
arrayToHtmlList:browser,array
|
||||||
ary:adapter,function
|
ary:adapter,function
|
||||||
atob:node,string,utility
|
atob:node,string,utility
|
||||||
|
|||||||
2
test/approximatelyEqual/approximatelyEqual.js
Normal file
2
test/approximatelyEqual/approximatelyEqual.js
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
const approximatelyEqual = (v1, v2, epsilon = 0.001) => Math.abs(v1 - v2) < epsilon;
|
||||||
|
module.exports = approximatelyEqual;
|
||||||
17
test/approximatelyEqual/approximatelyEqual.test.js
Normal file
17
test/approximatelyEqual/approximatelyEqual.test.js
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
const test = require('tape');
|
||||||
|
const approximatelyEqual = require('./approximatelyEqual.js');
|
||||||
|
|
||||||
|
test('Testing approximatelyEqual', (t) => {
|
||||||
|
//For more information on all the methods supported by tape
|
||||||
|
//Please go to https://github.com/substack/tape
|
||||||
|
t.true(typeof approximatelyEqual === 'function', 'approximatelyEqual is a Function');
|
||||||
|
t.true(approximatelyEqual(Math.PI / 2.0 , 1.5708), 'Works for PI / 2');
|
||||||
|
t.true(approximatelyEqual(0.1 + 0.2, 0.3), 'Works for 0.1 + 0.2 === 0.3');
|
||||||
|
t.true(approximatelyEqual(0.5, 0.5), 'Works for exactly equal values');
|
||||||
|
t.true(approximatelyEqual(0.501, 0.5, 0.1), 'Works for a custom epsilon');
|
||||||
|
//t.deepEqual(approximatelyEqual(args..), 'Expected');
|
||||||
|
//t.equal(approximatelyEqual(args..), 'Expected');
|
||||||
|
//t.false(approximatelyEqual(args..), 'Expected');
|
||||||
|
//t.throws(approximatelyEqual(args..), 'Expected');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
16
test/testlog
16
test/testlog
@ -1,4 +1,4 @@
|
|||||||
Test log for: Wed Feb 14 2018 12:33:42 GMT+0200 (GTB Standard Time)
|
Test log for: Wed Feb 14 2018 12:46:59 GMT+0200 (GTB Standard Time)
|
||||||
|
|
||||||
> 30-seconds-of-code@0.0.1 test G:\My Files\git Repositories\30-seconds-of-code
|
> 30-seconds-of-code@0.0.1 test G:\My Files\git Repositories\30-seconds-of-code
|
||||||
> tape test/**/*.test.js | tap-spec
|
> tape test/**/*.test.js | tap-spec
|
||||||
@ -11,6 +11,14 @@ Test log for: Wed Feb 14 2018 12:33:42 GMT+0200 (GTB Standard Time)
|
|||||||
√ Works for single-letter strings
|
√ Works for single-letter strings
|
||||||
√ Works for empty strings
|
√ Works for empty strings
|
||||||
|
|
||||||
|
Testing approximatelyEqual
|
||||||
|
|
||||||
|
√ approximatelyEqual is a Function
|
||||||
|
√ Works for PI / 2
|
||||||
|
√ Works for 0.1 + 0.2 === 0.3
|
||||||
|
√ Works for exactly equal values
|
||||||
|
√ Works for a custom epsilon
|
||||||
|
|
||||||
Testing arrayToHtmlList
|
Testing arrayToHtmlList
|
||||||
|
|
||||||
√ arrayToHtmlList is a Function
|
√ arrayToHtmlList is a Function
|
||||||
@ -1839,8 +1847,8 @@ Test log for: Wed Feb 14 2018 12:33:42 GMT+0200 (GTB Standard Time)
|
|||||||
√ Works with multiple promises
|
√ Works with multiple promises
|
||||||
|
|
||||||
|
|
||||||
total: 919
|
total: 924
|
||||||
passing: 919
|
passing: 924
|
||||||
duration: 2.4s
|
duration: 2.5s
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user