Skip to content

Commit 9d63f14

Browse files
committed
Add more arithmetic examples to simple-math
1 parent 76be1a4 commit 9d63f14

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

examples/simple-math.rs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ Input (0.8) with 10 decimals: 0.8 vs 0.8)
99
square 0.64
1010
From Prim: 3.300000000000000
1111
match test 33.90000000000000
12-
sum: 58.00000000000000
13-
components: 24.00000000000000, 34.00000000000000
12+
24.00000000000000 + 34.00000000000000 = 58.00000000000000
1413
sum mut: 48.00000000000000
1514
0.000000000000000 IS equal to zero
16-
divide: 24.00000000000000
15+
24.00000000000000 / 1.000000000000000 = 24.00000000000000
16+
24.0 / 1.000000000000000 = 24.0
17+
24.0 / 1.5 = 16
18+
24.000 / 1.50 = 16.0
1719
*/
1820

1921
fn main() {
@@ -35,10 +37,9 @@ fn main() {
3537
};
3638
println!("match test {}", bd_match);
3739

38-
let bd_add1 = BigDecimal::from_f64(24.0).unwrap();
39-
let bd_add2 = BigDecimal::from_f64(34.0).unwrap();
40-
println!("sum: {}", &bd_add1 + &bd_add2);
41-
println!("components: {}, {}", bd_add1, bd_add2);
40+
let bd_add1 = &BigDecimal::from_f64(24.0).unwrap();
41+
let bd_add2 = &BigDecimal::from_f64(34.0).unwrap();
42+
print_sum(bd_add1, bd_add2);
4243

4344
let mut bd_add3 = BigDecimal::from_f64(24.0).unwrap();
4445
let bd_add4 = BigDecimal::from_f64(24.0).unwrap();
@@ -52,12 +53,28 @@ fn main() {
5253
println!("{} IS equal to zero", &bd_nez);
5354
}
5455

55-
let bd_div1 = BigDecimal::from_f64(24.0).unwrap();
56-
let bd_div2 = BigDecimal::from_f64(1.0).unwrap();
57-
if bd_div2.ne(&BigDecimal::zero()) {
58-
let bd_div3: BigDecimal = bd_div1 / bd_div2;
59-
println!("divide: {}", bd_div3);
60-
} else {
61-
println!("cannot divide by zero");
62-
}
56+
let bd_div1 = &BigDecimal::from_f64(24.0).unwrap();
57+
let bd_div2 = &BigDecimal::from_f64(1.0).unwrap();
58+
print_division(bd_div1, bd_div2);
59+
60+
let bd_div1 = &BigDecimal::from_str("24.0").unwrap();
61+
print_division(bd_div1, bd_div2);
62+
63+
let bd_num = &BigDecimal::from_str("24.0").unwrap();
64+
let bd_den = &BigDecimal::from_str("1.5").unwrap();
65+
print_division(bd_num, bd_den);
66+
67+
let bd_num = &BigDecimal::from_str("24.000").unwrap();
68+
let bd_den = &BigDecimal::from_str("1.50").unwrap();
69+
print_division(bd_num, bd_den);
70+
}
71+
72+
fn print_sum(a: &BigDecimal, b: &BigDecimal) {
73+
let sum = a + b;
74+
println!("{} + {} = {}", a, b, sum);
75+
}
76+
77+
fn print_division(num: &BigDecimal, den: &BigDecimal) {
78+
let quotient = num / den;
79+
println!("{} / {} = {}", num, den, quotient);
6380
}

0 commit comments

Comments
 (0)