C++ complex number multiplication -
if have (1+i)^2, answer should 2i
but if do
std::complex<double> = sqrt(1), 1 = 1; cout << pow(one+i,2) << endl;
it outputs (4,0)
you initializing i
sqrt(1)
whereas thought sqrt(-1)
. such evaluated double
expression (after -1 converted double closest matching sqrt
, see mike's comment complete sequence), according cplusplus.com generates domain error negative arguments.
instead can initialize i
as:
std::complex<double> i(0,1);
alternatively use complex number argument sqrt
described in this answer, or potatoswatter indicated in comments use 1.i
c++14 (should have compiler & standard library supports user-defined literals standard library types, part 2).
Comments
Post a Comment