C#数学计算无法正常工作

Qco*_*com 0 c# math polynomial-math c#-4.0

好的,所以我在这里执行一个烦人的数学计算,试图解决其中一个立方根.

现在,这是我的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;
    }
Run Code Online (Sandbox Code Playgroud)

好的,我想弄清楚这里出了什么问题.

首先,由于这是MVC应用程序的一部分,我确保我的其他根工作正常,所以这纯粹是以下计算的错误,而不是其他任何地方的问题.

现在,我在这里检查了很多次,但我没有发现任何错误.

你可以在这里比较适当的公式: 替代文字

这是x1我试图在这里复制的根源.

此外,如果您想知道相同的Wikiepdia文章的官方判别形式,它是:

替代文字

你们看到有什么不对吗???

Ed *_* S. 8

这里显而易见的是:

(1 / 2)
Run Code Online (Sandbox Code Playgroud)

当您应该使用浮点数时,您正在执行整数除法:

(1 / 2.0)
Run Code Online (Sandbox Code Playgroud)

你没有告诉我们你的立方*值变量的声明,所以我假设那些是双倍的.