/** * Compares {@code a} and {@code b} "fuzzily," with a tolerance for nearly-equal values. * * <p>This method is equivalent to {@code fuzzyEquals(a, b, tolerance) ? 0 : Double.compare(a, * b)}. In particular, like {@link Double#compare(double, double)}, it treats all NaN values as * equal and greater than all other values (including {@link Double#POSITIVE_INFINITY}). * * <p>This is <em>not</em> a total ordering and is <em>not</em> suitable for use in {@link * Comparable#compareTo} implementations. In particular, it is not transitive. * * @throws IllegalArgumentException if {@code tolerance} is {@code < 0} or NaN * @since 13.0 */ public static int fuzzyCompare(double a, double b, double tolerance) { if (fuzzyEquals(a, b, tolerance)) { return 0; } else if (a < b) { return -1; } else if (a > b) { return 1; } else { return Booleans.compare(Double.isNaN(a), Double.isNaN(b)); } }
/** * Compares {@code a} and {@code b} "fuzzily," with a tolerance for nearly-equal values. * * <p>This method is equivalent to {@code fuzzyEquals(a, b, tolerance) ? 0 : Double.compare(a, * b)}. In particular, like {@link Double#compare(double, double)}, it treats all NaN values as * equal and greater than all other values (including {@link Double#POSITIVE_INFINITY}). * * <p>This is <em>not</em> a total ordering and is <em>not</em> suitable for use in {@link * Comparable#compareTo} implementations. In particular, it is not transitive. * * @throws IllegalArgumentException if {@code tolerance} is {@code < 0} or NaN * @since 13.0 */ public static int fuzzyCompare(double a, double b, double tolerance) { if (fuzzyEquals(a, b, tolerance)) { return 0; } else if (a < b) { return -1; } else if (a > b) { return 1; } else { return Booleans.compare(Double.isNaN(a), Double.isNaN(b)); } }
public void testFuzzyEqualsInfiniteTolerance() { for (double a : DOUBLE_CANDIDATES_EXCEPT_NAN) { for (double b : DOUBLE_CANDIDATES_EXCEPT_NAN) { assertTrue(DoubleMath.fuzzyEquals(a, b, Double.POSITIVE_INFINITY)); } } }
public void testFuzzyEqualsFinite() { for (double a : FINITE_DOUBLE_CANDIDATES) { for (double b : FINITE_DOUBLE_CANDIDATES) { for (double tolerance : FINITE_TOLERANCE_CANDIDATES) { assertEquals(Math.abs(a - b) <= tolerance, DoubleMath.fuzzyEquals(a, b, tolerance)); } } } }
public void testFuzzyEqualsTwoNaNs() { for (double tolerance : TOLERANCE_CANDIDATES) { assertTrue(DoubleMath.fuzzyEquals(Double.NaN, Double.NaN, tolerance)); } }
public void testFuzzyInfiniteVersusFiniteWithFiniteTolerance() { for (double inf : INFINITIES) { for (double a : FINITE_DOUBLE_CANDIDATES) { for (double tolerance : FINITE_TOLERANCE_CANDIDATES) { assertFalse(DoubleMath.fuzzyEquals(a, inf, tolerance)); assertFalse(DoubleMath.fuzzyEquals(inf, a, tolerance)); } } } }
public void testFuzzyInfiniteVersusInfiniteWithFiniteTolerance() { for (double inf : INFINITIES) { for (double tolerance : FINITE_TOLERANCE_CANDIDATES) { assertTrue(DoubleMath.fuzzyEquals(inf, inf, tolerance)); assertFalse(DoubleMath.fuzzyEquals(inf, -inf, tolerance)); } } }
public void testFuzzyEqualsOneNaN() { for (double a : DOUBLE_CANDIDATES_EXCEPT_NAN) { for (double tolerance : TOLERANCE_CANDIDATES) { assertFalse(DoubleMath.fuzzyEquals(a, Double.NaN, tolerance)); assertFalse(DoubleMath.fuzzyEquals(Double.NaN, a, tolerance)); } } }
public void testFuzzyEqualsBadTolerance() { for (double tolerance : BAD_TOLERANCE_CANDIDATES) { try { DoubleMath.fuzzyEquals(1, 2, tolerance); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { // success } } }
/** * Compares {@code a} and {@code b} "fuzzily," with a tolerance for nearly-equal values. * * <p>This method is equivalent to {@code fuzzyEquals(a, b, tolerance) ? 0 : Double.compare(a, * b)}. In particular, like {@link Double#compare(double, double)}, it treats all NaN values as * equal and greater than all other values (including {@link Double#POSITIVE_INFINITY}). * * <p>This is <em>not</em> a total ordering and is <em>not</em> suitable for use in {@link * Comparable#compareTo} implementations. In particular, it is not transitive. * * @throws IllegalArgumentException if {@code tolerance} is {@code < 0} or NaN * @since 13.0 */ public static int fuzzyCompare(double a, double b, double tolerance) { if (fuzzyEquals(a, b, tolerance)) { return 0; } else if (a < b) { return -1; } else if (a > b) { return 1; } else { return Booleans.compare(Double.isNaN(a), Double.isNaN(b)); } }
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)); } } }
public void testFuzzyEqualsZeroTolerance() { // make sure we test -0 tolerance for (double zero : Doubles.asList(0.0, -0.0)) { for (double a : ALL_DOUBLE_CANDIDATES) { for (double b : ALL_DOUBLE_CANDIDATES) { assertEquals( a == b || (Double.isNaN(a) && Double.isNaN(b)), DoubleMath.fuzzyEquals(a, b, zero)); } } } }
@Test public void testFsOperation() throws IOException, InterruptedException { Meter meter = new Meter(); Path fakePath = new Path("fakePath"); for (int i = 0; i < 100; i++) { Assert.assertFalse(this.rateControlledFs.exists(fakePath)); meter.mark(); Thread.sleep((RANDOM.nextInt() & Integer.MAX_VALUE) % 10); } // Assert a fuzzy equal with 5% of tolerance Assert.assertTrue(DoubleMath.fuzzyEquals(meter.getMeanRate(), 20d, 20d * 0.05)); }
@Test public void testThrottling() throws InterruptedException { Meter meter = new Meter(); for (int i = 0; i < 100; i++) { Assert.assertTrue(this.limiter.acquirePermits(1) != null); meter.mark(); Thread.sleep((RANDOM.nextInt() & Integer.MAX_VALUE) % 10); } // Assert a fuzzy equal with 5% of tolerance Assert.assertTrue(DoubleMath.fuzzyEquals(meter.getMeanRate(), 20d, 20d * 0.05)); }
@Override public List<WorkUnit> pack(Map<String, List<WorkUnit>> workUnitsByTopic, int numContainers) { setWorkUnitEstSizes(workUnitsByTopic); List<WorkUnit> workUnits = Lists.newArrayList(); for (List<WorkUnit> workUnitsForTopic : workUnitsByTopic.values()) { // For each topic, merge all empty workunits into a single workunit, so that a single // empty task will be created instead of many. MultiWorkUnit zeroSizeWorkUnit = MultiWorkUnit.createEmpty(); for (WorkUnit workUnit : workUnitsForTopic) { if (DoubleMath.fuzzyEquals(getWorkUnitEstSize(workUnit), 0.0, EPS)) { addWorkUnitToMultiWorkUnit(workUnit, zeroSizeWorkUnit); } else { workUnit.setWatermarkInterval(getWatermarkIntervalFromWorkUnit(workUnit)); workUnits.add(workUnit); } } if (!zeroSizeWorkUnit.getWorkUnits().isEmpty()) { workUnits.add(squeezeMultiWorkUnit(zeroSizeWorkUnit)); } } return worstFitDecreasingBinPacking(workUnits, numContainers); } }
private void updateSessionAllocationWithEvent(WmTezSession session, double allocation) { WmEvent event = null; WmContext ctx = session.getWmContext(); if (ctx != null && session.hasClusterFraction() && !DoubleMath.fuzzyEquals(session.getClusterFraction(), allocation, 0.0001f)) { event = new WmEvent(EventType.UPDATE); } session.setClusterFraction(allocation); if (event != null) { event.endEvent(session); } }
String pinotValue = pinotResultSetGroup.getResultSet(aggregationIndex).getString(0); double actualValue = Double.parseDouble(pinotValue); if (!DoubleMath.fuzzyEquals(actualValue, expectedValue, 1.0)) { String failureMessage = "Value: " + aggregationIndex + " does not match, expected: " + h2Value + ", got: " + pinotValue; String pinotValue = pinotGroupByResultSet.getString(groupIndex); double actualValue = Double.parseDouble(pinotValue); if (!DoubleMath.fuzzyEquals(actualValue, expectedValue, 1.0)) { String failureMessage = "Value: " + aggregationIndex + " does not match, expected: " + h2Value + ", got: " + pinotValue
protected void checkInputs(Function<Double, Double> f, double xLower, double xUpper) { ArgChecker.notNull(f, "function"); if (DoubleMath.fuzzyEquals(xLower, xUpper, ZERO)) { throw new IllegalArgumentException("Lower and upper values were not distinct"); } }
@Override public Double apply(Double x) { ArgChecker.notNull(x, "x"); ArgChecker.notNegative(x, "x"); if (DoubleMath.fuzzyEquals(x, 0.5, 1e-14)) { return 0.5; } return _dist.getInverseCDF(x); }
private void validateData(ResolvedFxSingleBarrierOption option, RatesProvider ratesProvider, BlackFxOptionVolatilities volatilities, RecombiningTrinomialTreeData data) { ResolvedFxVanillaOption underlyingOption = option.getUnderlyingOption(); ArgChecker.isTrue(DoubleMath.fuzzyEquals(data.getTime(data.getNumberOfSteps()), volatilities.relativeTime(underlyingOption.getExpiry()), SMALL), "time to expiry mismatch between pricing option and trinomial tree data"); ArgChecker.isTrue(DoubleMath.fuzzyEquals(data.getSpot(), ratesProvider.fxRate(underlyingOption.getUnderlying().getCurrencyPair()), SMALL), "today's FX rate mismatch between rates provider and trinomial tree data"); }