36
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(sqrt(a[i])); } } Function Call Trace

Function Call Trace

  • Upload
    gazit

  • View
    42

  • Download
    0

Embed Size (px)

DESCRIPTION

Function Call Trace. 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 ; - PowerPoint PPT Presentation

Citation preview

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(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(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(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(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(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(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(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(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(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(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(sqrt(a[i])); }}

Function Call Trace

% java Newton 1 2 3

a[]

{ 1.0, 2.0, 3.0 }

args[]

{ "1", "2", "3" }

i

3goes outof 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(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(sqrt(a[i])); }}

Function Call Trace

1.0

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(sqrt(a[i])); }}

Function Call Trace

1.0

c

1.0

% java Newton 1 2 3

i

0

a[]

{ 1.0, 2.0, 3.0 }

args[]

{ "1", "2", "3" }

1.0

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(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(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(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

1.0

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(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

1.0

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(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

1.0

1.0

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(sqrt(a[i])); }}

Function Call Trace

% java Newton 1 2 31.0

i

0

a[]

{ 1.0, 2.0, 3.0 }

args[]

{ "1", "2", "3" }

1.0

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(sqrt(a[i])); }}

Function Call Trace

% java Newton 1 2 31.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(sqrt(a[i])); }}

Function Call Trace

% java Newton 1 2 31.0

i

1

a[]

{ 1.0, 2.0, 3.0 }

args[]

{ "1", "2", "3" }

2.0

c

2.0

2.0

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(sqrt(a[i])); }}

Function Call Trace

% java Newton 1 2 31.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(sqrt(a[i])); }}

Function Call Trace

% java Newton 1 2 31.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(sqrt(a[i])); }}

Function Call Trace

% java Newton 1 2 31.0

i

1

a[]

{ 1.0, 2.0, 3.0 }

args[]

{ "1", "2", "3" }

c

2.0

t

2.0

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(sqrt(a[i])); }}

Function Call Trace

% java Newton 1 2 31.0

i

1

a[]

{ 1.0, 2.0, 3.0 }

args[]

{ "1", "2", "3" }

c

2.0

t

2.0

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(sqrt(a[i])); }}

Function Call Trace

% java Newton 1 2 31.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(sqrt(a[i])); }}

Function Call Trace

% java Newton 1 2 31.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(sqrt(a[i])); }}

Function Call Trace

% java Newton 1 2 31.0

i

1

a[]

{ 1.0, 2.0, 3.0 }

args[]

{ "1", "2", "3" }

c

2.0

t

1.41

1.414213562373095

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(sqrt(a[i])); }}

Function Call Trace

% java Newton 1 2 31.01.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(sqrt(a[i])); }}

Function Call Trace

% java Newton 1 2 31.01.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(sqrt(a[i])); }}

Function Call Trace

% java Newton 1 2 31.01.414213562373095

i

2

a[]

{ 1.0, 2.0, 3.0 }

args[]

{ "1", "2", "3" }

3.0

c

3.0

3.0

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(sqrt(a[i])); }}

Function Call Trace

% java Newton 1 2 31.01.414213562373095

i

2

a[]

{ 1.0, 2.0, 3.0 }

args[]

{ "1", "2", "3" }

c

3.0

1.7320508075688772

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(sqrt(a[i])); }}

Function Call Trace

% java Newton 1 2 31.01.4142135623730951.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(sqrt(a[i])); }}

Function Call Trace

% java Newton 1 2 31.01.4142135623730951.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(sqrt(a[i])); }}

Function Call Trace

% java Newton 1 2 31.01.4142135623730951.7320508075688772

a[]

{ 1.0, 2.0, 3.0 }

args[]

{ "1", "2", "3" }