TD1E3 Utilisation de la bibliothèque standard des complexes

This commit is contained in:
Geoffrey Frogeye 2016-01-17 19:36:01 +01:00
parent 600cc0bb6b
commit 84f95b2e59

View file

@ -1,11 +1,7 @@
/* Affiche les solutions à une équation du second degré */ /* Affiche les solutions à une équation du second degré */
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
#include <complex.h>
struct Complex {
double a; // Partie réelle
double b; // Partie imaginaire
};
int main() { int main() {
double a, b, c; double a, b, c;
@ -21,11 +17,9 @@ int main() {
} else if (d > 0) { } else if (d > 0) {
printf("x1 = %lf; x2 = %lf", (-b+sqrt(d))/2*a, (-b-sqrt(d))/2*a); printf("x1 = %lf; x2 = %lf", (-b+sqrt(d))/2*a, (-b-sqrt(d))/2*a);
} else { } else {
struct Complex z1,z2; double complex z1 = pow(b,2)/2*a + sqrt(-d)/2*a * I;
z1.a = z2.a = pow(b,2)/2*a; double complex z2 = pow(b,2)/2*a - sqrt(-d)/2*a * I;
z1.b = sqrt(-d)/2*a; printf("z1 = %lf+i%lf; z2 = %lf+i%lf\n", creal(z1), cimag(z1), creal(z2), cimag(z2));
z2.b = -sqrt(-d)/2*a;
printf("z1 = %lf+i%lf; z2 = %lf+i%lf\n", z1.a, z1.b, z2.a, z2.b);
} }
} }