/** * Checks if two double precision floating point numbers are approximately "equal" * Default values are used for tolerances * @param val1 the first value * @param val2 the second value * @return true if they are considered equal, else false */ public static boolean SingleValueFuzzyEquals(double val1, double val2) { return SingleValueFuzzyEquals(val1, val2, default_tolerance, default_tolerance); }
/** * Checks if two double precision floating point arrays are approximately "equal" * Equal means the arrays have values the are considered fuzzy equals appearing in the same order * and the arrays the same length. * * @param arr1 the first value * @param arr2 the second value * @param maxabserror determines the minimum threshold for "equal" in terms of the two numbers being very small in magnitude. * @param maxrelerror determines the minimum threshold for "equal" in terms of the relative magnitude of the numbers. * i.e. invariant of the magnitude of the numbers what is the maximum level of magnitude difference acceptable. * @return true if they are considered equal, else false */ public static boolean ArrayFuzzyEquals(double[] arr1, double[] arr2, double maxabserror, double maxrelerror) { if (arr1.length != arr2.length) { return false; } for (int i = 0; i < arr1.length; i++) { if (!SingleValueFuzzyEquals(arr1[i], arr2[i], maxabserror, maxrelerror)) return false; } return true; }
public void testGetNorm() { assertTrue(FuzzyEquals.SingleValueFuzzyEquals(result.getNorm(), 105.381175990066)); }
public void testGetDeterminant() { assertTrue(FuzzyEquals.SingleValueFuzzyEquals(result.getDeterminant(), 1613942.00000000)); }
public void testgetConditionNumber() { assertTrue(FuzzyEquals.SingleValueFuzzyEquals(result.getConditionNumber(), 13.3945104660836)); }
@Test public void EqualsTest_SingleValueFuzzyEqualsDouble() { double NaN = getNaN(); double pinf = getPosInf(); double ninf = getNegInf(); double neg0 = getNegZero(); // NaN branch assertFalse(FuzzyEquals.SingleValueFuzzyEquals(NaN, NaN)); assertFalse(FuzzyEquals.SingleValueFuzzyEquals(NaN, 1)); assertFalse(FuzzyEquals.SingleValueFuzzyEquals(1, NaN)); // Inf branches assertTrue(FuzzyEquals.SingleValueFuzzyEquals(pinf, pinf)); assertTrue(FuzzyEquals.SingleValueFuzzyEquals(ninf, ninf)); assertFalse(FuzzyEquals.SingleValueFuzzyEquals(pinf, ninf)); assertFalse(FuzzyEquals.SingleValueFuzzyEquals(ninf, pinf)); assertFalse(FuzzyEquals.SingleValueFuzzyEquals(pinf, Double.MAX_VALUE)); assertFalse(FuzzyEquals.SingleValueFuzzyEquals(ninf, -Double.MAX_VALUE)); // val 0 branches assertTrue(FuzzyEquals.SingleValueFuzzyEquals(0.e0, 0.e0)); assertTrue(FuzzyEquals.SingleValueFuzzyEquals(0.e0, neg0)); assertTrue(FuzzyEquals.SingleValueFuzzyEquals(neg0, 0.e0)); assertTrue(FuzzyEquals.SingleValueFuzzyEquals(neg0, neg0)); // same value as it trips the return true on "difference less than abs tol" branch assertTrue(FuzzyEquals.SingleValueFuzzyEquals(FuzzyEquals.getEps(), 2.e0 * FuzzyEquals.getEps())); // same value as it trips the return true on "difference less than relative error" branch assertTrue(FuzzyEquals.SingleValueFuzzyEquals(1.e308, 9.99999999999999e0 * 1.e307)); // fail, just plain different assertFalse(FuzzyEquals.SingleValueFuzzyEquals(1.e0, 2.e0)); }