public void testFuzzyCompareBadTolerance() { for (double tolerance : BAD_TOLERANCE_CANDIDATES) { try { DoubleMath.fuzzyCompare(1, 2, tolerance); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { // success } } }
private static void runTestFuzzyCompare(int toleranceIndex) { double tolerance = get(TOLERANCE_CANDIDATES, toleranceIndex); for (double a : ALL_DOUBLE_CANDIDATES) { for (double b : ALL_DOUBLE_CANDIDATES) { int expected = DoubleMath.fuzzyEquals(a, b, tolerance) ? 0 : Double.compare(a, b); int actual = DoubleMath.fuzzyCompare(a, b, tolerance); assertEquals(Integer.signum(expected), Integer.signum(actual)); } } }
/** * we do a fuzzy compare with a tolerance of EPSILON * * @param a * @param b * @return */ private static int compare(double a, double b) { return DoubleMath.fuzzyCompare(a, b, EPSILON); } }
public static boolean le(final double d1, final double d2) { return DoubleMath.fuzzyCompare(d1, d2, TOLERANCE) <= 0; } }
public static boolean gt(final double d1, final double d2) { return DoubleMath.fuzzyCompare(d1, d2, TOLERANCE) > 0; }
public static boolean lt(final double d1, final double d2) { return DoubleMath.fuzzyCompare(d1, d2, TOLERANCE) < 0; }
public static boolean ge(final double d1, final double d2) { return DoubleMath.fuzzyCompare(d1, d2, TOLERANCE) >= 0; }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; TransverseMercator that = (TransverseMercator) o; double tolerance = 1e-6; if (DoubleMath.fuzzyCompare(that.earthRadius, earthRadius, tolerance) != 0) return false; if (DoubleMath.fuzzyCompare(that.falseEasting, falseEasting, tolerance) != 0) return false; if (DoubleMath.fuzzyCompare(that.falseNorthing, falseNorthing, tolerance) != 0) return false; if (DoubleMath.fuzzyCompare(that.lat0, lat0, tolerance) != 0) return false; if (DoubleMath.fuzzyCompare(that.lon0, lon0, tolerance) != 0) return false; if (DoubleMath.fuzzyCompare(that.scale, scale, tolerance) != 0) return false; if ((defaultMapArea == null) != (that.defaultMapArea == null)) return false; // common case is that these are null if (defaultMapArea != null && !that.defaultMapArea.equals(defaultMapArea)) return false; return true; }
double horzDist = Math.max(leftDist, rightDist); double vertDist = Math.max(topDist, bottomDist); if (DoubleMath.fuzzyCompare(horzDist, 0, DOUBLE_EQ_EPSILON) >= 0 ^ DoubleMath.fuzzyCompare(vertDist, 0, DOUBLE_EQ_EPSILON) >= 0) { // case 1 return Math.max(vertDist, horzDist); if (DoubleMath.fuzzyCompare(horzDist, 0, DOUBLE_EQ_EPSILON) > 0) { // case 2 return Math.sqrt(vertDist * vertDist + horzDist * horzDist);
/** * Returns {@code true} if this bounding box contains {@code point}. * * @param point a point in projection coordinates. * @return {@code true} if this bounding box contains {@code point}. */ public boolean contains(ProjectionPoint point) { return DoubleMath.fuzzyCompare(point.getX(), getMinX(), 1e-6) >= 0 && DoubleMath.fuzzyCompare(point.getX(), getMaxX(), 1e-6) <= 0 && DoubleMath.fuzzyCompare(point.getY(), getMinY(), 1e-6) >= 0 && DoubleMath.fuzzyCompare(point.getY(), getMaxY(), 1e-6) <= 0; }
/** * Returns {@code true} if this bounding box contains {@code point}. * * @param point a point in projection coordinates. * @return {@code true} if this bounding box contains {@code point}. */ public boolean contains(ProjectionPoint point) { return DoubleMath.fuzzyCompare(point.getX(), getMinX(), 1e-6) >= 0 && DoubleMath.fuzzyCompare(point.getX(), getMaxX(), 1e-6) <= 0 && DoubleMath.fuzzyCompare(point.getY(), getMinY(), 1e-6) >= 0 && DoubleMath.fuzzyCompare(point.getY(), getMaxY(), 1e-6) <= 0; }
private int floatingEqual(Object actual, Object expected) { if (!(isFloatingPointValue(actual) && isFloatingPointValue(expected))) { return -1; } double expectedDouble = getDoubleValue(expected); double tolerance = 0; Optional<Double> configTolerance = configuration.getDouble(FLOAT_TOLERANCE_CONFIGURATION_KEY); if (configTolerance.isPresent()) { tolerance = Math.abs(configTolerance.get() * expectedDouble); } return DoubleMath.fuzzyCompare(getDoubleValue(actual), expectedDouble, tolerance); }
private int floatingEqual(Object actual, Object expected) { if (!(isFloatingPointValue(actual) && isFloatingPointValue(expected))) { return -1; } double expectedDouble = getDoubleValue(expected); double tolerance = 0; Optional<Double> configTolerance = configuration.getDouble(FLOAT_TOLERANCE_CONFIGURATION_KEY); if (configTolerance.isPresent()) { tolerance = Math.abs(configTolerance.get() * expectedDouble); } return DoubleMath.fuzzyCompare(getDoubleValue(actual), expectedDouble, tolerance); }
public void testFuzzyCompareBadTolerance() { for (double tolerance : BAD_TOLERANCE_CANDIDATES) { try { DoubleMath.fuzzyCompare(1, 2, tolerance); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { // success } } }
private static void runTestFuzzyCompare(int toleranceIndex) { double tolerance = get(TOLERANCE_CANDIDATES, toleranceIndex); for (double a : ALL_DOUBLE_CANDIDATES) { for (double b : ALL_DOUBLE_CANDIDATES) { int expected = DoubleMath.fuzzyEquals(a, b, tolerance) ? 0 : Double.compare(a, b); int actual = DoubleMath.fuzzyCompare(a, b, tolerance); assertEquals(Integer.signum(expected), Integer.signum(actual)); } } }
private boolean pointIsBetween(ProjectionPoint point, ProjectionPoint linePoint1, ProjectionPoint linePoint2) { if (linePoint1.getX() == linePoint2.getX()) { // No fuzzy comparison necessary. assert point.getX() == linePoint1.getX() : "point should have the same X as the line."; double minY = Math.min(linePoint1.getY(), linePoint2.getY()); double maxY = Math.max(linePoint1.getY(), linePoint2.getY()); // Returns true if point.getY() is in the range [minY, maxY], with fuzzy math. return DoubleMath.fuzzyCompare(minY, point.getY(), TOLERANCE) <= 0 && DoubleMath.fuzzyCompare(point.getY(), maxY, TOLERANCE) <= 0; } else if (linePoint1.getY() == linePoint2.getY()) { // No fuzzy comparison necessary. assert point.getY() == linePoint1.getY() : "point should have the same Y as the line."; double minX = Math.min(linePoint1.getX(), linePoint2.getX()); double maxX = Math.max(linePoint1.getX(), linePoint2.getX()); // Returns true if point.getX() is in the range [minX, maxX], with fuzzy math. return DoubleMath.fuzzyCompare(minX, point.getX(), TOLERANCE) <= 0 && DoubleMath.fuzzyCompare(point.getX(), maxX, TOLERANCE) <= 0; } else { throw new AssertionError("CAN'T HAPPEN: linePoint1 and linePoint2 are corners on the same side of a " + "bounding box; they must have *identical* x or y values."); } }
private boolean pointIsBetween(ProjectionPoint point, ProjectionPoint linePoint1, ProjectionPoint linePoint2) { if (linePoint1.getX() == linePoint2.getX()) { // No fuzzy comparison necessary. assert point.getX() == linePoint1.getX() : "point should have the same X as the line."; double minY = Math.min(linePoint1.getY(), linePoint2.getY()); double maxY = Math.max(linePoint1.getY(), linePoint2.getY()); // Returns true if point.getY() is in the range [minY, maxY], with fuzzy math. return DoubleMath.fuzzyCompare(minY, point.getY(), TOLERANCE) <= 0 && DoubleMath.fuzzyCompare(point.getY(), maxY, TOLERANCE) <= 0; } else if (linePoint1.getY() == linePoint2.getY()) { // No fuzzy comparison necessary. assert point.getY() == linePoint1.getY() : "point should have the same Y as the line."; double minX = Math.min(linePoint1.getX(), linePoint2.getX()); double maxX = Math.max(linePoint1.getX(), linePoint2.getX()); // Returns true if point.getX() is in the range [minX, maxX], with fuzzy math. return DoubleMath.fuzzyCompare(minX, point.getX(), TOLERANCE) <= 0 && DoubleMath.fuzzyCompare(point.getX(), maxX, TOLERANCE) <= 0; } else { throw new AssertionError("CAN'T HAPPEN: linePoint1 and linePoint2 are corners on the same side of a " + "bounding box; they must have *identical* x or y values."); } }