Bug 5227 – X ^^ FP at compile-time

Status
RESOLVED
Resolution
FIXED
Severity
blocker
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-11-16T18:47:03Z
Last change time
2018-04-17T17:35:38Z
Keywords
rejects-valid
Assigned to
No Owner
Creator
bearophile_hugs
See also
https://issues.dlang.org/show_bug.cgi?id=3749

Comments

Comment #0 by bearophile_hugs — 2010-11-16T18:47:03Z
Currently this D2 code doesn't work: import std.math; enum x = 2 ^^ 3.5; void main() {} DMD 2.050 prints: ...\dmd\src\phobos\std\math.d(3214): Error: powl cannot be interpreted at compile time, because it has no available source code ...\dmd\src\phobos\std\math.d(3217): Error: cannot evaluate impl(x,y) at compile time test.d(2): Error: cannot evaluate pow(2,3.5) at compile time test.d(2): Error: cannot evaluate pow(2,3.5) at compile time This limit may be removed using the ctfe_exp() and ctfe_log() functions of Comment 6 here: http://d.puremagic.com/issues/show_bug.cgi?id=3749#c6 Replacing this line 3214 of std.math.d: return core.stdc.math.powl(x, y); With something like (not tested much): if (__ctfe) { return ctfe_exp(x * ctfe_log(b)); } else { return core.stdc.math.powl(x, y); } A small precision test, computing 2 ^^ 3.5: core powl = 11.313708498984761163 exp log CTFE = 11.313708498984760390 A more precise result with an algebra system shows that the CTFE version is the more precise one of the two, it's correct to the last digit of the real number: pow(2, 3.5) = 11.313708498984760390413509793677584628557375003015584...
Comment #1 by code — 2012-02-05T18:03:41Z
There is missing CTFE support for some intrinsics. Without exp and log family functions this can't be implemented.
Comment #2 by safety0ff.bugz — 2014-12-31T16:37:44Z
*** Issue 13917 has been marked as a duplicate of this issue. ***
Comment #3 by safety0ff.bugz — 2014-12-31T16:37:57Z
*** Issue 12946 has been marked as a duplicate of this issue. ***
Comment #4 by safety0ff.bugz — 2014-12-31T16:38:10Z
*** Issue 12412 has been marked as a duplicate of this issue. ***
Comment #5 by safety0ff.bugz — 2014-12-31T16:38:13Z
*** Issue 8562 has been marked as a duplicate of this issue. ***
Comment #6 by turkeyman — 2015-01-01T07:47:45Z
Okay, so this has obviously come up a lot. It's a real problem not being able to express any non-linear function in CTFE. I'm changing this to a high-priority bug, rather than an enhancement request. I don't think pow() was deliberately designed not to support CTFE(?). It's an operator (^^), and users should surely expect that all basic operators work in CTFE.
Comment #7 by turkeyman — 2015-05-08T11:42:34Z
I'm changing this to blocker. Seriously, it's gone on long enough. 5 years apparently according to this bug report. It is actually blocking a whole bunch of projects. They are literally sitting waiting for this bugfix. We can't generate compile-time data/tables without this super-important function. There's no substitute or workaround. We need to be able to evaluate non-linear functions in CTFE. log() works! pow() is the rather more frequently occurring compliment.
Comment #8 by ibuclaw — 2015-05-08T16:18:29Z
(In reply to Manu from comment #7) > log() works! I doubt that, CTFE isn't powerful enough yet.
Comment #9 by simen.kjaras — 2015-05-08T16:28:01Z
(In reply to Iain Buclaw from comment #8) > (In reply to Manu from comment #7) > > log() works! > > I doubt that, CTFE isn't powerful enough yet. Disbelieve it all you want, it does work.
Comment #10 by turkeyman — 2015-05-09T00:59:27Z
(In reply to Iain Buclaw from comment #8) > (In reply to Manu from comment #7) > > log() works! > > I doubt that, CTFE isn't powerful enough yet. pragma(msg, log(101.2)); > 4.6171L pragma(msg, 2^^1.2); C:\dev\D\dmd2\windows\bin\..\..\src\phobos\std\math.d(4463): Error: cannot convert &real to ushort* at compile time C:\dev\D\dmd2\windows\bin\..\..\src\phobos\std\math.d(5688): called from here: isNaN(y) C:\dev\D\dmd2\windows\bin\..\..\src\phobos\std\math.d(5860): called from here: impl(cast(real)x, cast(real)y)
Comment #11 by ibuclaw — 2015-05-11T09:54:36Z
(In reply to Manu from comment #10) > (In reply to Iain Buclaw from comment #8) > > (In reply to Manu from comment #7) > > > log() works! > > > > I doubt that, CTFE isn't powerful enough yet. > > > pragma(msg, log(101.2)); > > > 4.6171L > Ah, DMD cheats here - I didn't see neither the INLINE_Y2LX path, nor the fact that DMD apparently executes the fy2lx instruction at compile time for you. I wouldn't rely on this behaviour if I were you.
Comment #12 by turkeyman — 2015-05-11T10:09:57Z
(In reply to Iain Buclaw from comment #11) > (In reply to Manu from comment #10) > > (In reply to Iain Buclaw from comment #8) > > > (In reply to Manu from comment #7) > > > > log() works! > > > > > > I doubt that, CTFE isn't powerful enough yet. > > > > > > pragma(msg, log(101.2)); > > > > > 4.6171L > > > > Ah, DMD cheats here - I didn't see neither the INLINE_Y2LX path, nor the > fact that DMD apparently executes the fy2lx instruction at compile time for > you. > > I wouldn't rely on this behaviour if I were you. That's worrying. Either way, this is the most basic non-linear maths. We really really do need this accessible in CTFE. Especially considering x^^y is an operator!
Comment #13 by clem — 2015-05-22T12:45:35Z
Comment #14 by ibuclaw — 2017-12-12T22:52:29Z
*** Issue 18069 has been marked as a duplicate of this issue. ***
Comment #15 by bugzilla — 2018-03-23T08:40:49Z
*** Issue 16474 has been marked as a duplicate of this issue. ***
Comment #16 by bugzilla — 2018-03-23T09:20:03Z
Comment #17 by github-bugzilla — 2018-04-17T08:26:22Z
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/6767a32f6988c6794815405e01f07250177d6098 fix Issue 5227 - X ^^ FP at compile-time https://github.com/dlang/dmd/commit/a4fbc9829da9e4b3e6d73859a42dc6e624e9b790 Merge pull request #8071 from WalterBright/fix5227 fix Issue 5227 - X ^^ FP at compile-time merged-on-behalf-of: Walter Bright <[email protected]>
Comment #18 by turkeyman — 2018-04-17T17:35:38Z
Yay! 8 years in the making. This must be a very fine brew! :P