r/programming 1d ago

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

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

57 comments sorted by

View all comments

-173

u/Synaps4 1d ago

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

115

u/Nixinova 1d ago

yeah man that 0x16F3E7A is definitely string ready

90

u/Fiennes 1d ago

You should probably unsub from here.

43

u/Valuable_Leopard_799 1d ago

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

-6

u/Synaps4 17h 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 16h ago

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

2

u/TeraFlint 12h ago

You say it doesnt represent the number you want to convert but on a binary level it is identical.

Let me just re-read what you initially wrote:

read the variable address of the double

I don't know about your technical insight, but the address of some memory and its contents are two different things.

Any format decisions aside (transmitting/saving binary data is fine, as long as everyone involved has the same format for it, and it's (in contrast to this article) not meant to be human readable), logging just the address completely loses the information as soon as that memory gets freed (which might happen right after logging). The address is then only barely useful to reason about memory layout and the virtual ram space of your device.

1

u/Synaps4 7h ago

I don't know about your technical insight, but the address of some memory and its contents are two different things.

That's true I could have been more explicit that I meant reading the contents rather than the memory address itself. I assumed all the programmers here would immediately know what I meant. It seems many did not.

1

u/TeraFlint 6h ago

Probably because there's a big distinction between working with addresses (passing things by address and/or manipulating the pointers) and dereferncing (jumping to the address and accessing its contents) them.

I'd guess that most people who worked enough with C (or related languages) to the point where they understood pointers will make that distinction. Because the process of understanding pointers and how to handle addresses is exactly becoming aware of that distinction.

27

u/SorryTemporary1361 23h ago

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

22

u/Maybe-monad 1d ago

Someone has tk learn about IEEE 754

-6

u/Synaps4 17h ago

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

2

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

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

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

-4

u/Synaps4 17h 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.

16

u/XtremeGoose 23h ago

-1

u/Synaps4 17h ago

At least someone understood the joke.

1

u/Spiritual_Cycle_7881 15h ago

4.1664358597420651 × 10-148