www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

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

↑ ↓ ← "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
→ "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
→ 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
→ 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
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
↑ ↓ → 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