sqrt method

DD sqrt()

Computes the positive square root of this value. If the number is NaN or negative, NaN is returned.

@return the positive square root of this number. If the argument is NaN or less than zero, the result is NaN.

Implementation

DD sqrt() {
/* Strategy:  Use Karp's trick:  if x is an approximation
  to sqrt(a), then

     sqrt(a) = a*x + [a - (a*x)^2] * x / 2   (approx)

  The approximation is accurate to twice the accuracy of x.
  Also, the multiplication (a*x) and [-]*x can be done with
  only half the precision.
 */

  if (isZero()) return valueOf(0.0);

  if (isNegative()) {
    return NaN;
  }

  double x = 1.0 / math.sqrt(hi);
  double ax = hi * x;

  DD axdd = valueOf(ax);
  DD diffSq = this.subtractDD(axdd.sqr());
  double d2 = diffSq.hi * (x * 0.5);

  return axdd.add(d2);
}