CS3343/3341
 Analysis of Algorithms 
  Newton's Method to   
  Perform Cube Root  

Newton's Method, Cube Root Outputs of tests
// CubeRoot: use Newton's method for cube_root(r)
//  includes normalization.
public class CubeRoot {

   private double mul; // denormalizing factor

   // norm: normalize to 0.125 <= r <= 1.0
   public double norm(double r) {
      // divide d by power of 2
      mul = 1.0; // power of 2
      double r0 = r;
      while (r0 > 1) {
         // divide by 8, will
         // later multiply by 2
/*1*/    r0 = r0/8.0;
         mul = mul*2.0;
      }
/*2*/ while (r0 < 0.125) {
/*3*/    r0 = r0*8.0;
         mul = mul/2.0;
      }
      // r0 now normalized
      return r0;
   }

   // denorm: reverse the normalization
   public double denorm(double qrtR0) {
/*4*/ return qrtR0*mul;
   }

   // calculate qrt(r0), 0.125 <= d0 <= 1
   public double qrt0(double r0) {
/*5*/ double x0 = 0.571428*r0 + 0.4682788;
      System.out.println("1st approx to qrt(r0):"+ x0);
      double x1;
      for ( ; ; ) {
/*6*/    x1 = 2.0*x0/3.0 + r0/(3.0*x0*x0);
         System.out.println(x1);
         if (Math.abs(x1 - x0) < 1.0e-7) break;
         x0 = x1;
      }
      return x1;   
   }

   public static void main(String[] args) {
      // calculate qrt(r) for any r > 0
      CubeRoot root = new CubeRoot();
      double r = Double.parseDouble(args[0]);
      System.out.println("r:" + r + ", want qrt(r)");
      // normalize to 0.125 <= r<= 1
      double r0 = root.norm(r);
      // r0 now normalized
      System.out.println("normalized, r0:" + r0 +
         ", want qrt(r0)");
      double qrtR0 = root.qrt0(r0);
      // denoramlize: multipy qrtR0 by mul
      double qrtR = root.denorm(qrtR0);
      System.out.println("qrt(" + r + ") = \t" + qrtR);
      System.out.println("check on ans:\t" +
         Math.pow(r, 1.0/3.0));
   }
}
13
r:13.0, want qrt(r)
normalized, r0:0.203125, want qrt(r0)
1st approx to qrt(r0):0.5843501125
0.587854480175757
0.5878336726667287
0.5878336719301894
qrt(13.0) =     2.3513346877207577
check on ans:   2.3513346877207573

3.141592653589792 r:3.141592653589792, want qrt(r) normalized, r0:0.392699081698724, want qrt(r0) 1st approx to qrt(r0):0.6926780508569385 0.7346050996164353 0.7323031947698995 0.7322959438525579 0.7322959437807616 qrt(3.141592653589792) = 1.4645918875615231 check on ans: 1.464591887561523
0.003247 r:0.003247, want qrt(r) normalized, r0:0.207808, want qrt(r0) 1st approx to qrt(r0):0.5870261098239999 0.5923646776700325 0.5923168539692227 0.5923168501077263 qrt(0.003247) = 0.14807921252693157 check on ans: 0.14807921252693157
2.718281828459045E-8 r:2.718281828459045E-8, want qrt(r) normalized, r0:0.4560520138493235, want qrt(r0) 1st approx to qrt(r0):0.7288796901698913 0.7720616726970644 0.7697365283217061 0.7697294906102655 0.7697294905459187 qrt(2.718281828459045E-8) = 0.0030067558224449948 check on ans: 0.0030067558224449956
4.812118250596034E14 r:4.812118250596034E14, want qrt(r) normalized, r0:0.21370097916122582, want qrt(r0) 1st approx to qrt(r0):0.5903935231201409 0.5979585969466128 0.5978636946532636 0.5978636795872904 qrt(4.812118250596034E14) = 78363.18821086532 check on ans: 78363.18821086522

(Revision date: 2014-06-06. Please use ISO 8601, the International Standard.)