Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expect(floatNumber).toBeNear(number, epsilon) #58

Closed
Nolanus opened this issue Aug 29, 2016 · 5 comments
Closed

expect(floatNumber).toBeNear(number, epsilon) #58

Nolanus opened this issue Aug 29, 2016 · 5 comments

Comments

@Nolanus
Copy link

Nolanus commented Aug 29, 2016

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.

@JamieMason
Copy link
Owner

Thank you for this, at what times would you use either this or Jasmine's toBeCloseTo please?

@Nolanus
Copy link
Author

Nolanus commented Oct 4, 2016

The second parameter of Jasmine's toBeCloseTo is non-linear. Especially when comparing integer values a lot of tweaking is required to get the expected "tolerance area":

expect(22).toBeNear(20, 2); // success
expect(22).toBeNear(20, 1); // failure
expect(22).toBeCloseTo(20, ??); // What to choose as "precision" parameter?

@JamieMason
Copy link
Owner

Thanks @Nolanus, do I understand correctly that

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

is equivalent to

expect(22).toBeWithinRange(18, 22);

@Nolanus
Copy link
Author

Nolanus commented Nov 3, 2016

Hi @JamieMason,
that's correct.

Just realized my initial example was wrong, just fixed that.

@jacobwardio
Copy link
Contributor

#68 adds the toBeNear() matcher.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants