This is my code:
#include <stdio.h>
#include <tgmath.h>
double complex execmathfunc(double complex val, double complex (*mathfunc)(double complex))
{
return mathfunc(val);
}
int main()
{
double complex val = -1;
val = execmathfunc(val, sqrt); //if I put csqrt here it works
printf("%.1f+%.1fi", creal(val), cimag(val));
return 0;
}
This code works if I use csqrt
, and will output 0.0+1.0i
as expected, but if I use regular sqrt
(putting my hopes in the type-generic functions of tgmath.h
), it outputs a garbage value for the real part and 0 for the imaginary part. Is there any way to get around that or do I need to stick with csqrt
and all the other complex functions when passing them as arguments?
I believe the code behaves this way because the tgmath functions are implemented as functional macros and those only expand if the name is followed by ()
.