/** * Computes twice the area of the oriented triangle (a, b, c), i.e., the area * is positive if the triangle is oriented counterclockwise. * The computation uses {@link DD} arithmetic for robustness. * * @param ax the x ordinate of a vertex of the triangle * @param ay the y ordinate of a vertex of the triangle * @param bx the x ordinate of a vertex of the triangle * @param by the y ordinate of a vertex of the triangle * @param cx the x ordinate of a vertex of the triangle * @param cy the y ordinate of a vertex of the triangle */ public static DD triAreaDDSlow(DD ax, DD ay, DD bx, DD by, DD cx, DD cy) { return (bx.subtract(ax).multiply(cy.subtract(ay)).subtract(by.subtract(ay) .multiply(cx.subtract(ax)))); }
/** * Computes twice the area of the oriented triangle (a, b, c), i.e., the area * is positive if the triangle is oriented counterclockwise. * The computation uses {@link DD} arithmetic for robustness. * * @param ax the x ordinate of a vertex of the triangle * @param ay the y ordinate of a vertex of the triangle * @param bx the x ordinate of a vertex of the triangle * @param by the y ordinate of a vertex of the triangle * @param cx the x ordinate of a vertex of the triangle * @param cy the y ordinate of a vertex of the triangle */ public static DD triAreaDD(DD ax, DD ay, DD bx, DD by, DD cx, DD cy) { return (bx.subtract(ax).multiply(cy.subtract(ay)).subtract(by.subtract(ay) .multiply(cx.subtract(ax)))); }
private DD delta(DD x, DD y) { return x.subtract(y).abs(); }
public void testEByTaylorSeries() { //System.out.println("--------------------------------"); //System.out.println("Computing e by Taylor series"); DD testE = computeEByTaylorSeries(); double err = Math.abs(testE.subtract(DD.E).doubleValue()); //System.out.println("Difference from DoubleDouble.E = " + err); assertTrue(err < 64 * DD.EPS); }
public void testPiByMachin() { //System.out.println("--------------------------------"); //System.out.println("Computing Pi by Machin's rule"); DD testE = computePiByMachin(); double err = Math.abs(testE.subtract(DD.PI).doubleValue()); //System.out.println("Difference from DoubleDouble.PI = " + err); assertTrue(err < 8 * DD.EPS); }
private void checkErrorBound(String tag, DD x, DD y, double errBound) { DD err = x.subtract(y).abs(); //System.out.println(tag + " err=" + err); boolean isWithinEps = err.doubleValue() <= errBound; assertTrue(isWithinEps); }
private void checkPow(double x, int exp, double errBound) { DD xdd = new DD(x); DD pow = xdd.pow(exp); //System.out.println("Pow(" + x + ", " + exp + ") = " + pow); DD pow2 = slowPow(xdd, exp); double err = pow.subtract(pow2).doubleValue(); boolean isOK = err < errBound; if (! isOK) System.out.println("Test slowPow value " + pow2); assertTrue(err <= errBound); }
private void checkParse(String str, DD expectedVal, double relErrBound) { DD xdd = DD.parse(str); double err = xdd.subtract(expectedVal).doubleValue(); double xddd = xdd.doubleValue(); double relErr = xddd == 0d ? err : Math.abs(err / xddd); //System.out.println("Parsed= " + xdd + " rel err= " + relErr); assertTrue("parsing '" + str + "' results in " + xdd.toString() + " ( " + xdd.dump() + ") != " + expectedVal + "\n err =" + err + ", relerr =" + relErr, relErr <= relErrBound); }
private void checkReciprocal(double x, double errBound) { DD xdd = new DD(x); DD rr = xdd.reciprocal().reciprocal(); double err = xdd.subtract(rr).doubleValue(); //System.out.println("DD Recip = " + xdd // + " DD delta= " + err // + " double recip delta= " + (x - 1.0/(1.0/x)) ); assertTrue(err <= errBound); }
DD distFrom1DD = xSqrt.subtract(DD.valueOf(1.0)); double distFrom1 = distFrom1DD.doubleValue(); if (Math.abs(distFrom1) < 1.0e-40)
public void testTrunc() { checkTrunc(DD.valueOf(1e16).subtract(DD.valueOf(1)), DD.valueOf(1e16).subtract(DD.valueOf(1))); // the appropriate error bound is determined empirically checkTrunc(DD.PI, DD.valueOf(3)); checkTrunc(DD.valueOf(999.999), DD.valueOf(999)); checkTrunc(DD.E.negate(), DD.valueOf(-2)); checkTrunc(DD.valueOf(-999.999), DD.valueOf(-999)); }
public double xrunDoubleDoubleSelf(int nIter) { Stopwatch sw = new Stopwatch(); for (int i = 0; i < nIter; i++) { DD a = new DD(9.0); DD factor = new DD(10.0); DD aMul = factor.multiply(a); DD aDiv = a.divide(factor); DD det = a.multiply(a) .subtract(aMul.multiply(aDiv)); // System.out.println(aDiv); // System.out.println(det); } sw.stop(); System.out.println("DD: nIter = " + nIter + " time = " + sw.getTimeString()); return sw.getTime() / (double) nIter; }
public double runDoubleDouble(int nIter) { Stopwatch sw = new Stopwatch(); for (int i = 0; i < nIter; i++) { DD a = new DD(9.0); DD factor = new DD(10.0); DD aMul = factor.multiply(a); DD aDiv = a.divide(factor); DD det = a.multiply(a) .subtract(aMul.multiply(aDiv)); // System.out.println(aDiv); // System.out.println(det); } sw.stop(); System.out.println("DD: nIter = " + nIter + " time = " + sw.getTimeString()); return sw.getTime() / (double) nIter; }
public void testWriteStandardNotation() { // standard cases checkStandardNotation(1.0, "1.0"); checkStandardNotation(0.0, "0.0"); // cases where hi is a power of 10 and lo is negative checkStandardNotation(DD.valueOf(1e12).subtract(DD.valueOf(1)), "999999999999.0"); checkStandardNotation(DD.valueOf(1e14).subtract(DD.valueOf(1)), "99999999999999.0"); checkStandardNotation(DD.valueOf(1e16).subtract(DD.valueOf(1)), "9999999999999999.0"); DD num8Dec = DD.valueOf(-379363639).divide( DD.valueOf(100000000)); checkStandardNotation(num8Dec, "-3.79363639"); checkStandardNotation(new DD(-3.79363639, 8.039137357367426E-17), "-3.7936363900000000000000000"); checkStandardNotation(DD.valueOf(34).divide( DD.valueOf(1000)), "0.034"); checkStandardNotation(1.05e3, "1050.0"); checkStandardNotation(0.34, "0.34000000000000002442490654175344"); checkStandardNotation(DD.valueOf(34).divide( DD.valueOf(100)), "0.34"); checkStandardNotation(14, "14.0"); }
k++; if (sign < 0) at = at.subtract(t.divide(d)); else at = at.add(t.divide(d));
/** * Uses Machin's arctangent formula to compute Pi: * * Pi / 4 = 4 arctan(1/5) - arctan(1/239) * * @return an approximation to Pi */ private DD computePiByMachin() { DD t1 = DD.valueOf(1.0).divide(DD.valueOf(5.0)); DD t2 = DD.valueOf(1.0).divide(DD.valueOf(239.0)); DD pi4 = (DD.valueOf(4.0) .multiply(arctan(t1))) .subtract(arctan(t2)); DD pi = DD.valueOf(4.0).multiply(pi4); //System.out.println("Computed value = " + pi); return pi; }