math - c++ quadratic equation code output error -


ok, creating quadratic equation solver in c++ , cant seem right output on imaginary numbers. real number roots come out fine (e.g. x= 2 , x = 5) when imaginary numbers come out, weird happens shows (x = -1.#ind)?? please me figure out? want show more x = 5.287*i. here code

#include <iostream> #include <string> #include <cmath> #include <complex> using namespace std; int main() {      cout << "project 4 (quadratic equation)\nlong beach city college \nauthor: mathias pettersson \njuly 15, 2015\n" << endl;     cout << "this program provide solutions trinomial expressions.\nexample: a*x^2 + b*x^2 + c = 0" << endl;      double a, b, c;     double discriminant;      //variable inputs     cout << "enter value of a: ";     cin >> a;     cout << "enter value of b: ";     cin >> b;     cout << "enter value of c: ";     cin >> c;      //computations     discriminant = (b*b) - (4 * * c);     double x1 = (((-b) + sqrt(discriminant)) / (2 * a));     double x2 = (((-b) - sqrt(discriminant)) / (2 * a));      //output     if (discriminant == 0)     {         cout << "the discriminant ";         cout << discriminant << endl;         cout << "the equation has single root.\n";     }     else if (discriminant < 0)     {         cout << "the discriminant ";         cout << discriminant << endl;         cout << "the equation has 2 complex roots.\n";         cout << "the roots of quadratic equation x = " << x1 << "*i, and" << x2 << "*i" << endl;     }     else      {         cout << "the discriminant ";         cout << discriminant << endl;         cout << "the equation has 2 real roots.\n";     }      //final root values     cout << "the roots of quadratic equation x = ";     cout << x1;     cout << ", ";     cout << x2 << endl << endl;     system("pause");      return 0; } 

double not represent complex numbers.

instead of passing double sqrt:

sqrt(discriminant) 

pass complex number complex result:

sqrt(std::complex<double>(discriminant)) 

Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -