Luc Maisonobe wrote:
Phil Steitz a écrit :
l...@apache.org wrote: }
/**
+     * Returns true iff both arguments are equal or within the range
of allowed
+     * error (inclusive).
+     * +     * @param x first value
+     * @param y second value
+     * @param eps the amount of absolute error to allow
+     * @return true if the values are equal or within range of each
other
+     */
+    public static boolean equals(double x, double y, double eps) {
+      return x == y || (x < y && (x + eps) >= y) || (x > y && x <= (y
+ eps));
+    }
Any reason not to just code this as Math.abs(x - y) <= eps  ?

I think infinities are handled properly by the first x == y and would
not be handled by the subtraction.
However thinking further about it, there is another problem with the
current implementation, it does not handle NaN the same way as equals
with no tolerance.

So what about:

   return equals(x, y) || Math.abs(x - y) <= eps

Luc

+1 - and we should include javadoc and test cases for infinities and NaNs

Phil


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
For additional commands, e-mail: dev-h...@commons.apache.org

Reply via email to