JavaHamcrest icon indicating copy to clipboard operation
JavaHamcrest copied to clipboard

Add FloatIsCloseTo matcher

Open laz2 opened this issue 7 years ago • 8 comments

  • Add FloatIsCloseTo matcher as copy of IsCloseTo matcher
  • Rename IsCloseTo matcher to DoubleIsCloseTo
  • Change error message for excluding errors in additional floating calculations (May user catch this error even with doubles?)
  • Ignore eclipse specific files

PS. Example of error with additional floating calculations:

public static void main(String[] args) {
        System.out.println(abs(1.0f - 0.1f) - 0.5f);
        System.out.println(abs(1.0d - 0.1d) - 0.5d);
}

After run:

0.39999998
0.4

laz2 avatar Nov 05 '18 20:11 laz2

Thanks for contributing. I have a couple of points. First, what problem are you trying to solve, what is the motivation for this change? Secondly, this would be a breaking change for existing users of IsCloseTo.

sf105 avatar Nov 09 '18 10:11 sf105

First, what problem are you trying to solve, what is the motivation for this change?

There's no closeTo matcher for float value (only with casting to double).

Secondly, this would be a breaking change for existing users of IsCloseTo.

I can rename DoubleIsCloseTo -> IsCloseTo. I tried resolve issue with single IsCloseTo and generics, but that solution doesn't work.

laz2 avatar Nov 09 '18 10:11 laz2

Sorry, to be clearer, is this just a matter of inconvenience, or is there a real problem? Are you seeing significant differences in the results when casting floats to doubles?

sf105 avatar Nov 09 '18 11:11 sf105

Are you seeing significant differences in the results when casting floats to doubles?

No, I don't. I think there may be problem. And of course there'r ugly type cast in strong type system.

laz2 avatar Nov 09 '18 12:11 laz2

@laz2 i personally prefer using the Matchers directly like Is.is instead of just referring to CoreMatchers.is. like microservices instead of monoliths, instead of one IsCloseTo to rule them all, creating FloatIsCloseTo and DoubleIsCloseTo makes more sense, also you might want to enforce Float or Double and not want auto boxing or type conversion.

please can you rebase from master, as hamcrest-core and hamcrest-library have been refactored a lot and also deprecated, so that everything is just in hamcrest.

nhojpatrick avatar Jun 29 '20 19:06 nhojpatrick

A long time ago, I had a plan to extract related matchers into a set of common DSL types, which I think might work here. Double and Float is probably one of the few occasions where a static import doesn't work.

sf105 avatar Jun 29 '20 19:06 sf105

Going to try and kick start hamcrest, so if you want to get it merged, please rebase from the branch v2.3-candidates. Still trying to understand how has permissions to perform a release.

nhojpatrick avatar Feb 13 '22 12:02 nhojpatrick

Done in another pull request #385

laz2 avatar Apr 20 '22 11:04 laz2