@Test public void testFloatComparison() { Number a = Float.valueOf(1); Number b = Float.valueOf(2); assertEquals(0, NumberOperations.compare(a, a)); assertEquals(-1, NumberOperations.compare(a, b)); assertEquals(1, NumberOperations.compare(b, a)); }
private static int floatComparison(Number n1, Number n2) { float val1 = floatValue(n1); float val2 = floatValue(n2); if (Float.isInfinite(val1) || Float.isNaN(val1) || Float.isInfinite(val2) || Float.isNaN(val2)) { return Float.compare(val1, val2); } int result; if (Math.abs(val1 - val2) < floatTol) { result = 0; } else { result = (val1 < val2 ? -1 : 1); } return result; }
NumberOperations { private static int floatComparison(Number n1, Number n2) { float val1 = floatValue(n1); float val2 = floatValue(n2); if (Float.isInfinite(val1) || Float.isNaN(val1) || Float.isInfinite(val2) || Float.isNaN(val2)) { return Float.compare(val1, val2); } int result; if (Math.abs(val1 - val2) < floatTol) { result = 0; } else { result = (val1 < val2 ? -1 : 1); } return result; } }
NumberOperations { private static int floatComparison(Number n1, Number n2) { float val1 = floatValue(n1); float val2 = floatValue(n2); if (Float.isInfinite(val1) || Float.isNaN(val1) || Float.isInfinite(val2) || Float.isNaN(val2)) { return Float.compare(val1, val2); } int result; if (Math.abs(val1 - val2) < floatTol) { result = 0; } else { result = (val1 < val2 ? -1 : 1); } return result; }  }
NumberOperations { private static int floatComparison(Number n1, Number n2) { float val1 = floatValue(n1); float val2 = floatValue(n2); if (Float.isInfinite(val1) || Float.isNaN(val1) || Float.isInfinite(val2) || Float.isNaN(val2)) { return Float.compare(val1, val2); } int result; if (Math.abs(val1 - val2) < floatTol) { 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 floatComparison(Number n1, Number n2) { float val1 = floatValue(n1); float val2 = floatValue(n2); if (Float.isInfinite(val1) || Float.isNaN(val1) || Float.isInfinite(val2) || Float.isNaN(val2)) { return Float.compare(val1, val2); } int result; if (Math.abs(val1 - val2) < floatTol) { 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; }