Comment #0 by bearophile_hugs — 2012-03-11T14:12:35Z
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 by hsteoh — 2012-03-28T08:04:13Z
Most std.math functions need use asm; we need to write ctfe versions for them.
Comment #2 by bearophile_hugs — 2012-03-28T10:03:17Z
(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 by hsteoh — 2012-03-28T10:05:51Z
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 by clugdbug — 2012-03-28T11:12:44Z
(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 by andy — 2015-01-27T03:00:53Z
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 by bearophile_hugs — 2015-01-27T12:13:48Z
(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 by n8sh.secondary — 2018-10-16T19:48:16Z