caolan / caolan/nodeunit

approx and deepApprox to safely compare numbers

Open
#218 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
1.9k
Forks
359
PR merge metrics
No merged PRs in 30d

Description

Testing equallity of numbers is a tricky business because of possible round of errors in floating point numbers. For example the following test will unexpectedly fail as the result of the left expression is `0.22000000000000003` and not `0.22`.

``` js
test.equal(2.2 / 10, 0.22);
```

It would be great to have functions like `approx` and `deepApprox` to safely compare numbers and objects/arrays containing numbers against each other. The function should compare whether the difference between the two numbers is at least a predefined factor epsilon smaller than the values itself (I guess there will be some edge cases when one of the values is zero, NaN, or Inf).

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.