我有这种方法来求解二次方程,它根据增量返回 2 种类型的数组(如果小于零,则有复杂的解)
public static final String[] quadEquationSolver(double a, double b, double c) {
double delta = (b * b) - 4 * a * c;
double q;
if (delta < 0) {
double deltar = Math.abs(Math.sqrt(delta));
q = ((b < 0) ? -0.5 * (b - deltar) : -0.5 * (b + deltar));
double t0 = q / a;
double t1 = c / q;
// return sorted array
return (t0 > t1) ? new String[] { t1 + "i", t0 + "i" }
: new String[] { t0 + "i", t1 + "i" };
} else {
double deltar = Math.sqrt(delta);
q = ((b < 0) ? -0.5 * (b - deltar) : -0.5 * (b + deltar));
double t0 = q / a;
double t1 = c / q;
// return sorted array
return (t0 > t1) ? new String[] { t1 + "", t0 + "" }
: new String[] { t0 + "", t1 + "" };
}
}
不幸的是,当delta<0
我得到一个尽管结果正确但只包含NaNi
值的字符串数组时,但我不知道为什么