question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a way to test numbers which are approximately equal?

See original GitHub issue

Ava 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:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

10reactions
josdejongcommented, May 31, 2017

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 both value and expected, and not just false !== 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?

1reaction
josdejongcommented, Feb 24, 2020

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?)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found