473,796 Members | 2,632 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(su m), cimag(sum));
printf("sum: abs = %f , angle = %f\n", cabs(sum), carg(sum));

return(0);

}
Thanks,
Reshmi
Sep 20 '08 #1
4 4592
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(su m), 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.co mwrote:
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 objektorientier ter Datenverarbeitu ng
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...@gm ail.comwrote:
On Sep 20, 4:57 am, Ian Collins <ian-n...@hotmail.co mwrote:
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 objektorientier ter Datenverarbeitu ng
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
4332
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 = form.ppi.value; var hite = form.ht.value; if (weight > 0.0) { form.totwid.value = int_zero( weight * 2 )+(pgs/ppi)+0.5;
5
3216
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
7569
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 and some who couldn't but that it wasn't important right now. And I said, 'sure, we can do that later'. So now I've developed an app without any thought to security and am trying to apply it afterwards. Doh!, doh! and triple doh!
8
5068
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 have a query that will pull a single field from all the tables. In other words i should have 44 fields. when i try to do that same, i get an error message saying "Query is too complex"
2
13190
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 parts. If first finds a natural logarithm, then converts it to a percentile. Even if I had to do it in two steps on the spreadsheet, I can't determine the calculations. *************************************************** Logarithm System The...
2
1922
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: complex a(...); complex b(...); complex c = a*b;
28
2665
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
9522
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 element, and the last element are known to be 0. I've implemented a -operator that returns a ComplexReference object that basically maps a complex<doubleinto the storage used in the array. What I would like to do is using the ComplexReference...
14
9058
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 be able to represent the number (95 ^ 63), which is approximately (2 ^ 6.6) ^ 63, which is about 2 ^ 416, so we're talking about maybe a 512-Bit number. So I need to be able to do division and multiplication on a 512-Bit number. Has anyone here...
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10455
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10173
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7547
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.