/** * Converts the <tt>double</tt> argument to a DoubleDouble number. * * @param x a numeric value * @return the extended precision version of the value */ public static DD valueOf(double x) { return new DD(x); }
/** * Creates a new DoubleDouble with the value of the argument. * * @param dd the DoubleDouble value to copy * @return a copy of the input value */ public static DD copy(DD dd) { return new DD(dd); }
private static DD createNaN() { return new DD(Double.NaN, Double.NaN); }
/** * Computes a new DoubleDouble whose value is <tt>(this / y)</tt>. * * @param y the divisor * @return a new object with the value <tt>(this / y)</tt> */ public final DD divide(DD y) { double hc, tc, hy, ty, C, c, U, u; C = hi/y.hi; c = SPLIT*C; hc =c-C; u = SPLIT*y.hi; hc = c-hc; tc = C-hc; hy = u-y.hi; U = C * y.hi; hy = u-hy; ty = y.hi-hy; u = (((hc*hy-U)+hc*ty)+tc*hy)+tc*ty; c = ((((hi-U)-u)+lo)-C*y.lo)/y.hi; u = C+c; double zhi = u; double zlo = (C-u)+c; return new DD(zhi, zlo); }
/** * Returns a new DoubleDouble whose value is <tt>-this</tt>. * * @return <tt>-this</tt> */ public final DD negate() { if (isNaN()) return this; return new DD(-hi, -lo); }
private void checkParse(String str, double expectedVal, double errBound) { checkParse(str, new DD(expectedVal), errBound); }
/** * Returns the absolute value of this value. * Special cases: * <ul> * <li>If this value is NaN, it is returned. * </ul> * * @return the absolute value of this value */ public DD abs() { if (isNaN()) return NaN; if (isNegative()) return negate(); return new DD(this); }
public void testAddMult2() { checkAddMult2(new DD(3)); checkAddMult2(DD.PI); }
private DD slowPow(DD x, int exp) { if (exp == 0) return DD.valueOf(1.0); int n = Math.abs(exp); // MD - could use binary exponentiation for better precision & speed DD pow = new DD(x); for (int i = 1; i < n; i++) { pow = pow.multiply(x); } if (exp < 0) { return pow.reciprocal(); } return pow; }
public void testSetValueDouble() { assertTrue(VALUE_DBL == (new DD(1)).setValue(VALUE_DBL).doubleValue()); } public void testSetValueDD()
private void checkAddMult2(DD dd) { DD sum = dd.add(dd); DD prod = dd.multiply(new DD(2.0)); checkErrorBound("AddMult2", sum, prod, 0.0); }
public void testSqrt() { // the appropriate error bound is determined empirically checkSqrt(DD.PI, 1e-30); checkSqrt(DD.E, 1e-30); checkSqrt(new DD(999.0), 1e-28); }
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); }
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 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; }
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); }