Phil Steitz a écrit : > [email protected] 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 > > Phil > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [email protected] > For additional commands, e-mail: [email protected] > --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
