Friday, January 30, 2015

Newton.java in programe

Newton.java

           public class Newton
{
              public static double sqrt(double c) 
{
        if (c < 0) return Double.NaN;
        double EPS = 1E-15;
        double t = c;
        while (Math.abs(t - c/t) > EPS*t)
            t = (c/t + t) / 2.0;
        return t;
    }
                 public static double sqrt(double c, double EPS)
 {
        if (c < 0) return Double.NaN;
        double t = c;
        while (Math.abs(t - c/t) > EPS*t)
        t = (c/t + t) / 2.0;
        return t;
    }
                public static void main(String[] args) 
               {
                double[] a = new double[args.length];
        for (int i = 0; i < args.length; i++) 
  {
            a[i] = Double.parseDouble(args[i]);
   }
                 for (int i = 0; i < a.length; i++) 
       {
            double x = sqrt(a[i]);
            StdOut.println(x);
        }
    }
}

No comments:

Post a Comment