The complex class template in C is used to represent complex numbers. It contains two parameters, the real part and the imaginary part, which can be obtained through the methods real() and imag(). The complex class supports addition, subtraction, multiplication, and division operations, and provides norm() and arg() methods to obtain modules and arguments. In the example, two complex objects z1 and z2 are instantiated, and the use of arithmetic operations and obtaining the real and imaginary parts is demonstrated.
complex in C
#complex is a class template in the C standard library used to represent complex numbers.
Structure
complex class template contains two template parameters:
Using
To use complex, you need to instantiate the class template first:
<code class="cpp">complex<double> z1(3.0, 4.0);</code>
After instantiation, you can use the complex object. Arithmetic operations:
z1 z2
, z1 - z2
z1 * z2
、z1 / z2
z1 == z2
、z1 != z2
、z1 < z2
etcMethods
The complex class provides some methods to obtain and operate complex numbers:
real()
: Get the real part of the complex number. imag()
: Get the imaginary part of the complex number. norm()
: Get the modulus of a complex number. arg()
: Get the argument of a complex number. Example
The following example demonstrates how to use the complex class:
<code class="cpp">#include <complex> int main() { complex<double> z1(3.0, 4.0); complex<double> z2(5.0, -2.0); // 加法和减法 cout << "z1 + z2 = " << z1 + z2 << endl; cout << "z1 - z2 = " << z1 - z2 << endl; // 乘法和除法 cout << "z1 * z2 = " << z1 * z2 << endl; cout << "z1 / z2 = " << z1 / z2 << endl; // 获取实部和虚部 cout << "Real part of z1: " << z1.real() << endl; cout << "Imaginary part of z1: " << z1.imag() << endl; return 0; }</code><p>Output result: </p> <pre class="brush:php;toolbar:false"><code>z1 + z2 = (8,-2) z1 - z2 = (-2,6) z1 * z2 = (23,-26) z1 / z2 = (0.64,0.16) Real part of z1: 3 Imaginary part of z1: 4</code>
The above is the detailed content of What does complex mean in c++. For more information, please follow other related articles on the PHP Chinese website!