Repository navigation
interpreter: print integral numbers as integers - #148
Conversation
There was a problem hiding this comment.
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.
093b783 to
3af41a1
Compare
There was a problem hiding this comment.
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.
|
Rebased on main: only the end of |
Alonely0
left a comment
There was a problem hiding this comment.
Nice fix! I'm going to point out two nitpicks that aren't necessary to solve.
- 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 usingio::Writeorfmt::Write, but one of the vector libraries we use doesn't support that :/. We can fix this eventually, it's very low-priority. - We would be better off adding
+0.0to 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.
|
Thanks for the patch! |
Integral values that are stored as floats print in exponent form once they reach 1e16, and
-0keeps its sign:POSIX says a number that is an integer converts to a string as if by
%d; for everything else OFMT/CONVFMT apply.write_stringsent every float throughzmij, 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-0as0. 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.2prints0.30000000000000004, gawk0.3), because that needs the printf engine.Same output in concatenation (
1e16 "") and for field values (5e20× 1 →500000000000000000000).Tests:
integral_numbers_print_as_integersfails 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.