Is there a way to test numbers which are approximately equal?
See original GitHub issueAva works like a charm for me, thanks!
I’m currently writing tests which involve comparing numbers, which needs some special treatment. Since numbers can have round off errors, this can lead to cases where tests which should be ok actually fail, like:
test('test', t => {
t.is(0.1 + 0.2, 0.3) // should be ok, but fails due to round-off errors
})
It would be great to be able to check whether numbers are approximately equal, both in tests like t.is
as well as t.deepEqual
. Maybe in the form of methods like approxEqual(value, expected, epsilon)
and approxDeepEqual(value, expected, epsilon)
, or some global setting allowing to configure how numbers are compared (exact or approx). Right now I’ve written my own helper functions for this, but I think it would be great to have this built in by default.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:9 (4 by maintainers)
Top Results From Across the Web
How to check two numbers are approximately ... - Tutorialspoint
STEP 3 − Check if difference is lesser than the epsilon. If true, display proper message saying the two numbers are approximately equal....
Read more >How to check if two values are nearly or approximately equal?
To check that two numbers are approximate to each other: ... how close you need the two numbers to be before you consider...
Read more >Function to determine if two numbers are nearly equal when ...
Given two numbers, check that they are approximately equal. Approximately equal is defined as the number of significant digits that agree.
Read more >Deciding if Numbers are Equal or Approximately Equal
Many calculator-solved problems give an approximate solution, not an exact solution, and the purpose of this section is to increase your ...
Read more >Approximate equality - Rosetta Code
The function should allow for differences in the magnitude of numbers, so that, for example, 100000000000000.01 may be approximately equal to ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The reason that I come up with this feature request is that I found myself writing helper functions for this in my AVA tests multiple times (in totally unrelated projects). That’s why I think this may be something more people than just me have to deal with and hence would be convenient to have out of the box.
A solution like
t.truthy(approxDeepEqual(value, expected))
works quite ok. AVA still outputs very useful info in case of a failing assertion like this (i.e. the actual values of bothvalue
andexpected
, and not justfalse !== true
).I would love to have a solution though which can output the diff of a failed approx deep equal assertion, though, like
deepEqual
of AVA does. That’s one of the things I love so much about AVA 😃Is there a way to write an extension for AVA?
There where I use Ava I’ve created a util function to test whether two numbers are approximately equal. In other projects I use Jest, which has a method
.toBeCloseTo(number, numDigits?)