Skip to content

Bug: default float display prints 'inf' for most values (§15.4) #438

Closed

Description

@ehartford

Summary

Fix default float display so ordinary values such as 3.14 and 10.0 render as numbers, not inf. This is a shipped user-visible correctness bug in the sole formatting mechanism of the language. The right fix is to replace the ad hoc float formatter with a correct deterministic conversion routine, not to patch individual thresholds.

Spec

§15.4 makes f-strings the only formatting mechanism and says f-strings return owned str (docs/with-specification.md:7611-7624). §15.4.2 defines float modes: f fixed-point, e scientific, and g general/shortest with g as the default (docs/with-specification.md:7676-7689). §15.4.4 requires default no-spec float formatting to use general format and gives f"{3.14}" == "3.14" (docs/with-specification.md:7730-7743). §15.4.7 says debug mode for floats matches default display (docs/with-specification.md:7766-7774).

Current state

  • Original issue verification: installed and stage2 binaries print f"{3.14}" -> inf and f"{10.0}" -> inf; 1.5, 0.5, and 0.001 happened to render correctly.
  • src/CodegenDispatch.w:2238-2241 routes default float formatting to the runtime with_fmt_f64.
  • rt/rt_core.w:64-134 implements rt_f64_to_buf, a hand-written "shortest" formatter. Its infinity detection uses if val > 1.7e308 / < -1.7e308 (rt/rt_core.w:72-84) rather than bit-level IEEE checks, then casts the integer part through u64 (rt/rt_core.w:97) and formats only six fractional digits (rt/rt_core.w:106-134).
  • rt/rt_core.w:137-185 implements fixed precision separately and lacks negative/positive infinity handling.
  • rt/rt_core.w:1091-1130 implements with_fmt_f64_spec; f-string float modes: :e scientific and bare :f (§15.4.4) #440 tracks that it discards the mode and shares the same formatter substrate.
  • test/behavior/behav_fstring_float_spec.w:5-24 covers precision specs but not default no-spec float display. No test pins 3.14, 10.0, extremes, NaN/inf, or debug-float behavior.

Implementation plan

  1. Replace the hand-rolled rt_f64_to_buf and rt_f64_to_fixed_buf logic with a deterministic Ryū d2s-style implementation in rt/rt_core.w. Use the shortest-roundtrip g path for default display and debug-float display.
  2. Implement bit-level classification helpers for NaN, positive/negative infinity, positive/negative zero, and finite values. Do not use numeric threshold comparisons for infinity.
  3. Make with_fmt_f64 and with_fmt_f64_spec share the same conversion core so Bug: default float display prints 'inf' for most values (§15.4) #438 and f-string float modes: :e scientific and bare :f (§15.4.4) #440 cannot diverge again.
  4. Preserve existing width, sign, and padding behavior around the produced numeric string, but apply it after correct float conversion.
  5. Add explicit handling for -0.0. Pick and document the spec-compatible spelling (-0 or -0.0) before tests; preserve the sign if the conversion routine exposes it.
  6. Runtime changes are seed-sensitive. Do this as a runtime-only logical change and run with build, with build :fixpoint, and with build :test before any seed/install target.

Test plan

  • Add test/behavior/behav_fstring_float_default.w:
    • f"{3.14}" == "3.14"
    • f"{10.0}" is numeric ("10" or the chosen general-format spelling), never "inf"
    • f"{1.5}" == "1.5", f"{0.5}" == "0.5", f"{0.001}" == "0.001"
    • f"{1e308}" and f"{1e-308}" do not misclassify as infinity or zero
    • f"{0.0}" and f"{-0.0}" follow the chosen signed-zero rule
  • Add NaN and +/-inf coverage using constants or operations the language can express reliably.
  • Add debug-float coverage: f"{3.14:?}" matches default float output per §15.4.7.
  • Add round-trip property-style coverage if the test harness can generate a fixed corpus of bit patterns: parse(format(x)) should recover x for finite values where parsing exists.

Acceptance criteria

  • Default no-spec float formatting never prints inf for finite values
  • Default float formatting uses general/shortest semantics consistent with §15.4.4
  • Debug float formatting matches default float display
  • NaN, +/-inf, +/-0, small, large, and ordinary decimal values are covered
  • The implementation is deterministic and libc-independent
  • with build && with build :fixpoint && with build :test pass

Dependencies

Overlaps #440; both should be fixed by the same formatter rewrite. Blocks #503 coverage from becoming meaningful for float modes.

Traceability

From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 15.4.2.8, 15.4.4.1, 15.4.7.4.

Activity

  1. ehartford commented on Jun 12, 2026

    @ehartford
    ContributorAuthor

    Implemented in f589534. Runtime f64 formatting now classifies NaN/inf/signed zero from IEEE-754 bits instead of numeric thresholds, formats finite default-display values through the deterministic decimal path, and extends runtime float parsing to exponent literals so large/small finite values are tested correctly. Added behavior coverage for ordinary decimals, debug-display parity, 1e308, 1e-308, signed zero, and NaN/+/-inf. Verification passed: with build, with build :fixpoint, with build :test, and with build :test-green using the project seed path.

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