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

发布于 2024-09-29 18:24:10 字数 2116 浏览 8 评论 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;
    }

好的,我正在尝试找出这里出了什么问题。

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

现在,我已经在这里检查过很多次了,并没有发现任何问题。

您可以在此处与正确的公式进行比较: alt text

这是我试图在此处复制的 x1 根。

另外,如果您想了解官方判别式,请参阅同一篇维基百科文章。是:

alt text

你们看到什么问题了吗???

OK, so I'm performing an annoying math computation here, trying to solve for one of the cubic roots.

Now, here is my C# code:

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;
    }

OK, I'm trying to figure out what's going wrong here.

First of all, since this is part of an MVC application, I have made sure my other roots are working properly, so this is purely the fault of the following computation, not a problem from anywhere else.

Now, I've checked many times here and I have not found anything wrong.

You can compare to the proper formula here:
alt text

It is the x1 root that I'm trying to replicate here.

Also, if you would like to know the official discriminant form the same Wikiepdia article here it is:

alt text

Do you guys see anything wrong???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人迟暮 2024-10-06 18:24:10

这里有一些明显的事情:

(1 / 2)

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

(1 / 2.0)

您没有向我们展示您的cubic*Value变量的声明,所以我假设它们是双精度数。

Right here is something obvious:

(1 / 2)

You are performing integer division there when you should be using a floating point number:

(1 / 2.0)

You don't show us the declaration of your cubic*Value variables, so I will assume that those are doubles.

蝶…霜飞 2024-10-06 18:24:10

这是或者应该是常见问题之一,答案是:
每个计算机科学家应该了解的浮点运算知识 (链接)

This is or should be one of frequently asked question, and the answer is:
What Every Computer Scientist Should Know About Floating-Point Arithmetic ( link )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文