Returns true if a and b are within tolerance of each other. Technically speaking, this is equivalent to Math.abs(a - b) <= tolerance || Double.valueOf(a).equals(Double.valueOf(b)). Notable special cases include: All NaNs are fuzzily equal. <li
(double a, double b, double tolerance)
| 353 | */ |
| 354 | |
| 355 | public static boolean fuzzyEquals(double a, double b, double tolerance) { |
| 356 | MathPreconditions.checkNonNegative("tolerance", tolerance); |
| 357 | return Math.copySign(a - b, 1.0) <= tolerance |
| 358 | // copySign(x, 1.0) is a branch-free version of abs(x), but with different NaN semantics |
| 359 | || (a == b) // needed to ensure that infinities equal themselves |
| 360 | || (Double.isNaN(a) && Double.isNaN(b)); |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Compares {@code a} and {@code b} "fuzzily," with a tolerance for nearly-equal values. |
no test coverage detected