【发布时间】:2010-10-30 23:35:42
【问题描述】:
好的,所以我在这里执行了一个烦人的数学计算,试图求解其中一个立方根。
现在,这是我的 C# 代码:
public void CubeCalculate()
{
//Calculate discriminant
double insideSquareRoot = (18 * cubicAValue * cubicBValue * cubicCValue * cubicDValue) + (-4 * (Math.Pow(cubicBValue, 3) * cubicDValue) + (Math.Pow(cubicBValue, 2) * Math.Pow(cubicCValue, 2)) + (-4 * cubicAValue * Math.Pow(cubicCValue, 3)) + (-27 * Math.Pow(cubicAValue, 2) * Math.Pow(cubicDValue, 2)));
if (insideSquareRoot < 0)
{
//One real solution, two imaginary
double onecuberootradical1 = (1 / 2) * (((2 * Math.Pow(cubicBValue, 3)) + (-9 * cubicAValue * cubicBValue * cubicCValue) + (27 * Math.Pow(cubicAValue, 2) * cubicDValue)) + (Math.Sqrt(Math.Pow((2 * Math.Pow(cubicBValue, 3)) + (-9 * cubicAValue * cubicBValue * cubicCValue) + (27 * Math.Pow(cubicAValue, 2) * cubicDValue), 2) + (-4 * Math.Pow(Math.Pow(cubicBValue, 2) + (-3 * cubicAValue * cubicCValue), 3)))));
double onecuberootradical2 = (1 / 2) * (((2 * Math.Pow(cubicBValue, 3)) + (-9 * cubicAValue * cubicBValue * cubicCValue) + (27 * Math.Pow(cubicAValue, 2) * cubicDValue)) - (Math.Sqrt(Math.Pow((2 * Math.Pow(cubicBValue, 3)) + (-9 * cubicAValue * cubicBValue * cubicCValue) + (27 * Math.Pow(cubicAValue, 2) * cubicDValue), 2) + (-4 * Math.Pow(Math.Pow(cubicBValue, 2) + (-3 * cubicAValue * cubicCValue), 3)))));
x1 = (-cubicBValue / (3 * cubicAValue)) + ((-1 / (3 * cubicAValue)) * (Math.Pow(onecuberootradical1, 1 / 3))) + (-1 / (3 * cubicAValue) * Math.Pow(onecuberootradical2, 1 / 3));
x2 = double.NaN;
x3 = double.NaN;
}
好的,我正在尝试找出这里出了什么问题。
首先,由于这是 MVC 应用程序的一部分,我确保我的其他根工作正常,所以这纯粹是以下计算的错误,而不是其他任何地方的问题。
现在,我在这里检查了很多次,没有发现任何问题。
您可以在此处与正确的公式进行比较:
这是我试图在此处复制的 x1 根。
另外,如果你想知道same Wikiepdia article 的官方判别式,这里是:
你们有什么不对吗???
【问题讨论】:
标签: c# math c#-4.0 polynomial-math