3 ms·
> Often the problem is framed as "converting a floating point number to the shortest possible decimal string representation", but this framing is neither necces
by lifthrasiir 9y ago
> Often the problem is framed as "converting a floating point number to the shortest possible decimal string representation", but this framing is neither neccessary nor sufficient for implementing the %e / %f / %g formats of sprintf.
This sentence is crucial for anyone wants to implement the float-to-decimal conversion. There exists a Grisu-inspired analogue for this problem but to my current knowledge it is not well described in any known literature. I had to fully analyze the analogue and recreate my (slightly improved) version of the algorithm for Rust [1]. It is not too complex once you understood but annoying enough to derive again.
[1] https://github.com/rust-lang/rust/blob/f8d485f/src/libcore/num/flt2dec/strategy/grisu.rs#L439-L683 https://github.com/rust-lang/rust/blob/f8d485f/src/libcore/n...