|
|
|
|
|
by gnuvince
2759 days ago
|
|
Because of integer overflows and floating-point operations, the notion of equivalent mathematical expressions is tricky. fn main() {
let a: i8 = 125;
let b: i8 = 3;
let c: i8 = (a + b) / 2;
let d: i8 = b + ((a - b) / 2);
println!("{} {}", c, d);
}
This program outputs `-64 64` although the computations of `c` and `d` are equivalent.Here's another example using floating point numbers: fn main() {
let mut total1: f32 = 0.0;
let mut total2: f32 = 0.0;
let mut counter1: f32 = 0.0;
let mut counter2: f32 = 100.0;
for _ in 0 .. 10001 {
total1 += counter1;
total2 += counter2;
counter1 += 0.01;
counter2 -= 0.01;
}
println!("{} {}", total1, total2);
}
The output of this program is `500041.16 500012.16`, a difference of 25 for a program that computes the same result (unless I made a mistake). |
|