-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy path12_enum_quad.rs
More file actions
34 lines (31 loc) · 784 Bytes
/
Copy path12_enum_quad.rs
File metadata and controls
34 lines (31 loc) · 784 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
enum Solution {
Double(f64, f64),
Single(f64),
NoSolution,
}
fn quad (a : f64, b : f64, c : f64) -> Solution
{
if a == 0.0 {
Solution::Single(-1.0 * c / b)
}
else if b*b - 4.0 * a * c < 0.0 {
Solution::NoSolution
}
else if b * b == 4.0 * a * c {
Solution::Single(-1.0 * b / (2.0 * a))
}
else {
Solution::Double((-1.0 * b + (b*b - 4.0 * a * c).sqrt()) / (2.0 * a),
(-1.0 * b - (b*b - 4.0 * a * c).sqrt()) / (2.0 * a))
}
}
fn main ()
{
let s : Solution ;
s = quad(1.0, 2.0, 1.0) ;
match s {
Solution::NoSolution => println!("Unsolvable"),
Solution::Single(x) => println!("{}", x),
Solution::Double(x, y) => println!("{}, {}", x, y)
}
}