Bug 20688 – Wrong code when linking to C complex number functions

Status
RESOLVED
Resolution
WORKSFORME
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2020-03-20T02:36:31Z
Last change time
2020-09-07T22:58:34Z
Keywords
backend, wrong-code
Assigned to
No Owner
Creator
Vladimir Panteleev

Comments

Comment #0 by dlang-bugzilla — 2020-03-20T02:36:31Z
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).
Comment #1 by bugzilla — 2020-09-07T05:32:11Z
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
Comment #2 by dlang-bugzilla — 2020-09-07T12:05:12Z
(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.
Comment #3 by dlang-bugzilla — 2020-09-07T12:10:12Z
Comment #4 by bugzilla — 2020-09-07T22:49:01Z
It works correctly when I try it with master. Perhaps this is because I have fixed other bugs regarding structs in the meantime.
Comment #5 by dlang-bugzilla — 2020-09-07T22:51:46Z
Confirmed, fixed in master. Thanks!
Comment #6 by bugzilla — 2020-09-07T22:57:51Z
I added a test case to ensure it.
Comment #7 by bugzilla — 2020-09-07T22:58:34Z