D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 18630 - std.math must CTFE
Summary: std.math must CTFE
Status: RESOLVED DUPLICATE of issue 17007
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P1 blocker
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-03-19 00:21 UTC by Manu
Modified: 2019-09-04 19:33 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Manu 2018-03-19 00:21:02 UTC
Every single function in std.math must CTFE.

At very least, let's start with pow, log, exp, sin/cos and friends...

Curves exist. Populating tables with curve data is a thing that I *constantly* want to do.

I usually workaround with a static constructor that pre-computes tables at startup, but that only solves some subset of cases. When I want those tables to interact with other CTFE code, I'm blocked.
Comment 1 Eduard Staniloiu 2018-11-29 14:02:42 UTC
I think they already are, but I could be wrong.

Did you try forcing the compile time evaluation?
I had no problem running the following

```
enum r = pow(2.0, 5);
static assert(r == 32.0);

static assert(log(E) == 1);

enum f = sin(-2.0f);
static assert(fabs(f -  -0.909297f) < .00001);
```

Those are all altered unittests from std.math, to which I added `enum varName` and `static assert` instead of `assert`
Comment 2 Eduard Staniloiu 2018-11-29 14:31:57 UTC
(In reply to Manu from comment #0)
> I usually workaround with a static constructor that pre-computes tables at
> startup, but that only solves some subset of cases. When I want those tables
> to interact with other CTFE code, I'm blocked.

Maybe you can replace the static ctor with this idiom
https://p0nce.github.io/d-idioms/#Precomputed-tables-at-compile-time-through-CTFE
Comment 3 Simen Kjaeraas 2018-11-30 08:54:20 UTC
(In reply to Eduard Staniloiu from comment #2)
> Maybe you can replace the static ctor with this idiom
> https://p0nce.github.io/d-idioms/#Precomputed-tables-at-compile-time-through-
> CTFE

That's exactly what he's trying to do. In 2.083.0, the following functions in std.math are still not CTFE-able:

nextPow2
truncPow2
asin
atan2
expi
lround
lrint
rndtol
quantize
frexp
ilogb
scalbn

Getting there, though - in 2.081.1, the list was about twice as big.
Comment 4 Nicholas Wilson 2019-06-10 07:56:08 UTC
asin now works at compile time.

The rest still fail for the following reasons.

nextPow2  -- reinterpret
truncPow2 -- reinterpret
atan2     -- reinterpret
expi      -- asm
lround    -- no avail source
lrint     -- ditto
rndtol    -- ditto
uantize   -- ditto
frexp     -- reinterpret
ilogb     -- y.vu[3] is used before initialised
scalbn    -- asm
Comment 5 Nathan S. 2019-08-23 21:51:52 UTC
Marking this as a duplicate of #17007 since that one is older even though this is the issue with discussion.

*** This issue has been marked as a duplicate of issue 17007 ***