quadratic equation

#include <iostream>
#include <cmath>
using namespace std;
bool quadraticFormula(double a, double b, double c, double *totalResults)
{
double D = sqrt( (b*b) - (4*a*c) );//shorter name

if (!a)// a==0 => 2*a==0
{
cout<<"Denominator is zero\n";
return false; // function failed -you can also use exceptions-
}

totalResults[0] = ( -b + D)/(2*a);
totalResults[1] = ( -b - D)/(2*a);

return true;//function succeded
}

int main(void)
{
double a, b, c;
double x[2];

cout<<"Please Enter a, b, and c to get Results of x:\n";

cout<<"a: ";
cin>>a;

cout<<"b: ";
cin>>b;

cout<<"c: ";
cin>>c;

if ( quadraticFormula(a, b ,c, x) )
{
cout<<"\nYour Results are:"<<endl;
cout << x[0] << endl << x[1] << endl;
}
system("pause");
return 0;
}

Everytime I compile this program it shows me:
Your results are :
-1.#IND
-1.#IND
Any help ?
Are you guarding against (b*b) - (4*a*c) being -ve?

sqrt() can only handle +ve numbers.
Last edited on
Topic archived. No new replies allowed.