+ 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.
7 ответов
+ 10
There is a complex class.
http://www.cplusplus.com/reference/complex/
+ 21
I also made a program on complex no.'s ☺
https://code.sololearn.com/c6G2Er0LS8fv/?ref=app
+ 6
Thanks all, this is the info I was looking for!
+ 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.
+ 3
It works on sololearn
https://code.sololearn.com/c0f4bUT528E1/?ref=app
here is a good documentation about it:
http://support.sas.com/documentation/onlinedoc/sasc/doc750/html/cpp/z0274855.htm
+ 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
+ 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 ^^