Quadratic Equation-output is NAN

soo...quadratic equation solver. output is "x=nan x=nan" any help appreciated. thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
#include <cmath>
using namespace std;


//prototyping
double ans_1(double,double,double);
double ans_2(double,double,double);


int main()
{

double a,b, c;
cout<<"Quadratic Equation Solver \n";
cout<<"Enter a value for a: ";
cin>>a;
cout<<endl;
cout<<"Enter a value for b: ";
cin>>b;
cout<<endl;
cout<<"Enter a value for c: ";
cin>>c;
cout<<endl;
cout<<"X="<<ans_1(a,b,c)<<endl;
cout<<"X="<<ans_2(a,b,c)<<endl;
}


double ans_1(double a, double b, double c)
{
double ans1;
double temp_c=sqrt((b*b)-(4*a*c));
ans1=((-b+temp_c)/(2*a));
    return ans1;
}

double ans_2(double a, double b, double c)
{
    double ans2;
double temp_d=sqrt((b*b)-(4*a*c));
ans2=((-b-temp_d)/(2*a));
    return ans2;
}
On line 42: What would be the square root if the provided formula results to negative number?
See my comments in a previous thread:

http://www.cplusplus.com/forum/beginner/113700/

Topic archived. No new replies allowed.