@Test public void testDoubleComparison() { Number a = Double.valueOf(1); Number b = Double.valueOf(2); assertEquals(0, NumberOperations.compare(a, a)); assertEquals(-1, NumberOperations.compare(a, b)); assertEquals(1, NumberOperations.compare(b, a)); }
private static int doubleComparison(Number n1, Number n2) { double val1 = doubleValue(n1); double val2 = doubleValue(n2); if (Double.isInfinite(val1) || Double.isNaN(val1) || Double.isInfinite(val2) || Double.isNaN(val2)) { return Double.compare(val1, val2); } int result; if (Math.abs(val1 - val2) < doubleTol) { result = 0; } else { result = (val1 < val2 ? -1 : 1); } return result; }
NumberOperations { private static int doubleComparison(Number n1, Number n2) { double val1 = doubleValue(n1); double val2 = doubleValue(n2); if (Double.isInfinite(val1) || Double.isNaN(val1) || Double.isInfinite(val2) || Double.isNaN(val2)) { return Double.compare(val1, val2); } int result; if (Math.abs(val1 - val2) < doubleTol) { result = 0; } else { result = (val1 < val2 ? -1 : 1); } return result; } }
NumberOperations { private static int doubleComparison(Number n1, Number n2) { double val1 = doubleValue(n1); double val2 = doubleValue(n2); if (Double.isInfinite(val1) || Double.isNaN(val1) || Double.isInfinite(val2) || Double.isNaN(val2)) { return Double.compare(val1, val2); } int result; if (Math.abs(val1 - val2) < doubleTol) { result = 0; } else { result = (val1 < val2 ? -1 : 1); } return result; }  }
NumberOperations { private static int doubleComparison(Number n1, Number n2) { double val1 = doubleValue(n1); double val2 = doubleValue(n2); if (Double.isInfinite(val1) || Double.isNaN(val1) || Double.isInfinite(val2) || Double.isNaN(val2)) { return Double.compare(val1, val2); } int result; if (Math.abs(val1 - val2) < doubleTol) { result = 0; } else { result = (val1 < val2 ? -1 : 1); } return result; }  static Number add(Number n1, Number n2); static Number subtract(Number n1, Number n2); static Number multiply(Number n1, Number n2); static Number divide(Number n1, Number n2); static int compare(Number n1, Number n2); static float getFloatTolerance(); static void setFloatTolerance(float tol); static double getDoubleTolerance(); static void setFloatTolerance(double tol); static int intValue(Number number); static long longValue(Number number); static float floatValue(Number number); static double doubleValue(Number number); static Number copy(Number number); static Number newInstance(Number number, Class<? extends Number> clazz); static Number castNumber(Number number, Class<? extends Number> clazz); static Class<? extends Number> highestClass(Number ...numbers); static Class<? extends Number> lowestClass(Number ...numbers); }
NumberOperations { private static int doubleComparison(Number n1, Number n2) { double val1 = doubleValue(n1); double val2 = doubleValue(n2); if (Double.isInfinite(val1) || Double.isNaN(val1) || Double.isInfinite(val2) || Double.isNaN(val2)) { return Double.compare(val1, val2); } int result; if (Math.abs(val1 - val2) < doubleTol) { result = 0; } else { result = (val1 < val2 ? -1 : 1); } return result; }  static Number add(Number n1, Number n2); static Number subtract(Number n1, Number n2); static Number multiply(Number n1, Number n2); static Number divide(Number n1, Number n2); static int compare(Number n1, Number n2); static float getFloatTolerance(); static void setFloatTolerance(float tol); static double getDoubleTolerance(); static void setFloatTolerance(double tol); static int intValue(Number number); static long longValue(Number number); static float floatValue(Number number); static double doubleValue(Number number); static Number copy(Number number); static Number newInstance(Number number, Class<? extends Number> clazz); static Number castNumber(Number number, Class<? extends Number> clazz); static Class<? extends Number> highestClass(Number ...numbers); static Class<? extends Number> lowestClass(Number ...numbers); static final float DEFAULT_FLOAT_TOL; static final double DEFAULT_DOUBLE_TOL; }