Floating-to-text conversion routine implemented - #23720
Conversation
|
berni44 implemented fast and accurate float to text conversion for Phobos, I would look into adapting those instead of adding another implementation |
|
@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 Also, it would be nice to benchmark it, maybe this implementation isn't that bad in terms of performance |
|
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 |
02d4346 to
0b78965
Compare
|
|
They are but the floating point conversion code doesn't rely on 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. |
|
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) |
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
libmfor 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 (
ushortused as "bigit" container) and also support of nativefloatandrealtypes was added.quadruple,decimal64anddecimal128can 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 onscalblnl()for now)Based on this we can implement our own implementation of
printf()analog with Interpolation Expression Sequences (IES)