Complex number
- C
- C++
src/c/data-structure/03-complex-number.c
// also see complex.h
#include <stdio.h>
int main()
{
_Complex float z = 4.0 + 3.0i;
printf("Real part = %f\n", __real__(z));
printf("Imaginary part = %f\n", __imag__(z));
// complex conjugate
_Complex float conj_z = ~z;
printf("Conjugate = (%f, %f)\n", __real__(conj_z), __imag__(conj_z));
return 0;
}
src/cpp/data-structure/03-complex-number.cpp
#include <iostream>
#include <complex>
using namespace std;
int main()
{
complex<double> z(2.0, 3.0); // declare complex number: 2 + 3i
// real and imaginary parts
cout << "Real part: " << real(z) << endl;
cout << "Imaginary part: " << imag(z) << endl;
// absolute value
cout << "Absolute value of " << z << " is " << abs(z) << endl;
// norm
cout << "Norm of " << z << " is " << norm(z) << endl;
// complex conjugate
cout << "Complex conjugate of " << z << " is " << conj(z) << endl;
complex<double> z2(3.0, 4.0);
cout << z << " + " << z2 << " = " << z + z2 << endl;
cout << z << " * " << z2 << " = " << z * z2 << endl;
return 0;
}