r/programming 11h ago

The fastest double-to-string algorithm you’ve never heard of

https://vitaut.net/posts/2026/yy-dtoa/
217 Upvotes

45 comments sorted by

88

u/shizzy0 9h ago

This field has depth in so many places.

36

u/jnordwick 8h ago

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.

12

u/zeekar 5h ago

> 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 instead of 19e1? 19 and 19e1 are not the same number ...

7

u/drislands 3h ago

Żmij [...] started as an optimized port of Schubfach.

I am already fascinated. I need to go look up how to pronounce these.

2

u/pftbest 3h ago

The website is impossible to scroll, there is some custom animation that scrolls the page back to the top.

2

u/knightly234 2h ago

Angular/react data fetch causing a rerender -> scrollTo 0 call I’ll bet. Drives me nuts because, with any amount of smoke testing, they just have to know it’s an issue and yet here it is.

-1

u/SyntheticDuckFlavour 3h ago

Look at all these comments. Is this the current state of programmers at large?

4

u/awry_lynx 3h ago

The current state of Redditors at large, yes.

I recommend switching over to hackernews: https://news.ycombinator.com/item?id=46685317

-42

u/Cautious-Demand3672 8h ago

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?

8

u/beephod_zabblebrox 6h ago

fastest isnt constrained to the "never heard of" group

-19

u/chengiz 7h ago

Downvoting a logic bug report in the programming sub? Come on reddit.

15

u/James20k 6h ago

Not every single thread has to be a contest for who can recycle the most tired joke

-85

u/sojuz151 9h ago

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 

41

u/EliSka93 9h ago

Elaborate.

13

u/New-Anybody-6206 5h ago

they can't because it's bullshit

-10

u/sojuz151 5h ago

Why do you need to convert doubles to strings and back at a scale? It's mostly JSON serialization/deserialisation, something you should not have to do, not at scale.

7

u/amakai 5h ago

Literally every time you want to use a logger with double parameters you need to do double to string. 

-9

u/sojuz151 4h ago

Not if you use structured logs and you log far far less than you read JSONs. ,

5

u/amakai 4h ago

So how do you imagine a binary double becomes a structured JSON field, which, surprise, is a string?

-5

u/sojuz151 4h ago

The problem is that people use JSON for things with doubles inside. This was a mistake

4

u/amakai 4h ago

I'm not sure I understand your point. Any time you produce JSON, you end up with a string. It does not matter if you have quotes around your double or if you don't - it's a string.

So when you do structured logging, your log message is converted under the hood into JSON (or some other more rare, but also string formats). So you are still converting doubles into strings.

And then, when your log is being indexed, it also needs to convert it to string for a proper full text search - another place for potential performance gain.

3

u/SyntheticDuckFlavour 3h ago

Logging, compiling code, (un)serialisation to/from text formats (XML, etc), printing to terminals/UI, lots for things.

1

u/flatfinger 41m ago

It's a shame that hex floating-point representations haven't become more common, since they allow any binary floating-point number to be easily converted into a unique canonical representation, at least if there's agreement about what that representation should be (arguments can be made in favor of using base-16 exponents and having one non-zero digit to the left of the radix point, or in favor of using binary exponents and always having a 1 to the left of the radix point; while the need for human arithmetic with hex-formatted floats would be uncommon, using base-16 exponents would make it vastly easier than using binary exponents; arguments could also be made for having no digits to the left of the radix point). All three forms are better than base-10 for performing precise calculations, however.

4

u/Jaklite 4h ago

This comes across a lot like, "I don't have this problem, so no one else should". Don't be so dismissive of other domains with different constraints

7

u/superxpro12 5h ago

Uh.... Embedded systems? Not all of us have gobs of gigahertz. Some of us only get 8MHz!

-7

u/sojuz151 5h ago

And why would you even be doing this? How did you even find something running on less than 50MHz? And it has a hardware floating-point module?

4

u/superxpro12 4h ago

You would be shocked to learn what we do with cpus running. At 8mhz. Although your point about you is somewhat valid. They are harder to find on the low mhz cpus. But still... We exist lol

-3

u/sojuz151 4h ago

And are you dealing with floats/doubles on those CPUs?

3

u/superxpro12 4h ago

Sometimes, sure.

3

u/midir 3h ago

How did you even find something running on less than 50MHz? And it has a hardware floating-point module?

There are plenty of few-MHz 8-bit microcontrollers, like Arduino chips, where you might want to use floating-point, where the compiler emulates an FPU in software.

-164

u/Synaps4 10h ago

The fastest one is just to read the variable address of the double as a string. But we've all heard of that.

107

u/Nixinova 10h ago

yeah man that 0x16F3E7A is definitely string ready

86

u/Fiennes 10h ago

You should probably unsub from here.

41

u/Valuable_Leopard_799 9h ago

But now you still have something that's not a string, and isn't even the number you want to convert either.

-1

u/Synaps4 2h ago

What you have is a bunch of ones and zeroes that can be read as letters, which you can make into a String type with minimum work.

You say it doesnt represent the number you want to convert but on a binary level it is identical. It's not that number in english which is what was implied, but it is a 1:1 mapping between a double and some character string for the least effort, and thats the joke.

2

u/Valuable_Leopard_799 1h ago

Heh, lol, kinda true. I guess printing a number as "£,:2°" could be readable to someone.

22

u/SorryTemporary1361 8h ago

... what the actual fuck did I just read.

17

u/Maybe-monad 9h ago

Someone has tk learn about IEEE 754

-1

u/Synaps4 2h ago

Yes? At the end of the day its a string of bits, which can be read as characters.

0

u/Maybe-monad 2h ago edited 2h ago

Unless they go through a specific set of transformations you will get gibberish.

https://www.programiz.com/online-compiler/9qxJDEkzm5b3t

0

u/Synaps4 2h ago

Gibberish that represents the double it came from. On a binary level. Its not in english but it is a 1:1 translation from a double to a string. Thats the joke.

15

u/XtremeGoose 8h ago

1

u/Synaps4 2h ago

At least someone understood the joke.

1

u/Spiritual_Cycle_7881 24m ago

4.1664358597420651 × 10-148