math.h functions for type _Complex

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.
 
C

CBFalconer

Keith said:
.... snip ...

The system("PAUSE") is non-portable; try finding some other way to
keep the program's output on the screen.

This is almost a sure sign of (mis)using Visual C's IDE. Which can
be cured by simply using a DOSBOX. Borlands IDE at least has a
means of showing the user screen (or used to).

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top