-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSqrt.java
More file actions
62 lines (57 loc) · 1.33 KB
/
Sqrt.java
File metadata and controls
62 lines (57 loc) · 1.33 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/******************************************************************************
* Compilation: javac Sqrt.java
* Execution: java Sqrt c
*
* Computes the square root of a nonnegative number c using
* Newton's method:
* - initialize t = c
* - replace t with the average of c/t and t
* - repeat until desired accuracy reached
*
* % java Sqrt 2
* 1.414213562373095
*
* % java Sqrt 1000000
* 1000.0
*
* % java Sqrt 0.4
* 0.6324555320336759
*
* % java Sqrt 1048575
* 1023.9995117186336
*
* % java Sqrt 16664444
* 4082.2106756021303
*
* % java Sqrt 0
* 0.0
*
* % java Sqrt 1e-50
* 9.999999999999999E-26
*
*
* Remarks
* ----------
* - using Math.abs() is required if c < 1
*
*
* Known bugs
* ----------
* - goes into an infinite loop if the input is negative
*
******************************************************************************/
public class Sqrt {
public static void main(String[] args) {
double c = Double.parseDouble(args[0]);
double epsilon = 1e-15;
double t = c;
if (c < 0) {
System.out.println("not support negative sqrt");
return;
}
while(Math.abs(t - c/t) > epsilon*t) {
t = (c/t + t) / 2.0;
}
System.out.println(t);
}
}