编写程序来计算以下二次方程:

时间:2015-03-14 07:35:59

标签: c++

我正在尝试为等式b+-b^2-4*a*c/2a执行以下程序:

#include<iostream>
#include<conio.h>
#include<cmath>
using namespace std;
int main()
{
    float x1,x2,x3,b,a,c;
    cout<<"Enter the value of \"a\",\"b\",\"c\""<<endl;
    cin>>a>>b>>c;
    x3=pow(b,2);
    x1=(-1*b+sqrt(x3-4*a*c))/2*a;
    x2=(-1*b-sqrt(x3-4*a*c))/2*a;
    cout<<"The answer for the first equation is:"<<x1<<endl;
    cout<<"The answer for the second equation is:"<<x2<<endl;
    getch();
    return 0;
}

输出:

Enter the value of "a","b","c":

2

2

2

The answer of the first equation is : NaN

The answer of the second equation is: NaN

为什么输出NaN

1 个答案:

答案 0 :(得分:2)

x3(4)-4 * a * c(4 * 2 * 2 = 16)= -12是负数,所以sqrt()返回nan