wth!! ctfe cannot format floating point at compile time?

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 14 06:11:20 PDT 2017


On 8/13/17 11:44 PM, Adam D. Ruppe wrote:
> On Monday, 14 August 2017 at 01:52:16 UTC, Johnson Jones wrote:
>> pretty simply, trying to convert a floating point to a string in a 
>> ctfe function and it thinks that it is too complex to do in a ctfe, 
>> really?
> 
> It uses a C function to do the conversion, which is not available at 
> compile time since CTFE can't run extern functions.
> 
> And it is a LOT harder to do than you might think which is why it still 
> uses the C function - implementing one in D is legit pretty complex.
> 
> When I first saw this too, I figured it must be simple to slap something 
> together, even if it is horribly inefficient and doesn't work 
> everywhere... and I ended up giving up after spending a few hours on it 
> too. If you search the web, you get academic papers describing it, eeek.
> 
> Of course, it might be reasonable to port one of the C implementations 
> directly... but even that is a fairly big job (and you might as well 
> just call the C function itself for all but CTFE cases) - the GNU one is 
> 1,300 lines, for example. And that has an incompatible license with the 
> rest of druntime!

Another reasonable idea is to have the compiler call the function for 
you. Since the compiler only ever exists on platforms with libc 
available, the libc function is available to the compiler too. having 
some special intrinsic to format floating points wouldn't be out of the 
question.

It is quite limiting to have floating point string conversions not 
available.

-Steve


More information about the Digitalmars-d-learn mailing list