Bug 8424 – Compile time conversions of double/floats to strings

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-07-24T03:22:23Z
Last change time
2021-04-12T07:25:24Z
Keywords
bootcamp, CTFE, pull
Assigned to
No Owner
Creator
Bassieg
See also
https://issues.dlang.org/show_bug.cgi?id=19124, https://issues.dlang.org/show_bug.cgi?id=3248

Comments

Comment #0 by mail — 2012-07-24T03:22:23Z
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?
Comment #1 by hsteoh — 2014-09-21T23:54:38Z
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.
Comment #2 by ag0aep6g — 2016-01-02T09:42:28Z
*** Issue 15497 has been marked as a duplicate of this issue. ***
Comment #3 by b2.temp — 2017-09-13T18:19:53Z
*** Issue 12181 has been marked as a duplicate of this issue. ***
Comment #4 by nick — 2018-12-27T12:49:35Z
Stefan Koch has ported "a short and efficient implementation of the grisu2 algorithm" to CTFEable D code: https://forum.dlang.org/post/[email protected]
Comment #5 by witold.baryluk+d — 2020-05-10T23:58:08Z
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.
Comment #6 by witold.baryluk+d — 2020-05-10T23:59:35Z
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)
Comment #7 by greeenify — 2020-05-11T00:37:31Z
Comment #8 by nick — 2021-03-19T20:02:23Z
Looks like this is implemented pending release: https://dlang.org/changelog/pending.html#formatting_floats_in_CTFE
Comment #9 by bugzilla — 2021-03-28T10:31:20Z
*** Issue 21573 has been marked as a duplicate of this issue. ***
Comment #10 by bugzilla — 2021-03-28T10:33:23Z
(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
Comment #11 by dlang-bot — 2021-04-11T10:17:40Z
@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
Comment #12 by dlang-bot — 2021-04-12T07:25:24Z
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