Hello,

I'm writing a a program in C++ that adds/subtracts complex numbers. I'm having trouble writing the square root of -1 or "i" in a complex number.

Complex number:
realPart + imaginaryPart * i (where i is /-1) ["square root of -1"]

so far I'm thinking the code is something like this, but don't know how to go abouts to writing the square root part.

double Complex::plusEq( Complex secondComplex )
{
	double totalReal = real + secondComplex.getReal();
	double totalImag = imaginary + secondComplex.getReal();

	return totalReal + totalImag + // i; <--------- this part
}

I can't figure out how to write the commented "i".
Help please! ^^

The square root of 1 is 1, this should lead to the square root of -1 being -1, however, this is imaginary. Taking the square root of a negative number doesn't work.

i = -1

Hope this helps

So you're saying it should be the same as:

int i = -1;

totalReal + totalImag + i;

Seems strange how the square root of -1 = -1...

Well,
the square root of 4 is 2 because 2*2 is 4. So the square root of x is a number which complies to this: y*y = x.
This works for every number that is positive, however, if you try to take the square root of -1, that means you want y

y*y = x
-1 * -1 = oops, no, it's not x!
it's 1 instead of -1. That means there is no square root of -1. We just say it is -1 because we want to be able to calculate complex numbers with it.

Maybe my previous explanation was a little short, I hope this helps :) feel free to ask questions

Well,
the square root of 4 is 2 because 2*2 is 4. So the square root of x is a number which complies to this: y*y = x.
This works for every number that is positive, however, if you try to take the square root of -1, that means you want y

y*y = x
-1 * -1 = oops, no, it's not x!
it's 1 instead of -1. That means there is no square root of -1. We just say it is -1 because we want to be able to calculate complex numbers with it.

Maybe my previous explanation was a little short, I hope this helps :) feel free to ask questions

Sounds complicated lol, but I sorta get what you mean. =) So assuming a number * itself is = x
so if
2 * 2 = 4 // square root of 4 is equal to 2
-1 * -1 = doesn't work cuz all square roots are positive

so does this mean if i use

totalReal + totalImaginary * 1 // or don't even bother with the *1 since it does nothing

should give me the correct answer?

* -1
The square root of -1 is -1 we say. So it would just make your number negative.
Maybe this documentation is helpful for you too :)
http://en.wikipedia.org/wiki/Complex_number

I will take a look at wikipedia for a more mathematical answer. Its weird but my teacher's sample output has a different answer than mine even if it is negative lol. Guess theres something else wrong with my code then lol. Thank you for your help brechtjah.

If you don't mind add your code and I'll try and take a look at it :)

sqrt(-1) = 1 * i
Where i can't be defined. You can't calculate it.

So if you were to return it as a result, it'd have to be a string like this:
"<Real part> + <Imaginary part> * i", in this case it would leave "0 + 1 * i", or "1i" in short.

Perhaps useful to add a String returning it this way to your class, or have a function do it.

If you are making a complex class you should return a complex object, and it should not be the first operand. I mean given z and w two complex number, the instruction z + w should not modify z! Instead, it should be used in an expression like c = z + w , assigning the result of the sum to another complex number c.

The member you need in this class are just Real and Imaginary, that are two double variables holding real and imaginary part of the compex number itself. You don't need to bother about calculating "i" (in fact, you couldn't even if you needed to). You should just append "i" as a char next to the imaginary value when you print the complex number on screen.

Here are examples:

Complex Complex::operator+(Complex op2) {
	Complex temp;
	temp.Real = Real + op2.Real;
	temp.Imaginary = Imaginary + op2.Imaginary;
	return temp;
}

to overload the operator+, and

std::ostream& operator<<(std::ostream& lhs, Complex& obj) {
	lhs << "(";
	if(obj.Real>=0) {
		lhs << "+" << obj.Real;
	}
	else {
		lhs << obj.Real;
	}
	if(obj.Imaginary>=0) {
		lhs << " +" << obj.Imaginary << "i";
	}
	else {
		lhs << " " << obj.Imaginary << "i";
	}
	lhs << ")";
	return lhs;
}

for output purposes.

Last but not least: square root of -1 is not -1.

commented: Solver of complex numbers in C++! ^^ +1

Yeah, my bad;
i² = -1
you can't calculate i but you can use it

Well, there are two common ways you can represent complex numbers. One is a+ib where i is the root of -1, which is NOT -1. This is called the Cartesian (or rectangular) coordinate system. The second way is to provide the equivalent hypotenuse of the a and b points (i.e. sqrt(a^2 + b^2)), and the angle between the origin (be it natural origin or a secondary), and the point a+ib (i.e. atan(b/a)). Might seem overly complicated, but it's good for multiplying and dividing imaginary numbers, though rectangular is better for addition and subtraction.

commented: Knows complicated math for complex numbers lol. =) +1

If you are making a complex class you should return a complex object, and it should not be the first operand. I mean given z and w two complex number, the instruction z + w should not modify z! Instead, it should be used in an expression like c = z + w , assigning the result of the sum to another complex number c.

The member you need in this class are just Real and Imaginary, that are two double variables holding real and imaginary part of the compex number itself. You don't need to bother about calculating "i" (in fact, you couldn't even if you needed to). You should just append "i" as a char next to the imaginary value when you print the complex number on screen.

Here are examples:

Complex Complex::operator+(Complex op2) {
	Complex temp;
	temp.Real = Real + op2.Real;
	temp.Imaginary = Imaginary + op2.Imaginary;
	return temp;
}

to overload the operator+, and

std::ostream& operator<<(std::ostream& lhs, Complex& obj) {
	lhs << "(";
	if(obj.Real>=0) {
		lhs << "+" << obj.Real;
	}
	else {
		lhs << obj.Real;
	}
	if(obj.Imaginary>=0) {
		lhs << " +" << obj.Imaginary << "i";
	}
	else {
		lhs << " " << obj.Imaginary << "i";
	}
	lhs << ")";
	return lhs;
}

for output purposes.

Last but not least: square root of -1 is not -1.

And the winner is mrboolf. I managed to solve the problem with your tips. Your answer is much more accurate in regards to what I needed to do. Lemme add to ur rep. ^^

Thanks to everyone else too for your help too, I'll remember to add to ur reps too. ;p

Now the only part left is to clean my code by declaring constants and using pass by reference where I can instead of passing by value. =P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.