JavaHamcrest icon indicating copy to clipboard operation
JavaHamcrest copied to clipboard

closeTo fails with negative error values

Open IanWorthington opened this issue 9 years ago • 3 comments

I'm getting an error that doesn't look right to me:

java.lang.AssertionError: test lng1 close to e-6 error Expected: is a numeric value within <-2.0013229086876708E-6> of <-2.001322908687671> but: <-2.001322908687671> differed by <2.0013229086876708E-6>

Test code is:

double lng1 = -2.001322908687671;

assertThat("test lng1 close to zero error", lng1, closeTo(lng1, 0D)); // This passes assertThat("test lng1 close to e-6 error", lng1, closeTo(lng1, lng1/1000000.0D)); // This fails!

This seems to be because the error value is negative which causes closeTo to always fail. Is this working as designed or does closeTo need an abs() in it?

IanWorthington avatar Sep 10 '16 01:09 IanWorthington

Just bumped into this issue. A very typical use case for the {{closeTo}} matcher is to use a relative error:

double expected = ....;
double actual = ...;
assertThat( actual, closeTo(expected, expected * 1e-8);

This will always fail when expected is a negative number.

PissedCapslock avatar Jan 10 '17 11:01 PissedCapslock

I already solved this... but nobody merges my pull request

StarBax89 avatar Jul 21 '17 20:07 StarBax89

There is still a pull request to solve this issue.

StarBax89 avatar Nov 23 '17 19:11 StarBax89