D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7691 - std.math.floor at compile-time too
Summary: std.math.floor at compile-time too
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86 Windows
: P2 enhancement
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2012-03-11 14:12 UTC by bearophile_hugs
Modified: 2018-10-16 19:48 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 bearophile_hugs 2012-03-11 14:12:35 UTC
import std.math: floor;
pure void main() {
    enum double x = floor(2.3);
}


DMD 2.059head gives:

test.d(3): Error: pure function 'main' cannot call impure function 'floor'
...\dmd2\src\phobos\std\math.d(1917): Error: floorl cannot be interpreted at compile time, because it has no available source code
test.d(3):        called from here: floor(2.3L)


So there are two problems here, floor is not pure and it can't run at compile-time.

std.math.fmod() too isn't pure.
Comment 1 hsteoh 2012-03-28 08:04:13 UTC
Most std.math functions need use asm; we need to write ctfe versions for them.
Comment 2 bearophile_hugs 2012-03-28 10:03:17 UTC
(In reply to comment #1)
> Most std.math functions need use asm; we need to write ctfe versions for them.

But note that I'd like to use it mostly in run-time code.
Comment 3 hsteoh 2012-03-28 10:05:51 UTC
Does dmd even support pureness checking for asm blocks? If not, I'm not sure when we will be able to mark asm functions as pure.
Comment 4 Don 2012-03-28 11:12:44 UTC
(In reply to comment #3)
> Does dmd even support pureness checking for asm blocks? If not, I'm not sure
> when we will be able to mark asm functions as pure.

asm blocks are ignored for pureness checking.

And I think it has to be that way. In the asm for BigInt, at one point I write to a static variable. That variable is never read from, EVER. It's a trick to force Intel processors to stay in sync every pass through the loop.

It would be impossible to enforce, anyway.

BTW: floor() isn't pure, because floorl() isn't pure,  because floorl(), being a C function, may set the matherr variable.
Comment 5 AndyC 2015-01-27 03:00:53 UTC
In dmd 2.066.1, I get:
$ dmd test.d
/usr/include/dmd/phobos/std/math.d(4183): Error: Cannot convert &double to ulong* at compile time
/usr/include/dmd/phobos/std/math.d(3288):        called from here: isNaN(x)
test.d(4):        called from here: floor(2.3)


Any reason it has to be enum?  Changing it to either const or immutable compiles fine:

pure void main() {
    const double x = floor(2.3);
}
Comment 6 bearophile_hugs 2015-01-27 12:13:48 UTC
(In reply to AndyC from comment #5)

> Any reason it has to be enum?

D needs to support basic operations like floor() at compile time too. I change the title of his issue.
Comment 7 Nathan S. 2018-10-16 19:48:16 UTC
std.math.floor is `pure` as of https://github.com/dlang/phobos/pull/1426 and currently works in CTFE.