On this page

0178: Shortest round-trip f64 text, identical on both backends

Status: Proposed (pending maintainer)

Context

printf("%g") loses precision and differs between C libraries; Rust's Display never uses an exponent, so 1e300 prints 301 digits. Output must be deterministic and byte-identical between harness interp and compiled C.

Decision

Printing, println and interpolation use one format:

The interpreter takes Rust's shortest digits ({:e}); the C runtime has its own exact bignum Dragon4 in runtime/hrt_f64.h and does not call printf. A seeded runtime test compares the two on random bit patterns.

s.parse_f64() -> option<f64> accepts exactly [+-]? digits ('.' digits)? ([eE] [+-]? digits)? and [+-]?inf, nan (ASCII, no spaces), so every printed value parses back. The result is correctly rounded; overflow gives ±inf, not none. The interpreter uses Rust's parser; C validates the grammar and calls strtod (correctly rounded on glibc, musl and macOS; the runtime never changes the "C" locale).

Consequences

Golden outputs for floats are portable. Printing costs a bignum loop in C (≤ 1,280-bit integers); it is not on a hot path today and can move to Ryu behind the same output if a benchmark asks for it.

Alternatives