Skip to content

fix(numfmt):fix precision loss for large numbers in #11654#11716

Merged
cakebaker merged 9 commits into
uutils:mainfrom
THEMIScale:main
Apr 10, 2026
Merged

fix(numfmt):fix precision loss for large numbers in #11654#11716
cakebaker merged 9 commits into
uutils:mainfrom
THEMIScale:main

Conversation

@THEMIScale
Copy link
Copy Markdown
Contributor

Fixes #11654

  • Add numeric.rs to preserve precision for large integers
  • Modify related functions in format.rs
  • Adjust some float related test cases in format.rs
  • Large integer values are now accurate
  • Float precision remains limited by f64 and is worse than GNU
  • cargo test --features unix passes for numfmt

@THEMIScale
Copy link
Copy Markdown
Contributor Author

There's a remaining problem here:

┌──(kali㉿KaliOnHP)-[~/coreutils]
└─$ ./target/debug/numfmt --from=iec 915339622.7555392131
915339622.7555391788

┌──(kali㉿KaliOnHP)-[~/coreutils]
└─$ /usr/bin/numfmt --from=iec 915339622.7555392131
915339622.7555392131

The f64 is not as precise as long double.
Even to igore the case, GNU numfmt still has an error when number is out of limitation(which WE don't):

┌──(kali㉿KaliOnHP)-[~/coreutils]
└─$ ./target/debug/numfmt --from=iec 915339622.7555392723748798127937497128934
915339622.7555392980575561523437500000000

┌──(kali㉿KaliOnHP)-[~/coreutils]
└─$ /usr/bin/numfmt --from=iec 915339622.7555392723748798127937497128934
numfmt: 值或精度过大,无法输出:"9.1534e+08/31"(请考虑使用 --to)

If anyone see this, pls NEW an ISSUE if it is reasonable, I'm worrying that this PR won't be merged, thus my environment is not relyable for now.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 8, 2026

GNU testsuite comparison:

Skip an intermittent issue tests/cut/bounded-memory (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/pr/bounded-memory (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/date/date-locale-hour (passes in this run but fails in the 'main' branch)
Note: The gnu test tests/cp/link-heap is now being skipped but was previously passing.
Congrats! The gnu test tests/expand/bounded-memory is now passing!

@cakebaker cakebaker changed the title fix(numfmt):fix recision loss for large numbers in #11654 fix(numfmt):fix precision loss for large numbers in #11654 Apr 9, 2026
@cakebaker
Copy link
Copy Markdown
Contributor

I don't know if you have seen it: a few numfmt tests fail in the CI.

@THEMIScale
Copy link
Copy Markdown
Contributor Author

Yeah, I just woke up and correct it.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 9, 2026

GNU testsuite comparison:

GNU test failed: tests/tail/tail-n0f. tests/tail/tail-n0f is passing on 'main'. Maybe you have to rebase?
Skip an intermittent issue tests/tail/symlink (fails in this run but passes in the 'main' branch)
Congrats! The gnu test tests/cut/cut-huge-range is now passing!

@THEMIScale
Copy link
Copy Markdown
Contributor Author

will the fails be a problem? How can I run these failed checks again, I guess it might not be my problem. 🤔 @cakebaker

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 9, 2026

GNU testsuite comparison:

Skip an intermittent issue tests/cut/bounded-memory (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/tty/tty-eof (fails in this run but passes in the 'main' branch)
Congrats! The gnu test tests/cp/link-heap is now passing!
Congrats! The gnu test tests/dd/no-allocate is now passing!
Congrats! The gnu test tests/seq/seq-epipe is now passing!

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 9, 2026

Merging this PR will not alter performance

✅ 309 untouched benchmarks
⏩ 46 skipped benchmarks1


Comparing THEMIScale:main (c7c803e) with main (761add4)

Open in CodSpeed

Footnotes

  1. 46 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.

@cakebaker
Copy link
Copy Markdown
Contributor

will the fails be a problem?

No, you can ignore them, they are unrelated.

Comment thread src/uu/numfmt/src/numeric.rs
Comment thread src/uu/numfmt/src/numeric.rs Outdated
@@ -0,0 +1,26 @@
// This file is written to solve #11654
// This file is related to all code changes for GitHub Issue #11654.
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.

This sentence is more or less the same as the previous sentence, so my suggestion is to remove it.

Suggested change
// This file is related to all code changes for GitHub Issue #11654.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 9, 2026

GNU testsuite comparison:

Skip an intermittent issue tests/date/date-locale-hour (fails in this run but passes in the 'main' branch)
Congrats! The gnu test tests/cp/link-heap is now passing!
Congrats! The gnu test tests/dd/no-allocate is now passing!
Congrats! The gnu test tests/seq/seq-epipe is now passing!

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 9, 2026

GNU testsuite comparison:

Skip an intermittent issue tests/date/date-locale-hour (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/tty/tty-eof (fails in this run but passes in the 'main' branch)
Congrats! The gnu test tests/cp/link-heap is now passing!
Congrats! The gnu test tests/dd/no-allocate is now passing!
Congrats! The gnu test tests/seq/seq-epipe is now passing!

@@ -479,14 +510,43 @@ fn consider_suffix(
Ok((v, Some((suffixes[i - 1], with_i))))
}
}
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 add an empty line as a separator between the two functions.

Suggested change
}
}

Comment thread src/uu/numfmt/src/format.rs Outdated
}
}

fn try_format_exact_int_without_output_scaling(
Copy link
Copy Markdown
Contributor

@cakebaker cakebaker Apr 9, 2026

Choose a reason for hiding this comment

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

It looks like there is a mismatch between the function name and what the function does: the function name contains "without_output_scaling" whereas the function does some scaling. Maybe you can simply drop "without_output_scaling" from the name?

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.

The function can be named more precisly afer try_format_exact_int_without_suffix_scaling, is it Ok?

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.

Yes, it's an improvement.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 9, 2026

GNU testsuite comparison:

GNU test failed: tests/timeout/timeout-group. tests/timeout/timeout-group is passing on 'main'. Maybe you have to rebase?
Skip an intermittent issue tests/tty/tty-eof (fails in this run but passes in the 'main' branch)
Congrats! The gnu test tests/cp/link-heap is now passing!
Congrats! The gnu test tests/dd/no-allocate is now passing!
Congrats! The gnu test tests/seq/seq-epipe is now passing!

Comment thread src/uu/numfmt/src/format.rs Outdated
fn try_scale_exact_int_without_suffix(
value: ParsedNumber,
from_unit: usize,
had_no_suffix: bool,
Copy link
Copy Markdown
Contributor

@cakebaker cakebaker Apr 9, 2026

Choose a reason for hiding this comment

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

I would use a name without negation (and switch its meaning) to avoid the double negation on line 378.

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.

Yes, that is a problem. I'm thinking to verify had_no_suffix in transform_from() , and change name of the function try_scale_exact_int_without_suffix into try_scale_exact_int_with_from_unit.

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.

Yes, that might be an option.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 9, 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/pr/bounded-memory (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/tail/inotify-dir-recreate (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/tty/tty-eof (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/date/date-locale-hour (passes in this run but fails in the 'main' branch)
Note: The gnu test tests/dd/no-allocate is now being skipped but was previously passing.
Note: The gnu test tests/printf/printf-surprise is now being skipped but was previously passing.
Note: The gnu test tests/tail/pipe-f is now being skipped but was previously passing.

@THEMIScale
Copy link
Copy Markdown
Contributor Author

can my code be merged now?

@github-actions
Copy link
Copy Markdown

GNU testsuite comparison:

GNU test failed: tests/cp/sparse-to-pipe. tests/cp/sparse-to-pipe is passing on 'main'. Maybe you have to rebase?
GNU test failed: tests/tail/pid-pipe. tests/tail/pid-pipe is passing on 'main'. Maybe you have to rebase?
Skip an intermittent issue tests/tty/tty-eof (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/date/date-locale-hour (passes in this run but fails in the 'main' branch)
Note: The gnu test tests/dd/no-allocate is now being skipped but was previously passing.
Note: The gnu test tests/rm/many-dir-entries-vs-OOM is now being skipped but was previously passing.
Skip an intermittent issue tests/tail/tail-n0f (was skipped on 'main', now failing)

@github-actions
Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/tail/symlink (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/date/date-locale-hour (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/pr/bounded-memory (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/tty/tty-eof (passes in this run but fails in the 'main' branch)
Note: The gnu test tests/rm/many-dir-entries-vs-OOM is now being skipped but was previously passing.

@cakebaker cakebaker merged commit ceacc67 into uutils:main Apr 10, 2026
169 checks passed
@cakebaker
Copy link
Copy Markdown
Contributor

Thanks!

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: large integers lose precision (f64 rounding) vs GNU

2 participants