Repository navigation
numfmt: round values printed without a suffix - #15636
ozenyunus35 wants to merge 2 commits into
Conversation
| div_round(n, 1.0, round_method) | ||
| }; | ||
| if unscaled.abs() <= bases[1] - 1.0 { | ||
| return Ok((n, None)); |
There was a problem hiding this comment.
why not return unscaled here? then transform_to wouldn't need to round it again
There was a problem hiding this comment.
@sylvestre Thanks! consider_suffix now returns unscaled for the no-suffix case, so transform_to prints it without rounding again. Below 10 the two steps (one decimal, then ties to even) are what GNU 9.12 does too: with the default from-zero, 1.45 gives 2, 2.5 gives 2 and 0.5 gives 0, and uutils now gives the same. I also added a --format=%.1f case for all five methods to test_round_without_suffix.
| localize(format_float(i2, precision)) | ||
| } | ||
| None => localize(format_float(i2, 0)), | ||
| None => localize(format_float(div_round(i2, 1.0, round_method), 0)), |
There was a problem hiding this comment.
below 10 this rounds twice now (one decimal, then ties-even), so 1.45 gives 2 but 2.5 gives 2, is that what GNU does?
could you please add 1.45 / 2.5 / 9.95 to the test?
| } | ||
|
|
||
| #[test] | ||
| fn test_round_without_suffix() { |
There was a problem hiding this comment.
please add a --format=%.1f case too, the precision branch isn't covered
With --to=si/iec, a value printed without a suffix ignored --round, and the suffix was picked from the unrounded value, so 999.9 with --round=down became 0.9k instead of 999. Fixes uutils#15574
Also cover the --format precision branch in the test.
f9df462 to
678a252
Compare
Fixes #15574
With
--to=si,--to=iecor--to=iec-i, a value printed without a suffix did not use--round:format_float(i2, 0), which rounds half to even. So--to=si 12.1gave12instead of13, the defaultfrom-zeroresult.consider_suffixchecked the unrounded value againstbases[1] - 1.0. So--round=down 999.9got a suffix and printed0.9k, where GNU prints999.consider_suffixnow rounds the value with the method first and leaves it without a suffix if the rounded value still fits. It usesdiv_round, orround_with_precision(.., 0)when a--formatprecision is given, matching how each case is printed later, and returns that rounded value, sotransform_todoes not round it again. Values below 10 keep one decimal and are then printed with ties to even, as before. GNU does the same: with the default method,1.45gives2and2.5gives2, and the results from #12032 (1.4and0.6give1) stay the same.I added
test_round_without_suffix, which checks all five methods with--to=si,--to=iecand--format=%.1f. It fails without the change, and alltest_numfmttests and theuu_numfmtunit tests pass.I also compared 90 option combinations with GNU 9.12 in the
Clocale:--to=si/iec/iec-i, each--roundmethod, and no format,--format=%f/%.0f/%.1f/%.2fand--padding=8. The input was 36 values between-999.9and999999, including values near 10, 999 and 1023. Before the change most of them differed. After it the output is the same, except for one input:-0.6with--round=uportowards-zeroand a--formatprecision prints-0(or-0.0), where GNU prints0. That is already the case onmainand is not changed here.cargo bench -p uu_numfmtshows no change against the merge base.I used an AI assistant to help draft parts of the code and tests; I reviewed the changes and ran the tests myself.