# Thread: Real and Imaginary numbers

1. ## Real and Imaginary numbers

ok, i made a program that adds 2 sets of real and imaginary numbers

like so

(4, 3.5i) + (5, 2.6i) = (9 , 6.1i)

ok
i got all the functions working and everything. the output is the problem, i have all the numbers set to doubles, and it prints out

-1.07374e+008

obviously this is wrong once again i set ALL the variables to double

any help is greatly apreciated. 2. are you adding the i's together. that is probably your problem. enter it as string, and parse the string... 3. That's because the quadrahedron of the double duplex modifier is conflicting with the simple semigon convertor. Fix that and everything will be sweet.

or post some code. 4. ## holy ####...

come on joe try to keep this english ok

ill post some code

here is the function page ( i split this up into 3 differnt sorces)
[CODE]

#include <iostream>
using namespace std;

#include "COMPLEX.H"

Complex::Complex(int r, double i, int re, double im)
{
r = areal;
i = aimaginary;
re = breal;
im = bimaginary;
}

Complex::~Complex()
{

}

{
int b;
b = areal + breal;
}

void Complex::SubR(int areal,int breal)
{
int s;

s = areal - breal;
}

void Complex::AddI( double aimaginary, double bimaginary)
{
float l;

l = aimaginary + bimaginary;

}
void Complex::SubI( double aimaginary, double bimaginary)
{
float n;
n = aimaginary - bimaginary;

}

void Complex ::displayA()
{
cout<<"("<<b<<","<<l<<"i )"<<endl;
}

void Complex ::disS()
{
cout<<"("<<s<<","<<n<<"i )"<<endl;
}

[\CODE]

[CODE]

#ifndef COMPLEX_H
#define COMPLEX_H

class Complex
{
int real;
double imaginary;
int sumreal;
double sumimaginary;
int areal;
int breal;
double aimaginary;
double bimaginary;
int b;
float l;
int s;
float n;

public:
Complex(int , double ,int ,double);

~Complex();
void SubR(int,int);
void SubI(double,double);

void displayA();
void disS();
};

#endif

[\CODE]

and finally the actually running prgram.

Code:
```#include <iostream>
using namespace std;

#include "COMPLEX.H"

int main()
{

cout<<"This program will add and subtract\n two predefined sets of real and imaginary numbers\n";

cout<<"(12 , 13.4i) + ( 13, 32.4i)\n\n\n";

cout<<"\n";
class Complex b(12.3, 13.4, 12.4 ,32.4);
b.displayA();
cout<<"\n\n\n";
cout<<"(12 , 13.4i) - ( 12 , 32.4i)\n\n\n";

b.disS();

return 0;

}```

i forgot to mention, this is off windows home xp and msvc++6.0 5. >>That's because the quadrahedron of the double duplex modifier is conflicting with the simple semigon convertor.
No no no, the overloaded discombobulator is toggling the enlistment flag too early causing the exponents to miss the bus pass for late binding conciliation. How could you possibly think that a conflict of the double duplex modifier and simple semigon convertor could happen? It's protected by the duplex convertor buffer flag, if the flag is set to NOTLD then the convertor zombies are rancid and have to be reaped before they eat the convertor's brains. So conversions don't even take place :-) 6. I started going through your code but complex numbers seem to be a bit more complex than I remember them. Why not just have one variable for the real part, and one for the imaginary part? Accept one real and one imaginary parameter in your add/subtract functions, and add and subtract these from your member variables. Unless I've completely missed the point; in which case ignore me and wait until someone more helpful comes along.

Oh, and ignore Cela because he/she/it is just making stuff up to try and impress the newbies. I mean, who ever heard of a buss pass? WTF is that all about? 7. the point is , to add them like this

(12 , 34.5i) + (2, 32.7i) = (14, 67.2i)

and why on earth would i get such a crazy output?

shhesh c++ is SO unpredctable  8. >>I mean, who ever heard of a buss pass? WTF is that all about?
Don't come crying to me when unreaped zombies start eating your brains :-)

>>and why on earth would i get such a crazy output?
Because you're not initializing your variables, they have nothing but garbage in them. The following code fixes all but the final call to disS(), the problem with that one is that you never call a function which sets s or n :-)
Code:
```#include <iostream>
using namespace std;

class Complex
{
int real;
double imaginary;
int sumreal;
double sumimaginary;
int areal;
int breal;
double aimaginary;
double bimaginary;
int b;
float l;
int  s;
float n;
public:
Complex(int , double ,int  ,double);

void SubR(int,int);
void SubI(double,double);

void displayA();
void disS();
};

Complex::Complex(int r, double i, int re, double im)
{
areal = r;
aimaginary = i;
breal = re;
bimaginary = im;
}

{
b =  areal + breal;
}

void Complex::SubR(int areal,int breal)
{
s = areal - breal;
}

void Complex::AddI( double aimaginary, double bimaginary)
{
l = aimaginary + bimaginary;
}
void Complex::SubI( double aimaginary, double bimaginary)
{
n = aimaginary - bimaginary;
}

void Complex ::displayA()
{
cout<<"("<<b<<","<<l<<"i  )"<<endl;
}

void Complex ::disS()
{
cout<<"("<<s<<","<<n<<"i  )"<<endl;
}

int main()
{
cout<<"This program will add and subtract\n"
"two predefined sets of real and imaginary numbers\n";

cout<<"(12 , 13.4i) + ( 13, 32.4i)\n\n\n";

cout<<"\n";
Complex b(12.3, 13.4, 12.4 ,32.4);
b.displayA();
cout<<"\n\n\n";
cout<<"(12 , 13.4i) - ( 12 , 32.4i)\n\n\n";

b.disS();

return 0;
}``` 9. hmmm doesnt this declare s and n?

Code:
```void Complex::SubI( double aimaginary, double bimaginary)
{
n = aimaginary - bimaginary;
}

// and the s

void Complex::SubR(int areal,int breal)
{
s = areal - breal;
}``` 10. >>hmmm doesnt this declare s and n?
Yes it does, but you never call those methods anywhere, so the effect is the same as not even having them :-) 11. im calling those methods in disS and in the imaginary subtraction and addition numbers see Code:
```void Complex ::disS()
{
cout<<"("<<s<<","<<n<<"i  )"<<endl;
}

// doesnt that call them?

void Complex::SubI( double aimaginary, double bimaginary)
{
n = aimaginary - bimaginary;
}

void Complex::SubR(int areal,int breal)
{
s = areal - breal;
}

// doesnt that use them?``` 12. >>im calling those methods in disS and in the imaginary subtraction and addition numbers see
Nope, you're using s and n in disS(), but since you don't actually call the methods SubR and SubI to give values to s and n, disS() prints garbage. Simply defining a class method doesn't call it. I think you're mistaking your use of the variables with the method call that initializes those variables :-) 13. ## omg,....

ROFL!!!! I MADE A NOOB COPY AND PASTE MISTAKE OMG i spen t almost all night trying to figure out new code to fix this, and alas, it was all a noobie copy and paste error..... this is why i love c++  14. Do you know about the built in STL complex template objects? Why build your own?
Code:
```#include <iostream>
#include <complex>
using namespace std;

int main()
{
complex<float> c1(4.5,3.1);                    // Real part = 4.5, imaginary = 3.1
complex<float> c2(2.0,1.0);                    // Real part = 2.0, imaginary = 1.0

cout << "c1: " << c1 << endl;                  // Outputs "c1: (4.5,3.1)"

cout << "c1 + c2: " << c1+c2 << endl;          // Outputs "c1 + c2: (6.5,4.1)"

cout << "c1 real part: " << c1.real() << endl; // Outputs "c1 real part: 4.5"
cout << "c1 imag part: " << c1.imag() << endl; // Outputs "c1 imag part: 3.1"

}```
You can do other stuff with them. There are member functions for calculating the magnitude, squared magnitude, and phase angle if you want to use them with polar coordinates. There is built in support for you to multiply them, add, divide, etc. with other vectors and scalar values. You can use these complex objects in arguments to the pow, sqrt, sin, cos, tan, sinh, cosh, tanh functions. Popular pages Recent additions 