Skip to content

Instantly share code, notes, and snippets.

@jmg
Last active April 13, 2016 04:41
Show Gist options
  • Save jmg/e5a79ceec0367aa8f5daf87e895c01a6 to your computer and use it in GitHub Desktop.
Save jmg/e5a79ceec0367aa8f5daf87e895c01a6 to your computer and use it in GitHub Desktop.
Quadratic Formula with complex numbers solutions
#include <stdio.h>
#include <complex>
#include <iostream>
int main()
{
double a1,b1,c1;
printf("ingrese valor de a,b,c\n");
scanf("%lf" "%lf" "%lf", &a1, &b1, &c1);
std::complex<double> a (a1,0);
std::complex<double> b (b1,0);
std::complex<double> c (c1,0);
std::complex<double> r1 (4,0);
std::complex<double> r2 (2,0);
std::complex<double> z = sqrt((b*b) - (r1*a*c));
std::complex<double> x = (-b+z)/(r2*a);
std::complex<double> y = (-b-z)/(r2*a);
if(x.imag() == 0 && y.imag() == 0 && x.real() != y.real()) {
printf("x1= %lf x2=%lf -> el polinomio tiene 2 raices reales distintas",x.real(),y.real());
}
else if(x.imag() == 0 && y.imag() == 0 && x.real() == y.real()) {
printf("%lf=%lf -> el polinomio tiene 2 raices reales iguales",x.real(),y.real());
}
else {
printf("x1= %lfi + %lfj y x2= %lfi + %lfj -> el polinomio tiene 2 raices imaginarias conjugadas",x.real(),x.imag(),y.real(),y.imag());
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment