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