Repository navigation
Description
printf with %f (and %e/%g family precision) rounds exact-half decimal inputs down where GNU rounds them up.
Minimal reproduction:
printf '%.3f\n' 1.2345
printf '%.3f\n' -1.234567
printf '%.2f\n' 1.245GNU bash 5.3.20 expected:
1.235
-1.235
1.25
rubash (master 70e962b9c2e4, verdict build) actual:
1.234
-1.234
1.24
GNU's outputs match correctly-rounded long-double arithmetic on every probe (e.g. %.2f 2.675 -> 2.67 and %.1f 0.15 -> 0.2 both agree). Rubash's outputs match neither correctly-rounded f64 arithmetic (1.245 and 1.234567 would round up) nor decimal half-even (2.675 would round to 2.68) — the rounding path needs a look, not a constant tweak.
Controls that agree (both shells): %.2f 1.235 -> 1.24, %.2f 2.675 -> 2.67, %.1f 0.15 -> 0.2, %.0f 2.5 -> 2, %.4f 1.23455 -> 1.2346, %.3f 1.2344 -> 1.234, %.2f 1.234567 -> 1.23, %.5f 1.234567 -> 1.23457, %f 1.234567 -> 1.234567, %.2e 1234.567 -> 1.23e+03, %.3g 1234.567 -> 1.23e+03, %.3f 0.0005 -> 0.000, %.3f 999.9995 -> 999.999.
Evidence: hunt rounds r128 (prescreen) + r128 verdict on fresh master build 70e962b9c2e4 (60 cases x2, pr116 DIFF both passes); morph probes n1–n18 bound it to the %f rounding path (14 controls agree). Regression seed re-verified on the verdict binary x2: GNU 9/9, rubash 5/9.
Regression seed:
#!/usr/bin/env bash
# Regression seed R128-B: printf %f-family rounding differs from GNU on
# exact-half decimal inputs. GNU rounds per the exact (long-double) value,
# i.e. 1.2345 -> 1.235, 1.245 -> 1.25, -1.234567 -> -1.235; rubash rounds
# those down (1.234 / 1.24 / -1.234).
# Usage: seed_r128b.sh <shell> (shell = path to bash/rubash binary)
S="${1:?usage: seed_r128b.sh <shell>}"
fail=0
check() { if [ "$2" != "$3" ]; then echo "FAIL $1: expected [$2] got [$3]"; fail=1; else echo "PASS $1"; fi; }
got=$("$S" -c 'printf "%.3f\n" 1.2345')
check "f3-1.2345" "1.235" "$got"
got=$("$S" -c 'printf "%.3f\n" -1.234567')
check "f3-neg-1.234567" "-1.235" "$got"
got=$("$S" -c 'printf "%.2f\n" 1.245')
check "f2-1.245" "1.25" "$got"
got=$("$S" -c 'printf "[%10.3f]\n" -1.234567')
check "f3-width-neg" "[ -1.235]" "$got"
# --- controls (pass on both shells) ---
got=$("$S" -c 'printf "%.2f\n" 1.235')
check "f2-1.235" "1.24" "$got"
got=$("$S" -c 'printf "%.2f\n" 2.675')
check "f2-2.675" "2.67" "$got"
got=$("$S" -c 'printf "%.0f\n" 2.5')
check "f0-2.5" "2" "$got"
got=$("$S" -c 'printf "%.4f\n" 1.23455')
check "f4-1.23455" "1.2346" "$got"
got=$("$S" -c 'printf "%.3f\n" 1.2344')
check "f3-1.2344" "1.234" "$got"
exit $failMaster SHA: 70e962b9c2e4212a4fc663db6acc16984f27b2d4
Activity
caomengxuan666 commentedon Nov 16, 2026
队长车道临时接手(自动续单派工):已开工修复本单,工作区
~/workspace/fix-3786/,分支fix/3786-printf-half-rounding,基底 masterc3bf91e8。先按 GNU 源码定语义、定位舍入路径的机制根因,再带回归钉修法。caomengxuan666 commentedon Nov 16, 2026
接手:前任车道被执行超时中断(只定了根因、未起钉、无提交、无 PR、无 REPORT),本车道从头做完。工作区
~/workspace/fix-3786/,分支fix/3786-printf-half-rounding,基底 c3bf91e。fix(printf): round %f above exact-half decimals up via sticky tail (R…
caomengxuan666 commentedon Nov 16, 2026
已由 #3807 合入(队长复核,merge 20fb781)。printf %f 精确半值舍入修法:format_fixed 截断前算 sticky 位。census 抖动格已裁(见 #3807 合入正文)。