1 / 36

Function Call Trace

This Java program demonstrates the implementation of Square Root calculation using Newton's method. The method, defined as `sqrt`, takes a double value and uses an iterative approach for computation. It incorporates a convergence criterion based on a small epsilon value (1E-15) to ensure accuracy. It handles edge cases, such as negative inputs, returning `Double.NaN` for invalid values. The main function processes command-line arguments to compute square roots for multiple input values, printing each result to the standard output.

vangie
Télécharger la présentation

Function Call Trace

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Function Call Trace publicclass Newton { public staticdouble 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])); } }

  2. Function Call Trace publicclass Newton { public staticdouble 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])); } } % java Newton 1 2 3

  3. Function Call Trace publicclass Newton { public staticdouble 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])); } } { "1", "2", "3" } args[] { "1", "2", "3" } % java Newton 1 2 3

  4. Function Call Trace publicclass Newton { public staticdouble 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])); } } args[] { "1", "2", "3" } a[] { 0.0, 0.0, 0.0 } % java Newton 1 2 3

  5. Function Call Trace publicclass Newton { public staticdouble 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])); } } args[] { "1", "2", "3" } a[] { 0.0, 0.0, 0.0 } i 0 % java Newton 1 2 3

  6. Function Call Trace publicclass Newton { public staticdouble 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])); } } args[] { "1", "2", "3" } a[] { 1.0, 0.0, 0.0 } i 0 % java Newton 1 2 3

  7. Function Call Trace publicclass Newton { public staticdouble 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])); } } args[] { "1", "2", "3" } a[] { 1.0, 0.0, 0.0 } i 1 % java Newton 1 2 3

  8. Function Call Trace publicclass Newton { public staticdouble 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])); } } args[] { "1", "2", "3" } a[] { 1.0, 2.0, 0.0 } i 1 % java Newton 1 2 3

  9. Function Call Trace publicclass Newton { public staticdouble 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])); } } args[] { "1", "2", "3" } a[] { 1.0, 2.0, 0.0 } i 2 % java Newton 1 2 3

  10. Function Call Trace publicclass Newton { public staticdouble 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])); } } args[] { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 2 % java Newton 1 2 3

  11. Function Call Trace publicclass Newton { public staticdouble 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])); } } args[] { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 3 goes outof scope % java Newton 1 2 3

  12. Function Call Trace publicclass Newton { public staticdouble 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])); } } args[] { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 0 % java Newton 1 2 3

  13. Function Call Trace publicclass Newton { public staticdouble 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])); } } 1.0 args[] { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 0 1.0 % java Newton 1 2 3

  14. Function Call Trace publicclass Newton { public staticdouble 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])); } } 1.0 c 1.0 args[] { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 0 1.0 % java Newton 1 2 3

  15. Function Call Trace publicclass Newton { public staticdouble 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])); } } c 1.0 args[] { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 0 % java Newton 1 2 3

  16. Function Call Trace publicclass Newton { public staticdouble 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])); } } c 1.0 args[] { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 0 % java Newton 1 2 3

  17. Function Call Trace publicclass Newton { public staticdouble 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])); } } c t 1.0 1.0 args[] { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 0 % java Newton 1 2 3

  18. Function Call Trace publicclass Newton { public staticdouble 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])); } } c t 1.0 1.0 args[] { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 0 % java Newton 1 2 3

  19. Function Call Trace publicclass Newton { public staticdouble 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])); } } c t 1.0 1.0 args[] 1.0 { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 0 1.0 % java Newton 1 2 3

  20. Function Call Trace publicclass Newton { public staticdouble 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])); } } args[] { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 0 1.0 % java Newton 1 2 3 1.0

  21. Function Call Trace publicclass Newton { public staticdouble 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])); } } args[] { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 1 % java Newton 1 2 3 1.0

  22. Function Call Trace publicclass Newton { public staticdouble 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])); } } 2.0 c 2.0 args[] { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 1 2.0 % java Newton 1 2 3 1.0

  23. Function Call Trace publicclass Newton { public staticdouble 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])); } } c 2.0 args[] { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 1 % java Newton 1 2 3 1.0

  24. Function Call Trace publicclass Newton { public staticdouble 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])); } } c 2.0 args[] { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 1 % java Newton 1 2 3 1.0

  25. Function Call Trace publicclass Newton { public staticdouble 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])); } } c t 2.0 2.0 args[] { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 1 % java Newton 1 2 3 1.0

  26. Function Call Trace publicclass Newton { public staticdouble 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])); } } c t 2.0 2.0 args[] { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 1 % java Newton 1 2 3 1.0

  27. Function Call Trace publicclass Newton { public staticdouble 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])); } } c t 2.0 1.41 args[] { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 1 % java Newton 1 2 3 1.0

  28. Function Call Trace publicclass Newton { public staticdouble 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])); } } c t 2.0 1.41 args[] { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 1 % java Newton 1 2 3 1.0

  29. Function Call Trace publicclass Newton { public staticdouble 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])); } } c t 2.0 1.41 args[] 1.414213562373095 { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 1 1.414213562373095 % java Newton 1 2 3 1.0

  30. Function Call Trace publicclass Newton { public staticdouble 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])); } } args[] { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 1 1.414213562373095 % java Newton 1 2 3 1.0 1.414213562373095

  31. Function Call Trace publicclass Newton { public staticdouble 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])); } } args[] { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 2 % java Newton 1 2 3 1.0 1.414213562373095

  32. Function Call Trace publicclass Newton { public staticdouble 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])); } } 3.0 c 3.0 args[] { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 2 3.0 % java Newton 1 2 3 1.0 1.414213562373095

  33. Function Call Trace publicclass Newton { public staticdouble 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])); } } c 3.0 args[] 1.7320508075688772 { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 2 1.7320508075688772 % java Newton 1 2 3 1.0 1.414213562373095

  34. Function Call Trace publicclass Newton { public staticdouble 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])); } } args[] { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 2 % java Newton 1 2 3 1.0 1.414213562373095 1.7320508075688772

  35. Function Call Trace publicclass Newton { public staticdouble 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])); } } args[] { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } i 3 % java Newton 1 2 3 1.0 1.414213562373095 1.7320508075688772

  36. Function Call Trace publicclass Newton { public staticdouble 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])); } } args[] { "1", "2", "3" } a[] { 1.0, 2.0, 3.0 } % java Newton 1 2 3 1.0 1.414213562373095 1.7320508075688772

More Related