-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathQuadratic.java
More file actions
47 lines (40 loc) · 1.12 KB
/
Quadratic.java
File metadata and controls
47 lines (40 loc) · 1.12 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
/******************************************************************************
* Compilation: javac Quadratic.java
* Execution: java Quadatic b c
*
* ½â¶þ´Î·½³Ìʽ
* Given b and c, solves for the roots of x*x + b*x + c.
* Assumes both roots are real valued.
*
* x1 = (-b + (b*b - 4ac)^(-2)) / 2a
* x2 = (-b - (b*b - 4ac)^(-2)) / 2a
*
*
* % java Quadratic -3.0 2.0
* 2.0
* 1.0
*
* % java Quadratic -1.0 -1.0
* 1.618033988749895
* -0.6180339887498949
*
* Remark: 1.6180339... is the golden ratio.
*
* % java Quadratic 1.0 1.0
* NaN
* NaN
*
*
******************************************************************************/
public class Quadratic {
public static void main(String[] args) {
double b = Double.parseDouble(args[0]);
double c = Double.parseDouble(args[1]);
double discriminant = b * b - 4.0 * c;
double sqroot = Math.sqrt(discriminant);
double root1 = (-b + sqroot) / 2.0;
double root2 = (-b - sqroot) / 2.0;
System.out.println(root1);
System.out.println(root2);
}
}