-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewton.java
More file actions
39 lines (28 loc) · 955 Bytes
/
Newton.java
File metadata and controls
39 lines (28 loc) · 955 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class Newton {
// return the square root of c, computed using Newton's method
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;
}
// overloaded version in which user specifies the error tolerance EPS
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;
}
// test client
public static void main(String[] args) {
// parse command-line parameters
double[] a = new double[args.length];
for (int i = 0; i < args.length; i++) {
a[i] = Double.parseDouble(args[i]);
}
System.out.println(sqrt(49));
}
}