-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
scientific notation incorrect decimal places #115737
Comments
No. The relevant code is actually the one for formatting integers in scientific notation, not floats. I. e. the relevant code is the code first introduced in #66721 As I mentioned in the linked URLO thread, I believe perhaps integer formatting in scientific notation should be fully identically behaving to the (seemingly "correct", and also historically older) formatting implementation for floats. This is a potential test we might want passing: let [mut s1, mut s2]: [String; 2] = Default::default();
for x in -2000_i64..=2000 {
let xf = x as f64;
macro_rules! test {
($fmt:literal, $assert_fmt:literal) => {
s1.clear();
s2.clear();
write!(&mut s1, $fmt, x).unwrap();
write!(&mut s2, $fmt, xf).unwrap();
assert_eq!(
&s1,
&s2,
"exponential formatting of integers vs. integral floats\nformat!(\"{}\", {})",
format_args!($assert_fmt),
x
);
};
}
test!("{:e}", "{{:e}}");
test!("{:E}", "{{:E}}");
for n in 0..5 {
test!("{:.n$e}", "{{:.{n}e}}");
test!("{:.n$E}", "{{:.{n}E}}");
}
} but as of now, this kind of test fails in a lot of cases:
In this test result, besides the behavior described in this issue, resulting in the wrong number of digits, there’s also the rounding behavior that is different between displaying floats and ints here. I’m not sure whether or not this difference is wanted. If it is wanted, it should be properly documented (on that note, I couldn’t easily find any documentation on the rounding used for floats, either), if it isn’t, it would be yet another issue or bug. |
I would like to try and fix this bug |
fix rounding issue with exponents in fmt fixes issue rust-lang#115737 , where the decimal places are rounded incorrectly when formatting scientific notation
I tried this code:
I expected this all to be fine.
Instead, the final assertion fails, and the formatting is done like this in the final case:
Not sure why 2 decimal places are returned, looks like a bug.
This bug also exists in the beta or nightly versions, running in Rust Playground.
Discussion: https://users.rust-lang.org/t/scientific-notation-decimal-places/99682
Edit: after looking around the compiler code for the first time I think the problem is somewhere in either format_exact or format_exact_opt.
The text was updated successfully, but these errors were encountered: