Zig gets improved float parser/formatter codeberg.org · 81 points · 10 comments ...

@best

Zig gets improved float parser/formatter codeberg.org · 81 points · 10 comments · HN thread

TL;DR A pull request to Zig's standard library replaces the float formatting and parsing code in std.fmt with a new algorithm (called fp) based on Russ Cox's research write-up. The author reports it is about 2x faster than Ryu for formatting and 10-20% faster than Eisel-Lemire, adds full-precision output and parsing for floats larger than f64, and fixes several rounding bugs.

  • The new implementation follows the algorithms in Russ Cox's 'fp' research article and generalizes them to arbitrary float types beyond f64.
  • Benchmarks on a Ryzen 5 5600X: shortest f64 formatting takes 14.49ns versus 28.64ns for std and 16.51ns for a Zig port of zmij; parsing is 16.50ns versus 19.09ns.
  • For f128, parsing drops from about 1117 microseconds (slow big-int fallback in std) to 120ns, and formatting from 220ns to 149ns.
  • Binary size is smaller in small optimization modes (f64 engine about 10.6KiB versus 30.4KiB for std) and comparable or smaller elsewhere.
  • New features include full-precision decimal output, a case option for inf/nan, and a new big-integer representation shared between formatting and fallback parsing.
  • Several rounding bugs in the existing std tests are fixed, and the PR closes multiple linked issues; a CI failure on musl with 128-bit long double was worked around by computing a constant without the log10 builtin.

Discussion The thread is short and mostly admiring. Commenters praise the work and point readers to Russ Cox's writing and blog, which the PR is based on. A few note that floating-point conversion was neglected for decades until Grisu, and that careful engineering like this is still valued. One commenter asks whether Zig is doomed given Rust's popularity in AI projects, which draws replies that Zig's distance from AI hype is a plus and that the question is unclear.

  • Russ Cox's work is essential reading for engineers (many): Several commenters recommend reading his blog and related writing, crediting him with inspiring them to program and calling the work a breakthrough.
  • Praise for the speedup, especially for 128-bit floats (some): Commenters welcome the large gains on f128, where the old fallback was very slow, and appreciate that some API-changing improvements were deferred.
  • Careful craftsmanship still matters despite AI trends (some): One commenter says it is reassuring that serious software engineering is still done by hand, and another argues Zig's lack of AI taint is a feature.
  • Floating-point conversion was neglected until recently (a few): One commenter notes that little progress was made for about 20 years until Grisu, after which many improvements followed.
  • Zig is doomed because of Rust and AI trends (a few): One commenter asks whether Zig is doomed given Rust's popularity in AI projects; a reply says Rust may be the one at risk, and another asks what doomed means.

Links from the comments

#zig #floatingPoint #stdlib #performance #parsing