I have the number 1255413760 which is exactly represented in an f32 but if i println it I get 1255413800. Anything I can do about that :)?
Funnily enough it does round trip back to 1255413760 since that is the closest.
#![feature(float_extras)]
#![allow(deprecated)]
use std::i64;
fn main(){
let f: f32 = "1255413760".parse().unwrap();
let (mantissa, exponent, sign) = f.integer_decode();
let i = sign as i64 * (mantissa as i64) * i64::pow(2, exponent as u32);
println!("m {} e {} s{}", mantissa, exponent, sign);
println!("i {}", i);
println!("f {}", f)
}
It's most likely a bug, as it prints the right number when parsed as f64 without formatting. I'm not sure where to file a bug but you should definitely open an issue.
(I've written the same response to #39428 just in case.)
This is intentional and is not an error (I've written the initial float-to-decimal routine). The point is that {} prints the closest decimal representation with the least significant digits that still round trips---having only 24 bits (7--8 decimal digits) of mantissa, this is expected. As you've discovered, giving the exact precision disables this behavior and prints the exact representation.
It might be argued that {} should behave just like {.0}. I personally disagree because the omission of useless digits is mandatory for more extreme cases:
For your information, {} will print the former as 898846567431158 followed by 293 zeroes, and the later as simply 0.0...05 with 323 zeroes after a decimal point, both are accurate and do not give the false sense of accuracy. Of course one can also argue that they should print in the scientific notation when placeholder zeroes starts to occur... Probably that should be another issue