Skip to content

printf, seq: round %e and %g ties to even - #16345

Open
Socialpranker wants to merge 2 commits into
uutils:mainfrom
Socialpranker:printf-round-ties-even
Open

Socialpranker wants to merge 2 commits into
uutils:mainfrom
Socialpranker:printf-round-ties-even

Conversation

@Socialpranker

Copy link
Copy Markdown
Contributor

%e, %E, %g and %G rounded a value that sits exactly halfway between two outputs away from zero, because bd_to_string_exp_with_prec used BigDecimal::with_prec, which rounds halves up. GNU, like C printf, rounds such ties to even, and our %f already does too, so %.0f and %.0e of 2.5 gave 2 and 3e+00.

This switches to with_precision_round with RoundingMode::HalfEven. Now printf '%.1e %.0e %.1g %.2e' 0.125 2.5 0.25 1.125 prints 1.2e-01 2e+00 0.2 1.12e+00, the same as GNU, and seq -f %.0e 2.5 2.5 prints 2e+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.45 gives 0.4 here and 0.5 in GNU).

Closes #15662

// (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");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the TODO still mentions with_prec

@Socialpranker

Copy link
Copy Markdown
Contributor Author

done, both fixed

@codspeed

codspeed Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by 14.9%

1 warning

⚠️ 70 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚡ 2 improved benchmarks
✅ 122 untouched benchmarks
⏩ 365 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ socket_addrs_direct[100000] 132.1 µs 105.2 µs +25.51%
⚡ hostname_ip_lookup[100000] 573.3 µs 545.1 µs +5.18%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing Socialpranker:printf-round-ties-even (c79901c) with main (2a072db)

Open in CodSpeed

Footnotes

  1. 365 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@sylvestre
sylvestre force-pushed the printf-round-ties-even branch from 43da309 to 9216c3c Compare November 16, 2026 19:19
@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

Congrats! The gnu test tests/env/env is no longer failing!
Congrats! The gnu test tests/misc/usage_vs_getopt is no longer failing!

@Socialpranker

Copy link
Copy Markdown
Contributor Author

Rebased onto main to rerun CI. The red jobs on the previous head were outside this change: Tests failed in test_install::test_install_strip_program_sees_caller_umask and test_split::test_number_filter_exiting_early_keeps_other_chunks, and MSYS could not find stdbuf.dll. #14187, which only touches wc, failed on exactly the same tests and step.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

printf: %e and %g round an exact halfway value up instead of to even (%.0e of 2.5 gives 3e+00)

2 participants