When using a floating-point while loop counter, an accumulation of rounding errors may result in a mismatch between the expected and
actual number of iterations.
Even if floating-point loop counters appears to behave correctly on one implementation, it may give a different number of iterations on another implementation.
To fix the issue, use an integer loop counter instead. This avoids the accumulation of rounding errors and ensures that the loop iterates the expected number of times.
fn main() {
let mut counter = 0.0;
while counter < 10.0 {
println!("Counter: {}", counter);
counter += 1.0;
}
}
fn main() {
let mut counter = 0;
while counter < 10 {
println!("Counter: {}", counter);
counter += 1;
}
}