Skip to content

ieeeBinaryToShortestDecimal

ieeeBinaryToShortestDecimal(value, format): Decimal

Defined in: packages/math-numbers/src/ieee754/binary/ieee-binary-to-shortest-decimal.ts:37

The shortest decimal that reads back as exactly this binary float — the numeral Number.prototype.toString produces, generalised to any format width.

ieeeBinaryToDecimal gives the exact expansion, which for binary64’s 0.1 is 55 digits; this gives 0.1, because no shorter numeral and no other 1-digit numeral rounds back to the same value. Both denote the same float, but only this one is what a reader expects to see.

Steele and White’s free-format algorithm (the “Dragon4” family), run on exact bigint rationals: the value is bracketed by the midpoints to its two neighbours, digits are generated most-significant first, and generation stops as soon as the numeral is unambiguously inside that bracket. Ryū computes the same digits from precomputed power-of-ten tables, which is faster but needs a table per format; exact arithmetic needs none and is the right trade for a path that is not hot.

The bracket is asymmetric at a power of two, where the gap below is half the gap above, and the low end is inclusive only for an even significand — the two details that decide whether 5e-324 prints as itself or as 5.0e-324.

O(d) big-integer operations for d digits produced, each on numbers of the value’s own width.

IeeeBinaryFinite

A finite, non-zero value in the unpacked pseudo-normal form.

IeeeFormat

The format value is expressed in.

Decimal

The shortest round-tripping decimal.