Complex number calculation using library

R

Reshmi

Hi,

I have this C code which does complex number arithmetic. When I try to
write a similar file for C++, it says that "creal’ was not declared in
this scope". Can anyone give a better idea to write it using
"complex.h", other than using own data structures?

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

int main(){

// double pi = 4*atan2(1);

double complex A = 32 + 24*I;
double complex B = 64 + 48*I;
double complex sum = A + B;

printf("\na is %f + %fi\n",creal(A), cimag(A));
printf("b is %f + %fi\n",creal(B), cimag(B));
printf("sum is %f + %fi\n",creal(sum), cimag(sum));
printf("sum: abs = %f , angle = %f\n", cabs(sum), carg(sum));

return(0);

}


Thanks,
Reshmi
 
I

Ian Collins

Reshmi said:
Hi,

I have this C code which does complex number arithmetic. When I try to
write a similar file for C++, it says that "creal’ was not declared in
this scope". Can anyone give a better idea to write it using
"complex.h", other than using own data structures?

#include <stdio.h>
#include <complex.h>
C++ does not include C99's _Complex type.

Use std::complex from <complex>
 
K

Kai-Uwe Bux

Reshmi said:
Hi,

I have this C code which does complex number arithmetic. When I try to
write a similar file for C++, it says that "creal? was not declared in
this scope". Can anyone give a better idea to write it using
"complex.h", other than using own data structures?

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

int main(){

// double pi = 4*atan2(1);

double complex A = 32 + 24*I;
double complex B = 64 + 48*I;
double complex sum = A + B;

printf("\na is %f + %fi\n",creal(A), cimag(A));
printf("b is %f + %fi\n",creal(B), cimag(B));
printf("sum is %f + %fi\n",creal(sum), cimag(sum));
printf("sum: abs = %f , angle = %f\n", cabs(sum), carg(sum));

return(0);

}

#include <complex>
#include <iostream>

typedef std::complex< double > double_complex;

int main ( void ) {
double_complex A ( 32, 24 );
double_complex B ( 64, 48 );
double_complex sum = A + B;
std::cout << "A is " << A << '\n';
std::cout << "B is " << B << '\n';
std::cout << "sum is " << sum << '\n';
std::cout << "sum: abs = " << std::abs( sum )
<< ", angle = " << std::arg( sum ) << '\n';
}


Best

Kai-Uwe Bux
 
R

Reshmi

Hi James,

This is part of a bigger function. All other files are in C++. So, I
wanted to write this function in C++ as well.

Reshmi
 

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,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top