C# 数学计算无法正常工作
好的,所以我在这里执行一项烦人的数学计算,试图求解其中一个立方根。
现在,这是我的 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
根。
另外,如果您想了解官方判别式,请参阅同一篇维基百科文章。是:
你们看到什么问题了吗???
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:
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:
Do you guys see anything wrong???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有一些明显的事情:
当您应该使用浮点数时,您正在执行整数除法:
您没有向我们展示您的cubic*Value变量的声明,所以我假设它们是双精度数。
Right here is something obvious:
You are performing integer division there when you should be using a floating point number:
You don't show us the declaration of your cubic*Value variables, so I will assume that those are doubles.
这是或者应该是常见问题之一,答案是:
每个计算机科学家应该了解的浮点运算知识 (链接)
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 )