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

Write a program to compute the following Quadratic Equation:

本文关键字:计算 二次方程 程序 一个      更新时间:2023-10-16

我正在尝试为等式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

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