[英]Problem with calculating cubic equations in C#
Recently I was working on a discord bot to compute the roots of a cubic equation.最近我正在研究一个 discord 机器人来计算三次方程的根。
I have already made another program beforehand in C# that basically does the same thing, and is proven to work correctly, so I straight up copied the codes into the one which I am working with now.我已经在 C# 中预先制作了另一个程序,它基本上做同样的事情,并且被证明可以正常工作,所以我直接将代码复制到我现在正在使用的代码中。
(Suppose that I am calculating the roots of 7x 3 + 2x 2 + 12x + 9 = 0) (假设我正在计算 7x 3 + 2x 2 + 12x + 9 = 0 的根)
The code below is from the program which was created beforehand:下面的代码来自预先创建的程序:
(Once again, the program and the results are proven to be correct , results are listed at the bottom of the code) * (再次证明程序和结果是正确的,结果列在代码底部) *
// parts 1-5
double p1 = -(b / (3 * a));
double p2 = (b * c / (6 * Math.Pow(a, 2))) - (Math.Pow(b, 3) / (27 * Math.Pow(a, 3))) - (d / (2 * a));
double p3 = (c / (3 * a)) - (Math.Pow(b, 2) / (9 * Math.Pow(a, 2)));
Complex p4 = new Complex(-1.0 / 2, Math.Sqrt(3) / 2);
Complex p5 = new Complex(-1.0 / 2, -(Math.Sqrt(3) / 2));
// solve for roots
Complex root1 = p1 + Math.Cbrt(p2 + Math.Sqrt(Math.Pow(p2, 2) + Math.Pow(p3, 3))) +
Math.Cbrt(p2 - Math.Sqrt(Math.Pow(p2, 2) + Math.Pow(p3, 3)));
Complex root2 = p1 + (p4 * Math.Cbrt(p2 + Math.Sqrt(Math.Pow(p2, 2) + Math.Pow(p3, 3)))) +
(p5 * Math.Cbrt(p2 - Math.Sqrt(Math.Pow(p2, 2) + Math.Pow(p3, 3))));
Complex root3 = p1 + (p5 * Math.Cbrt(p2 + Math.Sqrt(Math.Pow(p2, 2) + Math.Pow(p3, 3)))) +
(p4 * Math.Cbrt(p2 - Math.Sqrt(Math.Pow(p2, 2) + Math.Pow(p3, 3))));
/*
results :
root1 = (-0.6566823203779361, 0)
root2 = (0.18548401733182518, 1.3868992549529795)
root3 = (0.18548401733182518, -1.3868992549529795)
*/
I proceed to copy the whole thing into the new program I am working on.我继续将整个内容复制到我正在处理的新程序中。 Then I realized in my new program the Math.Cbrt function doesn't exist (I don't know why) .
然后我意识到在我的新程序中 Math.Cbrt function 不存在(我不知道为什么) 。 Therefore I made a custom program to tackle this problem, here is the code in my new program:
因此我制作了一个自定义程序来解决这个问题,这是我新程序中的代码:
(This is the problematic program, the results are different from the first one's.) (这是有问题的程序,结果与第一个不同。)
// parts 1-5
double p1 = -(b / (3 * a));
double p2 = (b * c / (6 * Math.Pow(a, 2))) - (Math.Pow(b, 3) / (27 * Math.Pow(a, 3))) - (d / (2 * a));
double p3 = (c / (3 * a)) - (Math.Pow(b, 2) / (9 * Math.Pow(a, 2)));
Complex p4 = new Complex(-1.0 / 2, Math.Sqrt(3.0) / 2);
Complex p5 = new Complex(-1.0 / 2, -(Math.Sqrt(3.0) / 2));
// solve for roots
Complex root1 = p1 + Cbrt(p2 + Math.Sqrt(Math.Pow(p2, 2) + Math.Pow(p3, 3))) +
Cbrt(p2 - Math.Sqrt(Math.Pow(p2, 2) + Math.Pow(p3, 3)));
Complex root2 = p1 + (p4 * Cbrt(p2 + Math.Sqrt(Math.Pow(p2, 2) + Math.Pow(p3, 3)))) +
(p5 * Cbrt(p2 - Math.Sqrt(Math.Pow(p2, 2) + Math.Pow(p3, 3))));
Complex root3 = p1 + (p5 * Cbrt(p2 + Math.Sqrt(Math.Pow(p2, 2) + Math.Pow(p3, 3)))) +
(p4 * Cbrt(p2 - Math.Sqrt(Math.Pow(p2, 2) + Math.Pow(p3, 3))));
public static Complex Cbrt(Complex value)
{
Complex result = Complex.Pow(value, 1D / 3);
return result;
}
/*
results :
root1 = (0.965490835755936, 0.936562108366076)
root2 = (0.185484017331825, -0.486224961779172)
root3 = (-1.43668913880205, -0.450337146586904)
*/
I have tried to extract different sections of the codes and run it separately but I still wasn't able to find what the cause is.我试图提取代码的不同部分并分别运行它,但我仍然无法找到原因。 If the codes are the same, why do they yield different results?
如果代码相同,为什么它们会产生不同的结果? Or is the Cbrt method I have created the real problem behind this?
还是我创建的 Cbrt 方法才是真正的问题所在?
Refering @mikuszefski 's comment,参考@mikuszefski 的评论,
The real cause behind this is that the Cbrt() function I have created calculates negative numbers wrongly.这背后的真正原因是我创建的 Cbrt() function 错误地计算了负数。
For example, Cbrt(-1)
should yield the result -1
.例如,
Cbrt(-1)
应该产生结果-1
。
However, the answer I have gotten instead is (NaN, 0)
.但是,我得到的答案是
(NaN, 0)
。
I have found the solution in another thread .我在另一个线程中找到了解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.