A simple question about floating point addition

hello forum, simple question why i cannot reach one in this simple loop ?
thx Julien.

fn main() {
    let mut ct: f32 = 0.0;
    while ct <= 1.0 {
        print!("-->{}\n", ct);
        ct += 0.1;





   Compiling playground v0.0.1 (/playground)
    Finished dev [unoptimized + debuginfo] target(s) in 1.70s
     Running `target/debug/playground`

Roundoff error. If you step by an integer times a power of two it will work as you'd expect, e.g. stepping by 0.125.

The same thing happens in any programming language, BTW.


thanks ^^
quick overview:

"What Every Computer Scientist Should Know About Floating-Point Arithmetic" :

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.