Urgent.News

What's breaking now, across thousands of outlets.

Tech

The fastest double-to-string algorithm you’ve never heard of

The yy algorithm, used in the binary-to-decimal conversion library Żmij, is a fast dtoa implementation not widely known. It operates on fixed-width integer arithmetic and uses only one multiplication by a precomputed power of 10, unlike classic Schubfach which requires two or three. For each binary float v = c · 2^e2, yy determines a decimal exponent e10 using a fixed-point approximation of log10 2.

The binary float v is then re-expressed at the decimal scale using a precomputed power-of-10 table p10, such that p10 · 2^ep ≈ 10^(-e10). This results in a value called bar v, which lies between four candidate decimal values. The algorithm prefers the shorter decimal representation if it rounds-trips, otherwise it falls back to the longer one.

The comparison involves three predicates evaluated against bar v and a half-ulp band δ around it. These predicates determine whether bar v reaches d0, u0, or requires a fallback to d1 or u1. The yy algorithm's key optimization is the one-multiplication claim, achieved by scaling the half-ulp of v by p10, which is a simple integer shift.

This allows the algorithm to avoid an additional multiplication required by Schubfach. The algorithm also handles irregular intervals, subnormals, and the digit-emission loop for a complete implementation.

Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at vitaut.net →

More in Tech

More from Tuesday 11 August →