What's this precision in float?

Here is program, which should print the output of

123456

but instead its printing

123457 (6 is missing)
fn main(){
    let x:f32 = 10.1234567;
    println!("the precision is {:?}", x);
}

Also what is this single pecision and double precision?

f32 is single precision, f64 is double precision. Single precision is 24 bits, or about 7.2 decimal digits.

To add: normally(oh, what a pun!) floating point values are stored "normalized", which means that 10.1234567 is actually 1.01234567e1 (well, actually the number is normalized in binary format, not decimal and the first 1 is not stored, as it is always there, except for 0,... ), so the 6th digit after the decimal point is 5, not 6.
On the other hand, 0.011234567 normalized is 1.1234567e-2, so you do "gain precision".

The 6 is not missing. It has been rounded up to 7. Clearly the 10.1234567 has more digits than f32 can represent.

Do read: https://floating-point-gui.de/

4 Likes

Nice website, thanks. I can add https://float.exposed/0x4121f9ae to see how you number is represented in memory.

2 Likes