how do we pass a complex no. to a C++ program
the real and imaginary values are dependent on some variables which are calculated at runtime
8 2924
how do we pass a complex no. to a C++ program
the real and imaginary values are dependent on some variables which are calculated at runtime
have a look at the C++ complex number class
http://www.eng.cam.ac.uk/help/tpl/languages/C++/complex.html
http://support.sas.com/documentation/onlinedoc/sasc/doc/cplus/z0274855.htm
I found that in the standard library
a structure has been defined for complex no. and not as class as mentioned in 2nd website
so
1) do we have to create our own class?
2)is there a tutorial that can teach how to use functions in complex.h
are there any examples of it?
I found that in the standard library
a structure has been defined for complex no. and not as class as mentioned in 2nd website
so
1) do we have to create our own class?
2)is there a tutorial that can teach how to use functions in complex.h
are there any examples of it?
If you have a modern compiler <complex> should be available - what compiler are you useing?
try a small program, e.g. -
#include <iostream>
-
#include <complex>
-
using namespace std;
-
int main() {
-
complex<double> c1(0.0,1.0), c2; // complex numbers with double components
-
c2=pow(c1,2.0);
-
cout << "The square of " << c1 << " is " << c2 << endl;
-
system("pause");
-
}
-
I'm not sure what you found, but Ansi C++ does support a complex datatype. The above links do have a good description of the class. There is a minor change and you should include "complex", not "complex.h".
This means you shouldn't have to make your own complex type.
i have pgain as a variable of data type complex<double>
now i want only it's real part to be put in a double variable
how do i do that
This tutorial should give you the answers you need.
Alternatively, you could always create your own class called complex to perform calculations. All it needs is two double variables (real and imaginary) and some operator overriding.
kindly someone reply
have a look at this program which determines the roots of a cubic equation
a*x^3 + b*x^2 + c*x + d =0 -
-
#include <iostream>
-
#include <complex>
-
#include <iomanip>
-
#include <math.h>
-
-
using namespace std;
-
-
// check root value
-
complex<double> check(double a, double b, double c, double d, complex<double> x)
-
{
-
return a*x*x*x + b*x*x + c * x + d;
-
}
-
-
// determine roots of cubic equation
-
// a*x^3 + b*x^2 + c*x + d =0
-
// algorithm from http://www.1728.com/cubic2.htm
-
void cubic(double a, double b, double c, double d)
-
{
-
double x1, x2, x3, x1r, x1i=0, x2i, x3i=0;
-
cout << a << " " << b << " " << c << " d " << d << endl;
-
double f =((3*c/a) - (b*b/(a*a)))/3;
-
cout << "f " << f << endl;
-
double g=(2*b*b*b/(a*a*a)-9*(b*c/(a*a))+27*d/a)/27;
-
cout << "g " << g << endl;
-
double h=(g*g)/4+f*f*f/27;
-
cout << "h " << h << endl;
-
if(h <= 0)
-
{
-
if(f == 0 && g == 0 && h == 0)
-
// all three roots real and equal
-
x1 = x2 = x3 =-pow(d/a, 1/3.0);
-
else
-
// three real roots
-
{
-
double i=sqrt((g*g/4)-h);
-
cout << "i " << i << endl;
-
double j=pow(i, 1/3.0);
-
cout << "j " << j << endl;
-
double k=acos(-(g/(2*i)));
-
cout << "k " << k << endl;
-
double l=-j;
-
cout << "l " << l << endl;
-
double m=cos(k/3);
-
cout << "m " << m << endl;
-
double n=sqrt(3.0)*sin(k/3);
-
cout << "n " << n << endl;
-
double p=-(b/(3*a));
-
cout << "p " << p << endl;
-
x1=2*j*cos(k/3)-(b/(3*a));
-
x2=l*(m+n)+p;
-
x3=l*(m-n)+p;
-
}
-
cout << "x1 " << x1 << endl;
-
cout << "x2 " << x2 << endl;
-
cout << "x3 " <<x3 << endl;
-
}
-
else
-
// one real, two complex roots
-
{
-
double r=-(g/2)+sqrt(h);
-
cout << "r " << r << endl;
-
double s=pow(r, 1/3.0);
-
cout << "s " << s << endl;
-
double t=-(g/2)-sqrt(h);
-
cout << "t " << t << endl;
-
double u;
-
if(t<0) u=-pow(-t, 1/3.0);
-
else u=-pow(t, 1/3.0);
-
cout << "u " << u << endl;
-
x1=(s+u)-(b/(3*a));
-
cout << "x1 " << x1 << endl;
-
x2=-(s+u)/2 -(b/(3*a));
-
x2i= (s-u)*sqrt(3.0)/2;
-
cout << "x2 " << x2 << " +i " << x2i << endl;
-
x3=x2;;
-
x3i=-x2i;
-
cout << "x3 " << x3 << " +i " << x3i << endl;
-
}
-
cout << "check x1 " << check(a, b, c, d, complex<double>(x1, 0)) << endl;
-
cout << "check x2 " << check(a, b, c, d, complex<double>(x2, x3i)) << endl;
-
cout << "check x3 " << check(a, b, c, d, complex<double>(x3, x3i)) << endl;
-
}
-
-
-
int main()
-
{
-
cout << "enter a, b, c, and d ";
-
double a, b, c, d;
-
cin >> a >> b >> c >> d;
-
cubic(a, b, c, d);
-
system("pause");
-
}
-
try a=3, b=-10, c=14, d= 27 which has one real root and two complex roots -1, 2.166+i2.0749, 2.166-i2.0749
Sign in to post your reply or Sign up for a free account.
Similar topics
by: gc |
last post by:
Hi,
Why didn't the committee propose a new type for complex numbers with
integer components?
thanks,
gc
|
by: Julian V. Noble |
last post by:
Dear C Mavens,
I am writing a Computing Prescription for CiSE on complex
arithmetic. I would like to be sure that I do not include
any gaffes about C99--especially about what is in the header...
|
by: David Marsh |
last post by:
Can someone show me or point me to an example of declaring and
initializing complex numbers in C99? Also, in looking at
complex.h, I don't see any library calls for + - * or /. How are
the basic...
|
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:
|
by: jacob navia |
last post by:
The C99 standard forgot to define the printf equivalent for complex numbers
Since I am revising the lcc-win implementation of complex numbers
I decided to fill this hole with
"Z"
for...
|
by: jacob navia |
last post by:
Consider this code:
< code >
#include <complex.h>
int main(void)
{
double complex c = 0.4+2.9I;
c = ~c;
}
< end code >
|
by: void main |
last post by:
I'm rather new to complex numbers in C and was wondering, how do I
initialize a complex variable properly if the imaginary part is 0.
I tried
--------
#include <complex.h>
float complex c...
|
by: BiDi |
last post by:
I have been trying to subclass complex, but I am not able to get the
right-hand arithmetic operators working.
As shown below, if an object of my subclass 'xcomplex' is added on the
right of a...
|
by: Reshmi |
last post by:
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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...
| |