Repository navigation
printf, seq: round %e and %g ties to even - #16345
Socialpranker wants to merge 2 commits into
Conversation
| // (e.g. 9995 to 3 digits => 1000 * 10^1, but we want 100 * 10^2), we compensate | ||
| // for that later. | ||
| let bd_round = bd.with_prec(precision as u64); | ||
| let precision_nz = NonZeroU64::new(precision as u64).expect("precision is at least 1"); |
There was a problem hiding this comment.
could we avoid the expect? unwrap_or(NonZeroU64::MIN) would do
| @@ -488,11 +489,13 @@ fn bd_to_string_exp_with_prec(bd: &BigDecimal, precision: usize) -> (String, i64 | |||
| // division followed by arithmetics on `digits` to round if necessary (using | |||
There was a problem hiding this comment.
the TODO still mentions with_prec
|
done, both fixed |
Merging this PR will improve performance by 14.9%1 warning
Performance Changes
Tip Curious why performance improved? Comment Comparing Footnotes
|
43da309 to
9216c3c
Compare
|
GNU testsuite comparison: |
9216c3c to
c79901c
Compare
|
Rebased onto main to rerun CI. The red jobs on the previous head were outside this change: |
%e,%E,%gand%Grounded a value that sits exactly halfway between two outputs away from zero, becausebd_to_string_exp_with_precusedBigDecimal::with_prec, which rounds halves up. GNU, like C printf, rounds such ties to even, and our%falready does too, so%.0fand%.0eof 2.5 gave2and3e+00.This switches to
with_precision_roundwithRoundingMode::HalfEven. Nowprintf '%.1e %.0e %.1g %.2e' 0.125 2.5 0.25 1.125prints1.2e-01 2e+00 0.2 1.12e+00, the same as GNU, andseq -f %.0e 2.5 2.5prints2e+00. Non-tie values round as before.One thing this does not change: we round the decimal digits of the argument, while GNU rounds its binary
long double. So for a decimal tie that is not exact in binary, such as 0.45, the result can still differ from GNU, the same way it already does for%f(%.1f 0.45gives0.4here and0.5in GNU).Closes #15662