pub fn write_dragonbox(buf: &mut [u8; 32], d: f64) -> &[u8] ⓘExpand description
Writes a double the way the time series module writes a sample value on RESP2, which is neither of the two printers above.
The module hands the value to a vendored Dragonbox and replies with the
characters that come back, so a sample reads as 1.5 but a tenth reads as
1E-1 and ten million reads as 10000000. Deciding which of the two forms
a value takes is the whole problem, because Dragonbox makes that decision
on a decimal it has not finished shortening, and the caller never sees that
intermediate. Its printer takes the plain form when the exponent of that
unshortened decimal is in -16 ..= 0 and the value has an integer part,
and the exponent form otherwise.
The unshortened exponent can be recovered from the shortest one. Dragonbox
returns one of two adjacent exponents, floor(e2 * log10(2)) and one above
it, where e2 is the binary exponent of the significand read as an
integer, so the exponent it used is the shortest value’s own exponent
capped at the upper of that pair. A value whose significand bits are all
zero sits on a shorter rounding interval and its pair starts at
floor(e2 * log10(2) - log10(4 / 3)) instead. Both logarithms are the
usual integer approximations, exact for every binary exponent a double has.
Verified against the module’s own Dragonbox over seven and a half million doubles, random bit patterns, every awkward decade and boundary, and every one of the 2045 powers of two, with no difference.