1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)

Presentation on theme: "1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)"— Presentation transcript:

1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace

2 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3

3 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 { "1", "2", "3" } args[] { "1", "2", "3" }

4 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 args[] { "1", "2", "3" } a[] { 0.0, 0.0, 0.0 }

5 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 args[] { "1", "2", "3" } a[] { 0.0, 0.0, 0.0 } i 0

6 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 a[] { 1.0, 0.0, 0.0 } args[] { "1", "2", "3" } i 0

7 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 a[] { 1.0, 0.0, 0.0 } args[] { "1", "2", "3" } i 1

8 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 a[] { 1.0, 2.0, 0.0 } args[] { "1", "2", "3" } i 1

9 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 a[] { 1.0, 2.0, 0.0 } args[] { "1", "2", "3" } i 2

10 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" } i 2

11 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" } i 3 goes out of scope

12 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace i 0 % java Newton 1 2 3 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" }

13 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace 1.0 % java Newton 1 2 3 i 0 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" }

14 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace 1.0 c % java Newton 1 2 3 i 0 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" }

15 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 i 0 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" } c 1.0

16 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 i 0 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" } c 1.0

17 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 i 0 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" } c 1.0 t

18 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 i 0 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" } c 1.0 t

19 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 1.0 i 0 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" } c 1.0 t

20 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 1.0 i 0 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" }

21 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 1.0 i 1 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" }

22 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 1.0 i 1 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" } 2.0 c

23 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 1.0 i 1 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" } c 2.0

24 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 1.0 i 1 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" } c 2.0

25 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 1.0 i 1 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" } c 2.0 t

26 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 1.0 i 1 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" } c 2.0 t

27 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 1.0 i 1 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" } c 2.0 t 1.41

28 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 1.0 i 1 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" } c 2.0 t 1.41

29 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 1.0 i 1 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" } c 2.0 t 1.41 1.414213562373095

30 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 1.0 1.414213562373095 i 1 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" } 1.414213562373095

31 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 1.0 1.414213562373095 i 2 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" }

32 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 1.0 1.414213562373095 i 2 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" } 3.0 c

33 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 1.0 1.414213562373095 i 2 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" } c 3.0 1.7320508075688772

34 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 1.0 1.414213562373095 1.7320508075688772 i 2 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" }

35 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 1.0 1.414213562373095 1.7320508075688772 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" } i 3

36 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * 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++) System.out.println(Newton.sqrt(a[i])); } Function Call Trace % java Newton 1 2 3 1.0 1.414213562373095 1.7320508075688772 a[] { 1.0, 2.0, 3.0 } args[] { "1", "2", "3" }

Download ppt "1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)"

Similar presentations