/// <summary>
/// 解一元二次方程ax2+bx+c=0
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="c"></param>
/// <param name="solution">解数组</param>
/// <returns>返回解的个数</returns>
public static int solution2equation(double a,double b,double c, double[] solution)
{
double delt = b * b - 4 * a * c;
if (delt >= 0)
{
if (a > 1e-10)
{
solution[0] = (-b + System.Math.Sqrt(delt)) / (2 * a);
solution[1] = (-b - System.Math.Sqrt(delt)) / (2 * a);
}
else
{
solution[0] = (2 * c)/(-b + System.Math.Sqrt(delt)) ;
solution[1] = (2 * c)/(-b - System.Math.Sqrt(delt)) ;
}
return 2;
}
else
{
return 0;
}
}
public static int solution2equation(double[] coefficient, double[] solution)
{
double a = coefficient[0];
double b = coefficient[1];
double c = coefficient[2];
return solution2equation(a, b, c, solution);
}
本文提供了一种解决一元二次方程的方法,通过输入方程的系数a、b、c,可以求得方程的实数解。该方法首先计算判别式,然后根据判别式的值来确定方程的解的数量。
1561





