C program: /////////////////// test.c /////////////////// #include <stdio.h> #include <complex.h> int main() { double complex r = cpow(2 + 0*I, 2 + 0*I); printf("%f+%fi\n", creal(r), cimag(r)); return 0; } ////////////////////////////////////////////// As expected, this prints 4.000000+0.000000i. Equivalent D program: ////////////////// test.d ///////////////// import std.stdio; import core.stdc.complex; void main() { cdouble r = cpowf(2. + 0i, 2. + 0i); printf("%f+%fi\n", creal(r), cimag(r)); } /////////////////////////////////////////// This prints garbage (for me, randomly -0.000000+-0.000000i or nan+nani).
The %f format is for doubles, and creal/cimag return reals. To fix: printf("%f+%fi\n", cast(double)creal(r), cast(double)cimag(r)); which prints: 4.000000+0.000000i
(In reply to Walter Bright from comment #1) > The %f format is for doubles, and creal/cimag return reals. So: https://github.com/dlang/druntime/blob/master/src/core/stdc/complex.d#L153 creal is weirdly an alias to "complex" instead of a function. > To fix: No, still a problem on Linux/x86_64.
Demonstration: https://run.dlang.io/is/haL0K3
It works correctly when I try it with master. Perhaps this is because I have fixed other bugs regarding structs in the meantime.
Confirmed, fixed in master. Thanks!
I added a test case to ensure it.
https://github.com/dlang/dmd/pull/11710