Here is an example how to use the complex numbers:
// I modified the code from this site:
// http://farside.ph.utexas.edu/teaching/329/lectures/node55.html
// Have fun
// Krisztian
#include <complex.h>
#include <stdio.h>
#include <iostream>
using namespace std;
/* Define complex double data type */
typedef complex<double> dcomp;
int main()
{
dcomp i, a, b, c, d, e, p, q, r; // Declare complex double variables
double x, y;
/* Set complex double variable equal to complex double constant */
i = dcomp (0.0, 1.0);
cout << "i = " << i << endl;;
// printf("\ni = (%6.4f, %6.4f)\n", i);
/* Test arithmetic operations with complex double variables */
a = i * i;
b = 1. / i;
cout << "i*i = " << a << endl;
// printf("\ni*i = (%6.4f, %6.4f)\n", a);
cout << "1/i = " << b << endl;
// printf("1/i = (%6.4f, %6.4f)\n", b);
/* Test mathematical functions using complex double variables */
c = sqrt(i);
d = sin(i);
e = pow(i, 0.25);
cout << "sqrt(i) = " << c << endl;
// printf("\nsqrt(i) = (%6.4f, %6.4f)\n", c);
cout << "sin(i) = " << d << endl;
// printf("sin(i) = (%6.4f, %6.4f)\n", d);
cout << "i^0.25 = " << e << endl;
// printf("i^0.25 = (%6.4f, %6.4f)\n", e);
/* Test complex operations */
p = conj(i);
q = real(i);
r = imag(i);
cout << "conj(i) = " << p << endl;
// printf("\nconj(i) = (%6.4f, %6.4f)\n", p);
cout << "real(i) = " << q << endl;
// printf("real(i) = %6.4f\n", q);
cout << "imag(i) = " << r << endl;
// printf("imag(i) = %6.4f\n", r);
cin.get();
return 0;
}
> Hello all,
>
> Could anyone tell me how to use the complex.h ?
>
> How to enter or declare a complex number and how to perform operation
> on complex numbers (+, / , * -, arg...) ?
>
> Thank you
>
> Chris
>
|