[英]Negative square root
How do you take the square root of a negative number in C++?你如何在 C++ 中取负数的平方根?
I know it should return a real and a complex part, I get a NaN?我知道它应该返回一个真实而复杂的部分,我得到一个 NaN?
How do I take the real part?我如何采取真正的部分?
#include <complex>
int main()
{
std::complex<double> two_i = std::sqrt(std::complex<double>(-4));
}
or just要不就
std::complex<double> sqrt_minus_x(0, std::sqrt(std::abs(x)));
sqrt(-x)
where x is a positive number is simply 0 + sqrt(x)*i
. sqrt(-x)
其中 x 是一个正数就是0 + sqrt(x)*i
。 The real part is just 0.实部只有 0。
In general, the real part is x > 0? sqrt(x): 0
一般来说,实部是
x > 0? sqrt(x): 0
x > 0? sqrt(x): 0
and the imaginary part is x < 0? sqrt(x): 0
x > 0? sqrt(x): 0
虚部是x < 0? sqrt(x): 0
x < 0? sqrt(x): 0
. x < 0? sqrt(x): 0
。
If what you call a negative number is a real, then the real part of its square root should just be 0
?如果你所说的负数是实数,那么它的平方根的实部应该只是
0
?
Maybe something like this也许是这样的
double negativeNumber = -321;
std::complex<double> number( negativeNumber, 0 );
std::complex<double> result = sqrt( number );
double realpart = result.real();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.