D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 8424 - Compile time conversions of double/floats to strings
Summary: Compile time conversions of double/floats to strings
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: bootcamp, CTFE, pull
: 12181 15497 21573 (view as issue list)
Depends on:
Blocks:
 
Reported: 2012-07-24 03:22 UTC by Bassieg
Modified: 2021-04-12 07:25 UTC (History)
9 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Bassieg 2012-07-24 03:22:23 UTC
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 hsteoh 2014-09-21 23:54:38 UTC
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 ag0aep6g 2016-01-02 09:42:28 UTC
*** Issue 15497 has been marked as a duplicate of this issue. ***
Comment 3 basile-z 2017-09-13 18:19:53 UTC
*** Issue 12181 has been marked as a duplicate of this issue. ***
Comment 4 Nick Treleaven 2018-12-27 12:49:35 UTC
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
Comment 5 Witold Baryluk 2020-05-10 23:58:08 UTC
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 Witold Baryluk 2020-05-10 23:59:35 UTC
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 Seb 2020-05-11 00:37:31 UTC
https://github.com/dlang/phobos/pull/7264 could likely help.
Comment 8 Nick Treleaven 2021-03-19 20:02:23 UTC
Looks like this is implemented pending release:
https://dlang.org/changelog/pending.html#formatting_floats_in_CTFE
Comment 9 Berni44 2021-03-28 10:31:20 UTC
*** Issue 21573 has been marked as a duplicate of this issue. ***
Comment 10 Berni44 2021-03-28 10:33:23 UTC
(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 Dlang Bot 2021-04-11 10:17:40 UTC
@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 Dlang Bot 2021-04-12 07:25:24 UTC
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