Skip to content

numfmt: reject %f values too large to format exactly#12012

Open
pocopepe wants to merge 1 commit intouutils:mainfrom
pocopepe:11936
Open

numfmt: reject %f values too large to format exactly#12012
pocopepe wants to merge 1 commit intouutils:mainfrom
pocopepe:11936

Conversation

@pocopepe
Copy link
Copy Markdown
Contributor

Fixes #11936

%N.Mf was silently lossy for very large ints via the f64 path. Now bails out when the value would need 20+ digits to print, like GNU does.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 26, 2026

GNU testsuite comparison:

GNU test failed: tests/misc/io-errors. tests/misc/io-errors is passing on 'main'. Maybe you have to rebase?
Skip an intermittent issue tests/date/date-locale-hour (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/tail/follow-name (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/tail/tail-n0f (passes in this run but fails in the 'main' branch)
Congrats! The gnu test tests/tail/retry is no longer failing!
Note: The gnu test tests/rm/many-dir-entries-vs-OOM is now being skipped but was previously passing.

Comment thread src/uu/numfmt/src/format.rs Outdated
fn format_gnu_scientific(v: f64) -> String {
// 6 significant figures with trimmed trailing zeros and signed exponent
let s = format!("{v:.5e}");
if let Some(e_pos) = s.find('e') {
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.

A detail: I would use an early return with let-else to get rid of the outer else block. Something like:

let Some(e_pos) = s.find('e') else {
    return s;
};

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

switched :)

let scaled = integer / to_unit;

Some(if precision == 0 {
// reject when formatted output would need 20+ digits
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.

can you please move this check into a new function?

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.

numfmt: --format=%N.Mf silently prints lossy output for very large integers (GNU rejects)

3 participants