Imaginary Numbers -- C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 10

Imaginary Numbers -- C++

Hello all, I'm wondering if there is any support for imaginary numbers and computations using imaginary numbers in c++, or any language in general. Something like, but not exactly: 4.2*i^2+4.35*2+7.38*i -> some a+bi form Does this make sense what I'm asking? I know you could create something to do this, using concatenation of numbers and "i", but this uses i as a character, not a number. I would like to know if there is anything, built in or otherwise, that could accomplish this. Thanks for any help.

17th Feb 2018, 1:13 AM
J.G.
J.G. - avatar
7 Answers
+ 10
17th Feb 2018, 1:44 AM
John Wells
John Wells - avatar
+ 21
I also made a program on complex no.'s ☺ https://code.sololearn.com/c6G2Er0LS8fv/?ref=app
17th Feb 2018, 2:50 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 6
Thanks all, this is the info I was looking for!
17th Feb 2018, 12:37 PM
J.G.
J.G. - avatar
+ 6
Nice code, but it's a little too beefy for me. when you start adding more functions, multiplication, division, conjugates, abs value, imaginary vectors... it would be way too long and hard to do everything you want. I was really looking for a class like @John Wells said.
18th Feb 2018, 1:09 AM
J.G.
J.G. - avatar
17th Feb 2018, 9:43 AM
Alex
Alex - avatar
+ 2
nice code, but I would add/change a bit: - double real, imag: you won't have natural numbers that often when you are in the complex area - separate the print methods and make the real+imag part a return value: getReal, getImag, printComplex. Sometimes you just need 1 element (complex conjugate) also print the full value in cartesian form (5.1 +3.2i, 2 -1.6i, etc) since it's better to read
17th Feb 2018, 9:39 PM
Alex
Alex - avatar
+ 2
The last answer was meant for @ghanem. The code I posted uses the std complex header. It works on sololearn. Some outputs are kinda ugly, but this can be solved with using the real() und imag() methods (direct output in ostream cuts the + if the imaginary part is positive for example) The class John Wells mentioned can do all these things you look for. There are even methods for phase angle and magnitude to build polar form. Have fun experimenting with it ^^
18th Feb 2018, 1:26 AM
Alex
Alex - avatar