468,736 Members | 2,058 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,736 developers. It's quick & easy.

Complex number calculation using library

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
Sep 20 '08 #1
4 4316
Reshmi wrote:
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>

--
Ian Collins.
Sep 20 '08 #2
Reshmi wrote:
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
Sep 20 '08 #3
On Sep 20, 4:57 am, Ian Collins <ian-n...@hotmail.comwrote:
Reshmi wrote:
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>
Or, given his code, compile the program as C (and ask any
further questions about it in comp.lang.c, of course).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 20 '08 #4
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
On Sep 20, 4:25 am, James Kanze <james.ka...@gmail.comwrote:
On Sep 20, 4:57 am, Ian Collins <ian-n...@hotmail.comwrote:
Reshmi wrote:
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>

Or, given his code, compile the program as C (and ask any
further questions about it in comp.lang.c, of course).

--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 21 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Justin Caldicott | last post: by
116 posts views Thread by Mike MacSween | last post: by
2 posts views Thread by cowboyboborton | last post: by
2 posts views Thread by jraul | last post: by
28 posts views Thread by beach.dk | last post: by
1 post views Thread by perroe | last post: by
14 posts views Thread by =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.