Skip to content

Fix nan print, simplify negative number printing. #7413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 14, 2025

Conversation

drinkcat
Copy link
Contributor

@drinkcat drinkcat commented Mar 7, 2025

test: printf: Add a test for scientific printing of negative number

This was broken before the last few commits.

test: printf: Add nan, inf, negative zero

Add a few end-to-end tests for printf of unusual floats (nan,
infinity, negative zero).

uucore: format: print absolute value of float, then add sign

Simplifies the code, but also fixes printing of negative and positive NaN:
cargo run printf "%f %f\n" nan -nan

Fixes part 2 of #7412.

uucore: format: force NaN back to lowercase

Fixes formatting of NaN to nan.

Fixes part 1 of #7412.

Copy link

github-actions bot commented Mar 7, 2025

GNU testsuite comparison:

Skip an intermittent issue tests/timeout/timeout (fails in this run but passes in the 'main' branch)

@sylvestre sylvestre requested a review from Copilot March 7, 2025 14:56
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

PR Overview

This pull request fixes the formatting of NaN and simplifies negative number printing while also adding new tests for non‐finite and negative zero floats.

  • Updated formatting functions in uucore to correctly handle negative numbers and force lowercase NaN
  • Simplified logic for generating sign indicators and default precision handling
  • Added new tests in both the printf and seq utilities to validate these formatting changes

Reviewed Changes

File Description
src/uucore/src/lib/features/format/num_format.rs Refactored float formatting code and sign handling logic
tests/by-util/test_seq.rs Added tests for default precision formatting of floats
tests/by-util/test_printf.rs Added tests for non-finite floats and negative zero printing

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (1)

src/uucore/src/lib/features/format/num_format.rs:309

  • [nitpick] Consider renaming the 'negative' parameter to 'is_negative' for improved clarity, making it more descriptive of its boolean nature.
fn get_sign_indicator(sign: PositiveSign, negative: bool) -> String {

@drinkcat
Copy link
Contributor Author

This also accidentally fixes negative number scientific printing:

# HEAD:
cargo run printf "%e %e\n" 1234 -1234
1.234000e+03 -1234.000000e+00
# This branch:
cargo run printf "%e %e\n" 1234 -1234
1.234000e+03 -1.234000e+03

I'll add a test for that, so it doesn't regress.

Copy link

GNU testsuite comparison:

Skip an intermittent issue tests/timeout/timeout (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/misc/stdbuf (passes in this run but fails in the 'main' branch)

Copy link

GNU testsuite comparison:

Skip an intermittent issue tests/misc/stdbuf (fails in this run but passes in the 'main' branch)

Fixes formatting of `NaN` to `nan`.

Fixes part 1 of uutils#7412.
Simplifies the code, but also fixes printing of negative and positive `NaN`:
`cargo run printf "%f %f\n" nan -nan`

Fixes part 2 of uutils#7412.
Add a few end-to-end tests for printf of unusual floats (nan,
infinity, negative zero).
This was broken before the last few commits.
@drinkcat
Copy link
Contributor Author

Rebased. Also, while doing further work I realized that I can now remove the sign handling logic from format_float_hexadecimal (since that only prints non-negative numbers now), so I squashed that in.

Copy link

GNU testsuite comparison:

Skip an intermittent issue tests/timeout/timeout (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/misc/stdbuf (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/misc/usage_vs_getopt (passes in this run but fails in the 'main' branch)

@RenjiSann RenjiSann merged commit e147063 into uutils:main Mar 14, 2025
66 checks passed
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.

3 participants