When converting a float to a string from within a mixin an error is given. Example: import std.conv; enum doubleString = mixin("to!string(0.6)"); Gives the following error: ..src\phobos\std\format.d(1355): Error: _snprintf cannot be interpreted at compile time, because it has no available source code The reason for the error is clear: _snprintf is an external function and can therefore not be interpreted. However, I think that compile-time conversion of floats to strings should be supported. Perhaps the formatting code can be ported to the std library instead of using an external function?
The use of snprintf also causes other issues like purity/safety issues. I think we should roll our own native D implementation of these conversions so that it's usable in pure/safe/@nogc/ctfe.
*** Issue 15497 has been marked as a duplicate of this issue. ***
*** Issue 12181 has been marked as a duplicate of this issue. ***
Stefan Koch has ported "a short and efficient implementation of the grisu2 algorithm" to CTFEable D code: https://forum.dlang.org/post/pfnyycxolnrlxtgzimgp@forum.dlang.org
Any chance to make this come into phobos work with good tests? The D implementation could also be faster possible that what it is trying to do now (with snprintf, which does a lot of other stuff, including varags stuff, and can't be optimized well by D compiler). I had this issue: bench.d(358): called from here: to(bm(1LU, 18446744073709551615LU, null, null, 0.1F)) bench.d(358): while evaluating pragma(msg, "Generating benchmark code for: bench.bm_pow_Dop_14 using default or user options: " ~ to(bm(1LU, 18446744073709551615LU, null, null, 0.1F))) bm is a struct here that has a float and double fields, and I want to use to!string on it during compile time for some compile time logging and error messages when some conditions are wrong.
BTW. Mixing is not needed to trigger the issue: import std.conv : to; enum x = to!string(0.1); is enough: /usr/lib/ldc/x86_64-linux-gnu/include/d/std/exception.d(516): Error: uncaught CTFE exception std.format.FormatException("Cannot format floating point types at compile-time") bench.d(338): called from here: to(0.1)
https://github.com/dlang/phobos/pull/7264 could likely help.
Looks like this is implemented pending release: https://dlang.org/changelog/pending.html#formatting_floats_in_CTFE
*** Issue 21573 has been marked as a duplicate of this issue. ***
(In reply to Nick Treleaven from comment #8) > Looks like this is implemented pending release: > https://dlang.org/changelog/pending.html#formatting_floats_in_CTFE Yet only for float and double; reals are still missing, but on the way... [1] (at least x87 reals) [1] https://github.com/dlang/phobos/pull/7907
@berni44 created dlang/phobos pull request #7951 "std.format: Replace snprintf for x87-reals" fixing this issue: - Fix Issue 8424 - Compile time conversions of double/floats to strings https://github.com/dlang/phobos/pull/7951
dlang/phobos pull request #7951 "std.format: Replace snprintf for x87-reals" was merged into master: - c75bd8756d148cf3de0b44fd71d4f667da44ef6c by berni44: Fix Issue 8424 - Compile time conversions of double/floats to strings https://github.com/dlang/phobos/pull/7951