How to compare that sequence of doubles are all "approximately equal" in Java?

FranXh picture FranXh · Feb 1, 2012 · Viewed 32.3k times · Source

I have a method in java that returns a double number and I want to compare every double number that is returned every time I call the method(say 5 times), so that I can conclude that the number returned is almost the same every time.

How can I do this?

Answer

Ano picture Ano · Feb 1, 2012
public static boolean almostEqual(double a, double b, double eps){
    return Math.abs(a-b)<eps;
}

Where eps is measure of equality.