472,965 Members | 2,186 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,965 software developers and data experts.

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 4535
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Schklerg | last post by:
I have this script performing a calculation on a page on my site: <script language="JavaScript"> function compute_weight(form) { var weight = form.wt.value; var pgs = form.pgs.value; var ppi =...
5
by: Justin Caldicott | last post by:
Hi There are n solutions to the nth root of any number, eg: (-8)^(1/3) = 1 + sqrt(3)i or -2 or 1 - sqrt(3)i The following code:
116
by: Mike MacSween | last post by:
S**t for brains strikes again! Why did I do that? When I met the clients and at some point they vaguely asked whether eventually would it be possible to have some people who could read the data...
8
by: Matt | last post by:
Hi all, Thank you for taking the time. I have a database with 45 tables on it. 44 tables are linked to a main table through a one to one relationship. My question is, is there no way i can...
2
by: cowboyboborton | last post by:
Looking for some help here. I've tried to solve this, but I just can't. What I need to know is what formula to use in an excel calculation to complete the following calculation. It's in two...
2
by: jraul | last post by:
Suppose you have a complex number class, and you overload conversions to double by only taking the real part. You also overload operator* to do complex multiplication. You then write: ...
28
by: beach.dk | last post by:
Hi, I'm trying to implement a simple hash algorith called rs_hash in javascript, but I cannot get a correct result. In c the code looks like this:
1
by: perroe | last post by:
Hi I have a array of complex numbers that are stored in a simple double array. This is done since the array is part of an wrapper for an external C library, and the imaginary part of the first...
14
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I need a Big Number library. I've been considering switching my project to C++ but at the moment I'm exploring the avenue of keeping it in C. What's the best Big Number library for C? I need to...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.