14

Are there any java libraries for doing double comparison? e.g.

public static boolean greaterThanOrEqual(double a, double b, double epsilon){
     return a - b > -epsilon;
}

Every project I start I end up re-implementing this and copy-pasting code and test.

NB a good example of why its better to use 3rd party JARs is that IBM recommend the following:

"If you don't know the scale of the underlying measurements, using the test "abs(a/b - 1) < epsilon" is likely to be more robust than simply comparing the difference"

I doubt many people would have thought of this and illustrates that even simple code can be sub-optimal.

DD.
  • 21,498
  • 52
  • 157
  • 246
  • I don't follow. You're doing the comparison right there with a really short line of code. Why would you need to wrap it in a method? – Vala Jul 09 '12 at 07:48
  • Why would you want a library that does double comparison? Makes no sense since standard language already does that. Put that method of yours in a JAR, import it whenever needed and you're done. – m0skit0 Jul 09 '12 at 07:49
  • I write code for lots of different people and I can't use the same JAR for intellectual property reasons. Also there is always the chance my code is buggy. – DD. Jul 09 '12 at 07:54
  • 1
    This question + answers has what you need: http://stackoverflow.com/questions/6837007/comparing-float-double-values-using-operator – Nim Jul 09 '12 at 07:55
  • IMHO I am not in favour of replacing code with a function name which is longer than the original code. – Peter Lawrey Jul 09 '12 at 08:05

3 Answers3

18

Guava has DoubleMath.fuzzyCompare().

palacsint
  • 28,416
  • 10
  • 82
  • 109
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • 1
    WARNING: Guava's `fuzzyCompare()` uses the naive "maximum absolute difference" approach, which is highly unreliable, as it only works for a very limited range of values due to roundoff and cancellation inherent in floats. I wouldn't use it. See [this answer](http://stackoverflow.com/a/28751350/4610114) to a related question for more details, or (my recommendation) read [this article](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/) for a great introduction into the complexities of the apparently simple problem of float comparisons. – Franz D. Mar 01 '15 at 06:49
2

In the standard Java library there are no methods to handle your problem actually I suggest you to follow Joachim's link and use that library which is quite good for your needs, even though my suggestion would be to create an utils library in which you could add frequently used methods as the one you've stated in your question, as for different implementations of your problem you should consider looking into this :

Java double comparison epsilon

Feel free to ask out any other ambiguities

Community
  • 1
  • 1
Lucian Enache
  • 2,510
  • 5
  • 34
  • 59
2

You should abstain from any library that uses the naive "maximum absolute difference" approach (like Guava). As detailed in the Bruce Dawson's excellent article Comparing Floating Point Numbers, 2012 edition, it is highly error-prone as it only works for a very limited range of values. A much more robust approach is to use relative differences or ULPs for approximate comparisons.

The only library I know of that does implement a correct approximate comparison algorithm is apache.common.math.

Franz D.
  • 1,061
  • 10
  • 23