K
Keith Thompson
Lane Straatman said:#include <stdio.h>
#include <stdlib.h>
#include <complex.h>
#include <math.h>
int main(void)
{
long double complex z1, z3, z4, z5;
long double mod, phi, a, b, c, d;
z1 = .4 + .7I;
a = creal(z1);
b = cimag(z1);
mod = sqrt(pow(a, 2) + pow(b, 2));
phi = atan(b/a);
c = ( (sqrt(mod)) * cos( .5 * phi ) );
d = ( (sqrt(mod)) * sin( .5 * phi ) );
z4 = c + d*I;
z5 = cpow(z1, .5);
z3 = cpow(z4, 2.0);
printf("The square root of %lf %lfi\n", creal(z1), cimag(z1));
printf("is : %lf %lfi\n", creal(z4), cimag(z4));
printf("using cpow to get the root: %lf %lfi\n", creal(z5), cimag(z5));
printf("using cpow to check the square: %lf %lfi\n", creal(z3),
cimag(z3));
system("PAUSE");
return 0;
}
Have I mixed types here in a bad way? LS
The imaginary literals .7I is a gcc extension. You'll get a warning
if you compile with "gcc -std=c99 -pedantic". (It also warns about
"I" in the assignment to z4, but that's a bogus warning.)
The system("PAUSE") is non-portable; try finding some other way to
keep the program's output on the screen.
You declare your variables of type long double or long double complex,
but you use functions that work on double or double complex.
Rather than pow(a, 2), I'd just write a * a.
Th names z1, z2, z3, z4, and z5 make it hard to follow what's going
on.
Those are the only problems I see off the top of my head.