#include #include #include #include "complex.h" static complex cpower(complex,int); int main(int argc, char*argv[]) { complex i ; complex z; i = complex_new(0.0,1.0); z = cmul(i,i); printf("(%f +%fi)\n",real_part(z),imag_part(z)); printf("module de i^2 : %f\n",cmodule(z)); printf("module de i : %f\n",cmodule(i)); cdelete(z); cdelete(i); { double angle = M_PI/3 ; complex j = complex_new(cos(angle),sin(angle)); z = cpower(j,23); printf("(%f,%f)\n",real_part(z),imag_part(z)); cdelete(z); cdelete(j); } return EXIT_SUCCESS; } complex cpower(complex z,int n) { complex a; complex c; int p=n; a = inject(1.0); c = cclone(z); /* invariant : cmult(a,cpower(c,p)) == cpower(z,n) */ while( p > 0) { if (p % 2 == 0) { complex tmp = c; c = cmul(c,c); cdelete(tmp); p /= 2; } else { complex tmp = a ; a = cmul(a,c); cdelete(tmp); p--; } } cdelete(c); return a; }