A double-to-string conversion algorithm based on Schubfach and yy with implementations in C and C++
- Round trip guarantee
- Shortest decimal representation
- Correct rounding
- High performance
- Fast compile time
- Negative zero dependencies
- Small, clean codebase consisting of one source file and one header
- Permissive license
#include "zmij.h"
#include <stdio.h>
int main() {
char buf[zmij::buffer_size];
zmij::dtoa(6.62607015e-34, buf);
puts(buf);
}More than 3x faster than Ryu used by multiple C++ standard library implementations and ~2x faster than Schubfach on dtoa-benchmark run on Apple M1.
| Function | Time (ns) | Speedup |
|---|---|---|
| ostringstream | 871.982 | 1.00x |
| sprintf | 737.510 | 1.18x |
| double-conversion | 84.304 | 10.34x |
| to_chars | 42.786 | 20.38x |
| ryu | 37.081 | 23.52x |
| schubfach | 24.885 | 35.04x |
| fmt | 22.274 | 39.15x |
| dragonbox | 20.701 | 42.12x |
| yy | 13.974 | 62.40x |
| zmij | 12.271 | 71.06x |
| null | 0.930 | 937.62x |
Compile time is ~60ms by default and ~68ms with optimizations enabled as measured by
% time c++ -c -std=c++20 zmij.cc [-O2]
taking the best of 3 runs.
- 1 instead of 3 multiplications by powers of 10 in the common case
- Faster logarithm approximations
- Faster division and modulo
- Fewer conditional branches
- More efficient significand and exponent output
- Improved storage of powers of 10
Żmij (pronounced roughly zhmeey or more precisely /ʐmij/) is a Polish word that refers to a mythical dragon- or serpent-like creature. This continues the dragon theme started by Steele and White. Nice feature of this name is that it has a floating point in the first letter.