The minimal decimal representation of a floating point number is as short as possible and when converted back to a binary floating point number it must be identical to the original number (round-trip property).

Such an unambiguous decimal representation is necessary when converting floats from binary to text and back. For example when using CSV as intermediate format or when using SQL to write a binary float to a database.

With sprintf("%f") or similar C++ routines the output is neither distinct for every floating point number nor minimal. Several algorithms were developed over the years:

History

Dragon4 (1990)

Paper: How to print floating-point numbers accurately by Guy Steele and Jon White.

Source code can be found in the grisu3 or Errol3 repository.

First algorithm for a minimal decimal representation. Slow due the use of very large integer arithmetics (bignums).

grisu3 (2010)

Paper: Printing Floating-Point Numbers Quickly and Accurately with Integers by Florian Loitsch.

Reference implementation: https://github.com/google/double-conversion

Much faster than Dragon4 due to the use of small fixed length integers. However, for 0.5% of the numbers the result is either not minimal (grisu2) or the conversion is rejected and Dragon4 must be used (grisu3).

Errol3 (2016)

Paper: Printing Floating-Point Numbers - An Always Correct Method by Marc Andrysco, Ranjit Jhala and Sorin Lerner.

Reference implementation: https://github.com/marcandrysco/Errol

Uses Knuth’s double-double floating point arithmetics instead of fixed length integers. In the first version of the paper the evaluation was incorrect and indicated that Errol3 is faster than grisu3. In the corrected version it is slower, but always finds the minimal representation. In the github repo Errol4 can be found which is even faster than grisu3, but Errol4 is not mentioned in the paper.

Ryu (2018)

Paper: Ryū: fast float-to-string conversion by Ulf Adams.

Reference implementation: https://github.com/ulfjack/ryu

Faster than the previous algorithms, but requires much larger look-up tables.

Schubfach (March 2020)

Paper: The Schubfach way to render doubles by Raffaello Giulietti.

Java implementation by the author: https://github.com/c4f7fcce9cb06515/Schubfach/blob/master/todec/src/math/DoubleToDecimal.java

C++ Implementation by Drachennest: https://github.com/abolz/Drachennest/blob/master/src/schubfach_64.cc

Some similarities with Ryu, but faster. Trailing zeros must be truncated.

Dragonbox (September 2020)

Dragonbox: A New Floating-Point Binary-to-Decimal Conversion Algorithm by Junekey Jeon.

Reference implementation: https://github.com/jk-jeon/dragonbox

Brief implementation by Drachennest: https://github.com/abolz/Drachennest/blob/master/src/dragonbox.cc

Based on Schubfach, some optimizations from grisu are used to reduce the number of expensive 128-bit x 64-bit multiplications. As of 2023 the fastest algorithm. Both paper and implementation are extensive but hard to follow.

Comparison

https://github.com/abolz/Drachennest