www.digitalmars.com         C & C++   DMDScript  

c++ - Problem with complex number function cimag()

reply "John Smith" <jsmith email.com> writes:
Consider the following C code:

#include <stdio.h>
#include <complex.h>

int main(void)
{
  double complex a = 2.0 + 0.0 * I;
  double complex b = 1.5 + .5 * I;
  double complex c = cpow(a, b);
  double complex csum = a + b;
  double r = creal(c);
  double i = cimag(c);
  printf("%f %f\n", r, i);
  printf("%f %f\n", creal(csum), cimag(csum));

  return 0;
}

I want to raise the complex number 2.0 +0.0i to the complex power 1.5 +
0.5i.
The result of this calculation is approximately 2.66 + .96i. The above code
compiled with DMC outputs the correct value for the real part but 0.0i for
the
imaginary part. In the second printf() statement cimag() outputs the correct
value.
Can anybody give me some insight where the problem lies?

JS
Jan 13 2004
next sibling parent "Walter" <walter digitalmars.com> writes:
"John Smith" <jsmith email.com> wrote in message
news:bu1o5e$11pp$1 digitaldaemon.com...
 Consider the following C code:

 #include <stdio.h>
 #include <complex.h>

 int main(void)
 {
   double complex a = 2.0 + 0.0 * I;
   double complex b = 1.5 + .5 * I;
   double complex c = cpow(a, b);
   double complex csum = a + b;
   double r = creal(c);
   double i = cimag(c);
   printf("%f %f\n", r, i);
   printf("%f %f\n", creal(csum), cimag(csum));

   return 0;
 }

 I want to raise the complex number 2.0 +0.0i to the complex power 1.5 +
 0.5i.
 The result of this calculation is approximately 2.66 + .96i. The above

 compiled with DMC outputs the correct value for the real part but 0.0i for
 the
 imaginary part. In the second printf() statement cimag() outputs the

 value.
 Can anybody give me some insight where the problem lies?

The problem is not in creal or cimag. The issue is that cpow() returns a real number, not a complex number. Are you sure it is supposed to return 2.66+.96i, and not just 2.66?
Jan 19 2004
prev sibling next sibling parent Ejaz Ahmed <Ejaz_member pathlink.com> writes:
I want youe help in writting the expresion in c++. kindly mentioned to me how to
write this expression.
thanks 
Ejaz Ahmed
Sep 04 2005
prev sibling next sibling parent Ejaz Ahmed <Ejaz_member pathlink.com> writes:
I want youe help in writting the expresion in c++. kindly mentioned to me how to
write this expression.
thanks 
Ejaz Ahmed
Sep 04 2005
prev sibling parent reply Ejaz Ahmed <Ejaz_member pathlink.com> writes:
I want youe help in writting the expresion in c++. kindly mentioned to me how to
write this expression.
thanks 
Ejaz Ahmed
aajaz1 gmail.com
Sep 04 2005
parent Scott Michel <scottm aero.org> writes:
Ejaz Ahmed wrote:
 I want youe help in writting the expresion in c++. kindly mentioned to me how
to
 write this expression.
 thanks 
 Ejaz Ahmed
 aajaz1 gmail.com
 
 

Sorry, can't help with what's clearly a homework problem. Not unless you swing by during office hours (were I teaching next quarter at UCLA).
Sep 08 2005