| | |
Square Root of -1
Thread Solved
![]() |
•
•
Join Date: Sep 2008
Posts: 39
Reputation:
Solved Threads: 0
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.
I can't figure out how to write the commented "i".
Help please! ^^
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.
C++ Syntax (Toggle Plain Text)
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! ^^
•
•
Join Date: Sep 2008
Posts: 39
Reputation:
Solved Threads: 0
So you're saying it should be the same as:
Seems strange how the square root of -1 = -1...
C++ Syntax (Toggle Plain Text)
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
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 •
•
Join Date: Sep 2008
Posts: 39
Reputation:
Solved Threads: 0
•
•
•
•
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 helpsfeel free to ask questions
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
C++ Syntax (Toggle Plain Text)
totalReal + totalImaginary * 1 // or don't even bother with the *1 since it does nothing
* -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
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
•
•
Join Date: Sep 2008
Posts: 39
Reputation:
Solved Threads: 0
•
•
•
•
* -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
•
•
Join Date: Sep 2008
Posts: 53
Reputation:
Solved Threads: 9
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.
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.
Last edited by BeyondTheEye; Nov 22nd, 2008 at 8:59 am.
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
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:
to overload the operator+, and
for output purposes.
Last but not least: square root of -1 is not -1.
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:
C++ Syntax (Toggle Plain Text)
Complex Complex::operator+(Complex op2) { Complex temp; temp.Real = Real + op2.Real; temp.Imaginary = Imaginary + op2.Imaginary; return temp; }
to overload the operator+, and
C++ Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- Square root program without sqrt or pwr (C++)
- C++ Square Root Problem (C++)
- simple problem with square root (C)
- help with square root finder (C++)
- Trying To Find The Square Root Of Each Number Of A Array (C++)
- Displaying Square root symbol in output (C)
Other Threads in the C++ Forum
- Previous Thread: Building a class in c++
- Next Thread: Genreic Double Linked List Using Templates
| Thread Tools | Search this Thread |
ace_thread api array assignment based binary bitmap borland c++ c++endcode c/c++ char class classes code coding compile connect console conversion count delete delete-line deploy desktop developer directshow dll download dynamic dynamiccharacterarray email embedded encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream information input int integer java lib linkedlist linker loop looping loops map math mathhomeworkhelp matrix memory multidimensional multiple news node output pointer problem program programming project python random read recursion reference remote reverse richedit rpg string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets





