-3

Ok, so i am creating a quadratic equation solver in c++ and cant seem to get the right output on imaginary numbers. the real number roots come out fine (e.g. x= 2 and x = 5) but when the imaginary numbers come out, something weird happens where it shows (x = -1.#IND)?? someone please help me figure this out? i want it to show something more like x = 5.287*i. Here is my 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 will provide solutions for trinomial expressions.\nEXAMPLE: A*x^2 + B*x^2 + C = 0" << endl;

    double a, b, c;
    double discriminant;

    //Variable Inputs
    cout << "Enter the value of a: ";
    cin >> a;
    cout << "Enter the value of b: ";
    cin >> b;
    cout << "Enter the value of c: ";
    cin >> c;

    //Computations
    discriminant = (b*b) - (4 * a * c);
    double x1 = (((-b) + sqrt(discriminant)) / (2 * a));
    double x2 = (((-b) - sqrt(discriminant)) / (2 * a));

    //Output
    if (discriminant == 0)
    {
        cout << "The discriminant is ";
        cout << discriminant << endl;
        cout << "The equation has a single root.\n";
    }
    else if (discriminant < 0)
    {
        cout << "The discriminant is ";
        cout << discriminant << endl;
        cout << "The equation has two complex roots.\n";
        cout << "The roots of the quadratic equation are x = " << x1 << "*i, and" << x2 << "*i" << endl;
    }
    else 
    {
        cout << "The discriminant is ";
        cout << discriminant << endl;
        cout << "The equation has two real roots.\n";
    }

    //Final Root Values
    cout << "The roots of the quadratic equation are x = ";
    cout << x1;
    cout << ", ";
    cout << x2 << endl << endl;
    system("PAUSE");

    return 0;
}
  • What do you think happens when you do `sqrt(discriminant);` and `discriminant` is negative? Or better yet, have you tried to output `sqrt(-1);`?? – scohe001 Jul 16 '15 at 22:03
  • i get what you are saying, but how do i write this in a way that allows the negative sqrt to become imaginary instead? – Mathias Pettersson Jul 16 '15 at 22:12

1 Answers1

1

double does not represent complex numbers.

Instead of passing a double to sqrt:

sqrt(discriminant)

Pass a complex number to get a complex result:

sqrt(std::complex<double>(discriminant))
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • im sorry i am a newbie here haha, but where would i write std::complex in the program? – Mathias Pettersson Jul 16 '15 at 22:10
  • i did this, but it gives an error saying no stuiable conversion from "std::complex" to "double" exists – Mathias Pettersson Jul 16 '15 at 22:17
  • Are you perhaps trying to store the result in a `double`? That's what that error means. – Drew Dormann Jul 16 '15 at 22:19
  • 1
    @MathiasPettersson [complex sqrt](http://www.cplusplus.com/reference/complex/sqrt/) returns a complex number, not a double. Make sure you're not trying to store it as a double. – scohe001 Jul 16 '15 at 22:19
  • what should i store the discriminant as then? – Mathias Pettersson Jul 16 '15 at 22:22
  • okay, so i did something and it got rid of the #IND but i still dont think this is right. this is what i wrote. std::complex x1 = (((-b) + sqrt(std::complex(discriminant))) / (2 * a)); std::complex x2 = (((-b) - sqrt(std::complex(discriminant))) / (2 * a)); – Mathias Pettersson Jul 16 '15 at 22:24
  • okay nevermind lol. THANK YOU SO MUCH :D i just checked one of my teachers examples and the answer cam out right!!! thank you so much – Mathias Pettersson Jul 16 '15 at 22:26
  • 2
    @MathiasPettersson accepting this answer will let users know that you're no longer looking for help. [The same applies to your other questions!](http://stackoverflow.com/questions/29909341) – Drew Dormann Jul 16 '15 at 22:30
  • @DrewDormann and also let others with the same issue know that this has been solved! – scohe001 Jul 16 '15 at 23:00