Skip to content

interpreter: print integral numbers as integers - #148

Merged
Alonely0 merged 1 commit into
uutils:mainfrom
jayhemnani9910:fix/integral-float-output
Oct 27, 2026
Merged

Alonely0 merged 1 commit into
uutils:mainfrom
jayhemnani9910:fix/integral-float-output

Conversation

@jayhemnani9910

Copy link
Copy Markdown
Contributor

Integral values that are stored as floats print in exponent form once they reach 1e16, and -0 keeps its sign:

$ awk 'BEGIN { print 2^64, 1e16, -0 }'
1.8446744073709552e+19 1e+16 -0
$ gawk 'BEGIN { print 2^64, 1e16, -0 }'    # gawk 5.3.2
18446744073709551616 10000000000000000 0

POSIX says a number that is an integer converts to a string as if by %d; for everything else OFMT/CONVFMT apply. write_string sent every float through zmij, which picks the shortest round-trip form. A finite float with no fractional part is now written as an integer with all its digits, and -0 as 0. mawk gives the same output up to 2^64 too. Non-integral numbers are unchanged: they still don't go through OFMT/CONVFMT (0.1+0.2 prints 0.30000000000000004, gawk 0.3), because that needs the printf engine.

Same output in concatenation (1e16 "") and for field values (5e20 × 1 → 500000000000000000000).

Tests: integral_numbers_print_as_integers fails without the change. cargo test --workspace: test_awk 98 passed (97 on main), the rest unchanged; clippy clean. A 64-program comparison with gawk changes only on the line that prints these numbers.

Copilot AI balanced review requested due to automatic review settings October 26, 2026 11:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

A float went through the shortest round-trip formatter, so integral
values from 1e16 up came out in exponent form (print 2^64 gave
1.8446744073709552e+19) and -0 printed as -0. POSIX converts an integer
value as if by %d, and gawk prints 18446744073709551616 and 0. Write
finite integral values with all their digits.
@jayhemnani9910
jayhemnani9910 force-pushed the fix/integral-float-output branch from 093b783 to 3af41a1 Compare October 26, 2026 15:10
Copilot AI balanced review requested due to automatic review settings October 26, 2026 15:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@jayhemnani9910

Copy link
Copy Markdown
Contributor Author

Rebased on main: only the end of test_awk.rs conflicted, both tests kept. cargo test --workspace passes and clippy is clean.

@Alonely0 Alonely0 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice fix! I'm going to point out two nitpicks that aren't necessary to solve.

  1. There is an intermediate allocation with format!(), and the macro bloats the emitted code compared to .to_string(). We really should just avoid it by using io::Write or fmt::Write, but one of the vector libraries we use doesn't support that :/. We can fix this eventually, it's very low-priority.
  2. We would be better off adding +0.0 to the float instead of branching to avoid negative zeros, but some platforms don't have conformant IEEE 754 numbers (looks harshly at RISC-V), so it can be kept as-is at the moment.

@Alonely0
Alonely0 merged commit c73ad66 into uutils:main Oct 27, 2026
15 checks passed
@Alonely0

Copy link
Copy Markdown
Collaborator

Thanks for the patch!

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