Auto merge of #116301 - mj10021:issue-115737-fix, r=cuviper

fix rounding issue with exponents in fmt

fixes issue #115737 , where the decimal places are rounded incorrectly when formatting scientific notation
This commit is contained in:
bors 2023-11-14 00:04:05 +00:00
commit b9175240ea
2 changed files with 24 additions and 7 deletions

View File

@ -309,7 +309,6 @@ macro_rules! impl_Exp {
n /= 10;
exponent += 1;
}
let (added_precision, subtracted_precision) = match f.precision() {
Some(fmt_prec) => {
// number of decimal digits minus 1
@ -331,9 +330,15 @@ macro_rules! impl_Exp {
let rem = n % 10;
n /= 10;
exponent += 1;
// round up last digit
if rem >= 5 {
// round up last digit, round to even on a tie
if rem > 5 || (rem == 5 && (n % 2 != 0 || subtracted_precision > 1 )) {
n += 1;
// if the digit is rounded to the next power
// instead adjust the exponent
if n.ilog10() > (n - 1).ilog10() {
n /= 10;
exponent += 1;
}
}
}
(n, exponent, exponent, added_precision)

View File

@ -150,6 +150,18 @@ fn test_format_int_exp_precision() {
// test padding with precision (and sign)
assert_eq!(format!("{:+10.3e}", 1), " +1.000e0");
// test precision remains correct when rounding to next power
for i in i16::MIN..=i16::MAX {
for p in 0..=5 {
assert_eq!(
format!("{i:.p$e}"),
format!("{:.p$e}", f32::from(i)),
"integer {i} at precision {p}"
);
}
}
}
#[test]