1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0)...

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

Transcript of 1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0)...

Page 1: 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)

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

Page 2: 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)

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

Page 3: 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)

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" }

Page 4: 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)

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 }

Page 5: 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)

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

Page 6: 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)

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

Page 7: 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)

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

Page 8: 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)

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

Page 9: 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)

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

Page 10: 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)

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

Page 11: 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)

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

3goes outof scope

Page 12: 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)

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" }

Page 13: 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)

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

1.0

% java Newton 1 2 3

i

0

a[]

{ 1.0, 2.0, 3.0 }

args[]

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

Page 14: 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)

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

1.0

c

1.0

% java Newton 1 2 3

i

0

a[]

{ 1.0, 2.0, 3.0 }

args[]

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

Page 15: 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)

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

Page 16: 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)

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

Page 17: 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)

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

1.0

Page 18: 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)

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

1.0

Page 19: 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)

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 31.0

1.0

i

0

a[]

{ 1.0, 2.0, 3.0 }

args[]

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

c

1.0

t

1.0

Page 20: 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)

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 31.0

1.0

i

0

a[]

{ 1.0, 2.0, 3.0 }

args[]

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

Page 21: 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)

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 31.0

i

1

a[]

{ 1.0, 2.0, 3.0 }

args[]

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

Page 22: 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)

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 31.0

i

1

a[]

{ 1.0, 2.0, 3.0 }

args[]

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

2.0

2.0

c

2.0

Page 23: 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)

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 31.0

i

1

a[]

{ 1.0, 2.0, 3.0 }

args[]

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

c

2.0

Page 24: 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)

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 31.0

i

1

a[]

{ 1.0, 2.0, 3.0 }

args[]

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

c

2.0

Page 25: 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)

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 31.0

i

1

a[]

{ 1.0, 2.0, 3.0 }

args[]

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

c

2.0

t

2.0

Page 26: 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)

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 31.0

i

1

a[]

{ 1.0, 2.0, 3.0 }

args[]

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

c

2.0

t

2.0

Page 27: 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)

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 31.0

i

1

a[]

{ 1.0, 2.0, 3.0 }

args[]

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

c

2.0

t

1.41

Page 28: 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)

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 31.0

i

1

a[]

{ 1.0, 2.0, 3.0 }

args[]

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

c

2.0

t

1.41

Page 29: 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)

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

Page 30: 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)

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 31.01.414213562373095

i

1

a[]

{ 1.0, 2.0, 3.0 }

args[]

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

1.414213562373095

Page 31: 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)

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 31.01.414213562373095

i

2

a[]

{ 1.0, 2.0, 3.0 }

args[]

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

Page 32: 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)

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 31.01.414213562373095

i

2

a[]

{ 1.0, 2.0, 3.0 }

args[]

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

3.0

3.0

c

3.0

Page 33: 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)

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 31.01.414213562373095

i

2

a[]

{ 1.0, 2.0, 3.0 }

args[]

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

c

3.0

1.7320508075688772

1.7320508075688772

Page 34: 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)

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 31.01.4142135623730951.7320508075688772

i

2

a[]

{ 1.0, 2.0, 3.0 }

args[]

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

Page 35: 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)

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 31.01.4142135623730951.7320508075688772

a[]

{ 1.0, 2.0, 3.0 }

args[]

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

i

3

Page 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)

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 31.01.4142135623730951.7320508075688772

a[]

{ 1.0, 2.0, 3.0 }

args[]

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