Skip to content

Floating-to-text conversion routine implemented - #23720

Open
denizzzka wants to merge 1 commit into
dlang:masterfrom
denizzzka:ftot_done
Open

Floating-to-text conversion routine implemented#23720
denizzzka wants to merge 1 commit into
dlang:masterfrom
denizzzka:ftot_done

Conversation

@denizzzka

@denizzzka denizzzka commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Nobody asked for this, but if we want to get rid of libc, we'll have to - for some reason humanity hasn't created a universal library for converting floating-point numbers to text (like libm for math).

I implemented the algorithm "double to string conversion in 150 lines of code". Articles promised it's about as fast as the ones used glibc - much better than "naive". And it is also have "understandable" feature - i.e., ~anyone can just look at it with eyes and immediately understand how it wroks.

The difference from the "base version" is that support for 32-bit systems has been added (ushort used as "bigit" container) and also support of native float and real types was added. quadruple, decimal64 and decimal128 can also be easily added when needed.

And unlike "base version" non-scientific format (%f) support added too.

Compatibility mode choice (Phobos or Libc) are implemented. Libc mode allows to compare results with well-tested libc implementations during testing.

Code is nothrow @nogc @safe and pure (but not for real - its support rely on scalblnl() for now)

Based on this we can implement our own implementation of printf() analog with Interpolation Expression Sequences (IES)

Comment thread druntime/src/core/internal/string.d Outdated
@dkorpel

dkorpel commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

berni44 implemented fast and accurate float to text conversion for Phobos, I would look into adapting those instead of adding another implementation

@denizzzka

Copy link
Copy Markdown
Contributor Author

@dkorpel I'm almost certain it's impossible without a lot of effort: exceptions and GC are available in Phobos, but we want to get rid of it in the druntime because we may want to use floating output code at very early stages (for example when druntime shows GC settings help screen)

On the other hand, Phobos can use this code because core/internal is available for it.

Also, it would be nice to benchmark it, maybe this implementation isn't that bad in terms of performance

@denizzzka

Copy link
Copy Markdown
Contributor Author

It is important to clarify: I didn't make a simple ("naive") implementation because it's almost the same size (in code lines, maybe 50 lines less or so), so there was no point in it

@denizzzka

denizzzka commented Aug 27, 2026

Copy link
Copy Markdown
Contributor Author

Windows issue: #23726

@dkorpel

dkorpel commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

exceptions and GC are available in Phobos

They are but the floating point conversion code doesn't rely on it

On the other hand, Phobos can use this code because core/internal is available for it.

berni44 did a lot of testing to ensure it handled all edge cases, had good performance, and covered all of Phobos' use cases. I don't see a reason to re-implement that from scratch in druntime and then replace Phobos's code with that. Worst case you introduce a bunch of regressions, best case it's the same as before, so you might as well just have ported the code instead of rewriting it.

@denizzzka

Copy link
Copy Markdown
Contributor Author

I don't have the resources to transfer floating point code from Phobos - it's large and complex task. When someone does this it will be possible to delete this code.

Complex code (supporting all formatting features, etc) is not needed in the druntime: we just need to be able to show 3-4 floating point numbers and use it for some low-level debugging.

I'm not suggesting replacing everything with my code: this is a fallback in case libc isn't available (currently we havent such platforms supported in the official sources) so that druntime can work without it.

And I can add all tests from Phobos (Just show me where they are - I couldn't find them)

@denizzzka
denizzzka marked this pull request as ready for review August 28, 2026 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants