Skip to content

f-string float modes: :e scientific and bare :f (§15.4.4) #440

Closed

Description

@ehartford

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-3672 validates that f, e, and g modes are legal only for floats, so the parser/sema side recognizes the mode family.
  • src/MirLower.w:2150-2163 extracts spec_mode from f-string specs and routes mode/width/precision to FMT_BUF_WRITE_FMT.
  • src/CodegenDispatch.w:2500-2515 passes mode through to with_fmt_f64_spec.
  • rt/rt_core.w:1091-1099 immediately discards mode with let _ = mode, then chooses only between fixed formatting when precision >= 0 and default formatting otherwise.
  • rt/rt_core.w:137-185 implements 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-24 covers precision and sign, but not explicit f, explicit e, or g.

Implementation plan

  1. 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.w with a Ryū d2s-style implementation that can emit g, fixed, and scientific forms from the same classified finite value.
  2. Define mode constants at the runtime boundary (0, f, e, g) or document the byte values used by Parser.parse_format_spec_text.
  3. In with_fmt_f64_spec, route:
    • mode 0 + precision < 0 to general/default;
    • mode 0 + precision >= 0 to fixed;
    • mode f to fixed with default precision 6 when precision is absent;
    • mode e to scientific with default precision determined by the spec decision, matching the examples;
    • mode g to general with optional precision.
  4. Preserve width, sign, fill, align, and zero padding after the mode-specific numeric string is produced.
  5. Add explicit NaN/inf handling to every mode. rt_f64_to_fixed_buf currently handles NaN but not infinities; do not leave mode-specific gaps.
  6. 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 precision
    • f"{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_spec no longer discards mode
  • :e, :f, and :g produce 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 :test pass

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

  1. ehartford commented on Jun 12, 2026

    @ehartford
    ContributorAuthor

    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.

  2. ehartford commented on Jun 12, 2026

    @ehartford
    ContributorAuthor

    Correction: implemented in aac9b94.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions