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.

expect(floatNumber).toBeNear(number, epsilon)

See original GitHub issue

Compare a given value numerically to be in the range of number-epsilon and number+epsilon. Thus epsilon specifies the maximum tolerance the compared value is allowed to vary.

Example

expect(4.23223432434).toBeNear(4, 0.25); // success
expect(4.23223432434).toBeNear(4, 0.2); // failure

Implementation

Could internally use the “toBeWithinRange” matcher, but the above is much nicer to write if you’re not targeting a specific range, but just a value and allow some wobbling.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
jacobwardiocommented, Jan 15, 2017
1reaction
JamieMasoncommented, Nov 3, 2016

Thanks @Nolanus, do I understand correctly that

expect(22).toBeNear(20, 2);

is equivalent to

expect(22).toBeWithinRange(18, 22);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Number.EPSILON - JavaScript - MDN Web Docs
The Number.EPSILON property represents the difference between 1 and the smallest floating point number greater than 1.
Read more >
What are the possible usage scenarios for Number.EPSILON?
EPSILON property represents the difference between 1 and the smallest floating point number greater than 1.
Read more >
JavaScript Number EPSILON Property - GeeksforGeeks
EPSILON property is used to check whether floating-point numbers are equal or not. In this EPSILON is accessed by calling the Number as...
Read more >
Avoid testing floating point numbers for equality - Appmarq
As long as this imprecision stays small, it can usually be ignored. However, it also means that numbers expected to be equal (e.g....
Read more >
Number.EPSILON - JavaScript
EPSILON property represents the difference between 1 and the smallest ... object to access this static property (use Number. ... expected output: true....
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