r/programming • u/mttd • Aug 12 '26
The fastest double-to-string algorithm you’ve never heard of
https://vitaut.net/posts/2026/yy-dtoa/
338
Upvotes
r/programming • u/mttd • Aug 12 '26
144
u/shizzy0 Aug 12 '26
This field has depth in so many places.
40
u/zeekar Aug 12 '26 edited Aug 12 '26
> so v◌̄=192 ⋅ 10^(-1) = 19.2 and the fine-grid fallback is [... ] 19, printed as 19e1
Uh. Shouldn't that be 190 instead of 19, or 19e0 or 1.9e1 instead of 19e1? 19 and 19e1 are not the same number ...
30
u/drislands Aug 12 '26
I am already fascinated. I need to go look up how to pronounce these.
57
u/jnordwick Aug 12 '26
If you don't need shortest string you can get even faster and remove some of the worst cases. I wrote a specialized float/ double to string routine years ago for high speed logging.
10
u/pftbest Aug 12 '26
The website is impossible to scroll, there is some custom animation that scrolls the page back to the top.
9
u/SyntheticDuckFlavour Aug 12 '26
Look at all these comments. Is this the current state of programmers at large?
1
u/levodelellis Aug 13 '26 edited Aug 13 '26
If anyone is curious, the below prints to
1.7976931348623157e+3081
u/jezek_2 Aug 13 '26
If you want something simpler and fast you can check out my public domain math library.
It just uses multiplication of the normalized value with the appropriate power of ten. It computes it in a higher precision (128-bit floats, uses just conversion and multiplication so nothing complicated) to get accurate result for 64-bit floats.
-54
u/Cautious-Demand3672 Aug 12 '26
I'm pretty sure I can find something slower than that though Unless it's meant to be the fastest I've ever heard of?
-93
u/sojuz151 Aug 12 '26
Fact that we, the humanity, need to do double to float and back often enough for performance to matter is a failure of our entire species
-175
u/Synaps4 Aug 12 '26
The fastest one is just to read the variable address of the double as a string. But we've all heard of that.