A C++ reference implementation of the Dragonbox algorithm for fast, correct, and shortest float-to-string conversion.
Dragonbox is a C++ library implementing the Dragonbox algorithm, which converts binary floating-point numbers (float and double) into decimal strings. It solves the fundamental problem of `ftoa`/`dtoa` by generating the shortest, correctly rounded decimal representation that guarantees exact roundtrip parsing back to the original binary value.
C++ developers and library authors who need a fast, correct, and minimal dependency for converting floating-point numbers to strings, such as those building serialization libraries, logging systems, or data format utilities.
Developers choose Dragonbox for its proven correctness, guaranteed shortest output, and high performance, often matching or exceeding alternatives like Ryu. Its header-only core and policy-based design offer flexibility without sacrificing speed.
Reference implementation of Dragonbox in C++
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
The algorithm guarantees roundtrip accuracy, shortest decimal output, and correct rounding, with proofs provided in the accompanying paper and extensive testing against Ryu.
Benchmarks show Dragonbox competes with or outperforms contemporary algorithms like Ryu and Grisu-Exact, as detailed in the performance plots included in the README.
The core algorithm in dragonbox.h is header-only and requires only C++11, making it easy to integrate without complex build steps for basic conversion.
Offers fine-grained control over sign handling, rounding modes, trailing zeros, and cache usage, allowing optimization for specific use cases like reduced memory footprint.
The to_decimal function has undefined behavior for infinities, NaNs, and zeros, requiring external checks for robust usage, as warned in the README.
Full string generation requires extra C++ source files (dragonbox_to_chars.cpp), not just headers, complicating manual integration and increasing build complexity.
Policy combinations can cause multiple template instantiations, increasing binary size in applications using diverse settings, as noted in the policies section.