Repository navigation
Description
Summary
Implement float f-string modes :e and bare :f according to §15.4.4. The current runtime receives the mode but discards it, so scientific notation is unavailable and fixed-point defaults are inconsistent. This should be fixed with the same formatter rewrite as #438.
Spec
§15.4.2 defines float modes: f for fixed-point, e for scientific notation, and g for general/shortest with default general formatting (docs/with-specification.md:7676-7689). §15.4.4 adds that when precision is specified without a mode, the mode defaults to f, and gives examples for .2, .2f, and .2e (docs/with-specification.md:7730-7743).
Expected behavior:
f"{3.14159:.2}" // "3.14"
f"{3.14159:.2f}" // "3.14"
f"{3.14159:.2e}" // "3.14e+00"
f"{3.0:f}" // "3.000000" (default fixed precision)
Current state
src/SemaCheck.w:3669-3672validates thatf,e, andgmodes are legal only for floats, so the parser/sema side recognizes the mode family.src/MirLower.w:2150-2163extractsspec_modefrom f-string specs and routes mode/width/precision toFMT_BUF_WRITE_FMT.src/CodegenDispatch.w:2500-2515passesmodethrough towith_fmt_f64_spec.rt/rt_core.w:1091-1099immediately discardsmodewithlet _ = mode, then chooses only between fixed formatting whenprecision >= 0and default formatting otherwise.rt/rt_core.w:137-185implements fixed precision but has no scientific notation path and no mode-specific default precision for bare:f.test/behavior/behav_fstring_float_spec.w:5-24covers precision and sign, but not explicitf, explicite, org.
Implementation plan
- Fix this with Bug: default float display prints 'inf' for most values (§15.4) #438 by replacing the runtime float conversion core in
rt/rt_core.wwith a Ryū d2s-style implementation that can emitg, fixed, and scientific forms from the same classified finite value. - Define mode constants at the runtime boundary (
0,f,e,g) or document the byte values used byParser.parse_format_spec_text. - In
with_fmt_f64_spec, route:- mode
0+ precision< 0to general/default; - mode
0+ precision>= 0to fixed; - mode
fto fixed with default precision 6 when precision is absent; - mode
eto scientific with default precision determined by the spec decision, matching the examples; - mode
gto general with optional precision.
- mode
- Preserve width, sign, fill, align, and zero padding after the mode-specific numeric string is produced.
- Add explicit NaN/inf handling to every mode.
rt_f64_to_fixed_bufcurrently handles NaN but not infinities; do not leave mode-specific gaps. - Runtime changes are seed-sensitive; keep this as a runtime-only logical slice and run the standard bootstrap gates.
Test plan
- Extend or add
test/behavior/behav_fstring_float_modes.w:f"{3.14159:.2f}" == "3.14"f"{3.14159:.2e}" == "3.14e+00"f"{3.0:f}" == "3.000000"or the chosen fixed default precisionf"{3.14:g}" == "3.14"- width/sign combinations such as
f"{3.14:+10.2f}".
- Keep existing negative tests for integer modes on floats, e.g.
test/compile_errors/err_fstring_spec_int_mode_on_float.w. - Add regression cases for NaN and +/-inf under
:f,:e, and:g.
Acceptance criteria
-
with_fmt_f64_specno longer discardsmode -
:e,:f, and:gproduce distinct, spec-compliant float output - Precision-without-mode continues to default to fixed-point
- Width/sign/padding still apply after mode formatting
- Shared tests cover the Bug: default float display prints 'inf' for most values (§15.4) #438 default path and explicit float modes
-
with build && with build :fixpoint && with build :testpass
Dependencies
Should be implemented with #438. Blocks the float portions of #503.
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 15.4.2.6, 15.4.2.7.
Activity
ehartford commentedon Jun 12, 2026
Implemented in $(git rev-parse --short HEAD). with_fmt_f64_spec now honors f/e/g mode bytes, bare :f and :e use default precision 6, precision-without-mode remains fixed-point, and emit-C direct formatting passes the runtime mode argument. Added behavior coverage for explicit fixed/scientific/general modes, bare modes, width/sign/zero padding, and NaN/+/-inf under float modes; existing compile-error tests cover invalid mode/type combinations. Verification passed: with build, with build :emit-c-smoke, with build :fixpoint, with build :test, and with build :test-green using the project seed path.
ehartford commentedon Jun 12, 2026
Correction: implemented in aac9b94.