简体   繁体   English

C ++复数乘法

[英]C++ complex number multiplication

If I have (1+i)^2, the answer should be 2i 如果我有(1 + i)^ 2,答案应该是2i

But if I do 但是如果我这样做

std::complex<double> i = sqrt(1), one = 1;
cout << pow(one+i,2) << endl;

It outputs (4,0) 输出(4,0)

you are initializing i to sqrt(1) whereas you probably thought about sqrt(-1) . 您正在将i初始化为sqrt(1)而您可能考虑过sqrt(-1) As such it would be evaluated as a double expression (after -1 is converted to double as the closest matching sqrt , see Mike's comment for complete sequence), which according to cplusplus.com generates a domain error for negative arguments. 这样,它将被评估为double 精度表达式(将-1转换为最接近的匹配sqrt double后,请参见Mike的注释以获取完整序列),根据cplusplus.com ,该表达式将为负参数生成域错误。

Instead you can initialize i as: 相反,您可以将i初始化为:

std::complex<double> i(0,1);

Alternatively you could use a complex number as argument to sqrt as described in this answer , or as Potatoswatter indicated in comments you could use 1.i with C++14 (should you have a compiler & standard library that supports user-defined literals for standard library types, part 2 ). 或者,您可以按照此答案中的说明使用复数作为sqrt参数,或者如注释中指示的1.i ,可以将1.i与C ++ 14一起使用(如果您有支持用户定义文字的编译器和标准库, 标准库类型,第2部分 )。

The math.h header in C++ provides the following overloads of sqrt : C ++中的math.h标头提供以下sqrt重载:

auto sqrt( float arg ) -> float;
auto sqrt( double arg ) -> double;
auto sqrt( long double arg ) -> long double;
auto sqrt( Integral arg ) -> double;    // C++11 and later

where Integral denotes any integral type (this is a set of overloads or a function template). 其中Integral表示任何整数类型(这是一组重载或函数模板)。

The <complex> header additionally defines this overload: <complex>标头还定义了此重载:

template< class T >
auto sqrt( complex<T> const& x ) -> complex<T>;

There's also an overload for valarray , but it's not relevant here. valarray也有一个重载,但这与这里无关。

When you use -1 as actual argument, like 当您使用-1作为实际参数时,例如

sqrt( -1 )

the direct match of the argument type, the overload with Integral argument, is best fit, and that overload returns a double . 参数类型的直接匹配(带有Integral参数的重载)是最合适的,并且该重载返回double

There is no way to represent the mathematical i as a double value. 无法将数学i表示为double值。 So the value that you get is an implementation-defined value (if your C++ implementation's double type supports NaN, Not a Number , it can be a NaN); 因此,您获得的值是实现定义的值(如果您的C ++实现的double类型支持NaN,而不是Number ,则可以是NaN); “whether the integer expression errno acquires the value EDOM is implementation-defined” according to C99 §7.12.1/2. 根据C99§7.12.1/ 2,“整数表达式errno是否获取值EDOM是实现定义的”。 To avoid that you can make sure that the -1 is converted to an argument of type complex<double> , say: 为避免这种情况,可以确保将-1转换为complex<double>类型的参数,例如:

sqrt( complex<double>( -1 ) )

With the compilers I tried this yields the same result as writing 我尝试使用编译器产生与编写相同的结果

complex<double>( 0, 1 )

which represents the mathematical i , the square root of minus 1. 它代表数学值i ,即负1的平方根。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM