jacob navia wrote:

hi

I am trying to use the complex data type.

Consider this code, taken from the cclib library:

struct complex csqrt(Cpx z)

{ double r;

r=sqrt(z.re*z.re+z.im*z.im);

r=sqrt(ldexp(r+fabs(z.re),-1));

if(r==0.) z.re=z.im=0.;

else{

if(z.re>=0.){ z.re=r; z.im=ldexp(z.im/r,-1);}

else{ z.re=ldexp(fabs(z.im)/r,-1);

if(z.im>=0.) z.im=r; else z.im= -r;

}

}

return z;

}

Now, this works in lcc-win easily because lcc-win

allows access to the real and imaginary parts of the complex

data type easily, using

z.re (real part)

and

ze.im

for the imaginary part.

I do not see how can be this written in standard C.

Has anyone an idea?

Your function works just fine in standard C. Check the following:

#include <math.h>

#include <stdio.h>

/* if you don't have <complex.h>, remove or comment out the line below */

#define USE_COMPLEX_H

typedef struct cmplx

{

double re, im;

} Cpx;

/* 'cmplx' chosen for the name of the struct since 'complex' should be

<complex.h*/

Cpx Csqrt

/* renamed because csqrt should be in <complex.h*/

(Cpx z)

{

double r;

r = sqrt(z.re * z.re + z.im * z.im);

r = sqrt(ldexp(r + fabs(z.re), -1));

if (r == 0.)

z.re = z.im = 0.;

else {

if (z.re >= 0.) {

z.re = r;

z.im = ldexp(z.im / r, -1);

}

else {

z.re = ldexp(fabs(z.im) / r, -1);

if (z.im >= 0.)

z.im = r;

else

z.im = -r;

}

}

return z;

}

Cpx Csquare(Cpx z)

{

Cpx r;

r.re = z.re * z.re - z.im * z.im;

r.im = 2 * z.re * z.im;

return r;

}

#if defined(USE_COMPLEX_H)

#include <complex.h>

double complex Csqrt2(double complex z)

{

double r;

r = sqrt(creal(z) * creal(z) + cimag(z) * cimag(z));

r = sqrt(ldexp(r + fabs(creal(z)), -1));

if (r == 0.)

z = 0.;

else {

if (creal(z) >= 0.) {

z = r + ldexp(cimag(z) / r, -1) * I;

}

else {

z = ldexp(fabs(cimag(z)) / r,

-1) + (cimag(z) >= 0. ? r : -r) * I;

}

}

return z;

}

#endif

int main(void)

{

Cpx x, y, z;

#if defined(USE_COMPLEX_H)

double complex cx, cy, cz;

#endif

int i, j;

printf("Using the function Jacob Navia provided,\n"

"with a struct for complex types.\n\n");

for (i = -2; i < 3; i++)

for (j = -2; j < 3; j++) {

x.re = i;

x.im = j;

y = Csqrt(x);

z = Csquare(y);

printf("y = Csqrt(%g+%gi)) = %g+%gi, y*y=%g+%gi\n",

x.re, x.im, y.re, y.im, z.re, z.im);

}

#if defined(USE_COMPLEX_H)

printf("\nUsing the function Jacob Navia provided,\n"

"rewritten to use complex types.\n\n");

for (i = -2; i < 3; i++)

for (j = -2; j < 3; j++) {

cx = i + j * I;

cy = Csqrt2(cx);

cz = cy * cy;

printf("y = Csqrt2(%g+%gi)) = %g+%gi, y*y=%g+%gi\n",

creal(cx), cimag(cx),

creal(cy), cimag(cy),

creal(cz), cimag(cz));

}

#endif

return 0;

}

[Output]

Using the function Jacob Navia provided,

with a struct for complex types.

y = Csqrt(-2+-2i)) = 0.643594+-1.55377i, y*y=-2+-2i

y = Csqrt(-2+-1i)) = 0.343561+-1.45535i, y*y=-2+-1i

y = Csqrt(-2+0i)) = 0+1.41421i, y*y=-2+0i

y = Csqrt(-2+1i)) = 0.343561+1.45535i, y*y=-2+1i

y = Csqrt(-2+2i)) = 0.643594+1.55377i, y*y=-2+2i

y = Csqrt(-1+-2i)) = 0.786151+-1.27202i, y*y=-1+-2i

y = Csqrt(-1+-1i)) = 0.45509+-1.09868i, y*y=-1+-1i

y = Csqrt(-1+0i)) = 0+1i, y*y=-1+0i

y = Csqrt(-1+1i)) = 0.45509+1.09868i, y*y=-1+1i

y = Csqrt(-1+2i)) = 0.786151+1.27202i, y*y=-1+2i

y = Csqrt(0+-2i)) = 1+-1i, y*y=0+-2i

y = Csqrt(0+-1i)) = 0.707107+-0.707107i, y*y=1.5702e-16+-1i

y = Csqrt(0+0i)) = 0+0i, y*y=0+0i

y = Csqrt(0+1i)) = 0.707107+0.707107i, y*y=1.5702e-16+1i

y = Csqrt(0+2i)) = 1+1i, y*y=0+2i

y = Csqrt(1+-2i)) = 1.27202+-0.786151i, y*y=1+-2i

y = Csqrt(1+-1i)) = 1.09868+-0.45509i, y*y=1+-1i

y = Csqrt(1+0i)) = 1+0i, y*y=1+0i

y = Csqrt(1+1i)) = 1.09868+0.45509i, y*y=1+1i

y = Csqrt(1+2i)) = 1.27202+0.786151i, y*y=1+2i

y = Csqrt(2+-2i)) = 1.55377+-0.643594i, y*y=2+-2i

y = Csqrt(2+-1i)) = 1.45535+-0.343561i, y*y=2+-1i

y = Csqrt(2+0i)) = 1.41421+0i, y*y=2+0i

y = Csqrt(2+1i)) = 1.45535+0.343561i, y*y=2+1i

y = Csqrt(2+2i)) = 1.55377+0.643594i, y*y=2+2i

Using the function Jacob Navia provided,

rewritten to use __complex__ types.

y = Csqrt2(-2+-2i)) = 0.643594+-1.55377i, y*y=-2+-2i

y = Csqrt2(-2+-1i)) = 0.343561+-1.45535i, y*y=-2+-1i

y = Csqrt2(-2+0i)) = 0+1.41421i, y*y=-2+0i

y = Csqrt2(-2+1i)) = 0.343561+1.45535i, y*y=-2+1i

y = Csqrt2(-2+2i)) = 0.643594+1.55377i, y*y=-2+2i

y = Csqrt2(-1+-2i)) = 0.786151+-1.27202i, y*y=-1+-2i

y = Csqrt2(-1+-1i)) = 0.45509+-1.09868i, y*y=-1+-1i

y = Csqrt2(-1+0i)) = 0+1i, y*y=-1+0i

y = Csqrt2(-1+1i)) = 0.45509+1.09868i, y*y=-1+1i

y = Csqrt2(-1+2i)) = 0.786151+1.27202i, y*y=-1+2i

y = Csqrt2(0+-2i)) = 1+-1i, y*y=0+-2i

y = Csqrt2(0+-1i)) = 0.707107+-0.707107i, y*y=1.5702e-16+-1i

y = Csqrt2(0+0i)) = 0+0i, y*y=0+0i

y = Csqrt2(0+1i)) = 0.707107+0.707107i, y*y=1.5702e-16+1i

y = Csqrt2(0+2i)) = 1+1i, y*y=0+2i

y = Csqrt2(1+-2i)) = 1.27202+-0.786151i, y*y=1+-2i

y = Csqrt2(1+-1i)) = 1.09868+-0.45509i, y*y=1+-1i

y = Csqrt2(1+0i)) = 1+0i, y*y=1+0i

y = Csqrt2(1+1i)) = 1.09868+0.45509i, y*y=1+1i

y = Csqrt2(1+2i)) = 1.27202+0.786151i, y*y=1+2i

y = Csqrt2(2+-2i)) = 1.55377+-0.643594i, y*y=2+-2i

y = Csqrt2(2+-1i)) = 1.45535+-0.343561i, y*y=2+-1i

y = Csqrt2(2+0i)) = 1.41421+0i, y*y=2+0i

y = Csqrt2(2+1i)) = 1.45535+0.343561i, y*y=2+1i

y = Csqrt2(2+2i)) = 1.55377+0.643594i, y*y=2+2i