C# 应用程序求解二次虚根
我构建了一个极其简单、但功能齐全且非常有用的 WinForms C# 应用程序,用于求解二次方程的实根。
这是我当前的编程逻辑:
string noDivideByZero = "Enter an a value that isn't 0";
txtSolution1.Text = noDivideByZero;
txtSolution2.Text = noDivideByZero;
decimal aValue = nmcA.Value;
decimal bValue = nmcB.Value;
decimal cValue = nmcC.Value;
decimal solution1, solution2;
string solution1String, solution2String;
//Quadratic Formula: x = (-b +- sqrt(b^2 - 4ac)) / 2a
//Calculate discriminant
decimal insideSquareRoot = (bValue * bValue) - 4 * aValue * cValue;
if (insideSquareRoot < 0)
{
//No real solution
solution1String = "No real solutions!";
solution2String = "No real solutions!";
txtSolution1.Text = solution1String;
txtSolution2.Text = solution2String;
}
else if (insideSquareRoot == 0)
{
//One real solution
decimal sqrtOneSolution = (decimal)Math.Sqrt((double)insideSquareRoot);
solution1 = (-bValue + sqrtOneSolution) / (2 * aValue);
solution2String = "No real solution!";
txtSolution1.Text = solution1.ToString();
txtSolution2.Text = solution2String;
}
else if (insideSquareRoot > 0)
{
//Two real solutions
decimal sqrtTwoSolutions = (decimal)Math.Sqrt((double)insideSquareRoot);
solution1 = (-bValue + sqrtTwoSolutions) / (2 * aValue);
solution2 = (-bValue - sqrtTwoSolutions) / (2 * aValue);
txtSolution1.Text = solution1.ToString();
txtSolution2.Text = solution2.ToString();
}
txtSolution1
和txtSolution2
是不允许接收输入的文本框,但输出计算结果
nmcA、
nmcB
和 nmcC
是 NumericUpDown 控件,用于最终用户输入 a、b 和 c 值
OK,所以,我希望采取一步进一步,也可能求解虚值。考虑到我已经设置了条件,仅当判别式等于 0
或小于 0
时,我才需要考虑虚值。
但是,我想不出一个好的方法来解决这个问题。当人们试图求负数的平方根时,就会出现复杂的解决方案,导致 i 到处出现。 i = sqroot(-1)
和 i^2 = -1
。
有谁知道如何解决这个问题,或者是否不值得花时间?
编辑
通过更多的谷歌搜索,我发现 C# 4.0(或 .NET 4.0 我不确定是哪一个)可以在 System 中提供内置的复数支持。 Numerics.Complex
。我现在正在检查这个。
I have constructed an extremely simple, yet fully-functioning and quite helpful, WinForms C# application that solves for the real roots of a quadratic equation.
Here is my current programming logic:
string noDivideByZero = "Enter an a value that isn't 0";
txtSolution1.Text = noDivideByZero;
txtSolution2.Text = noDivideByZero;
decimal aValue = nmcA.Value;
decimal bValue = nmcB.Value;
decimal cValue = nmcC.Value;
decimal solution1, solution2;
string solution1String, solution2String;
//Quadratic Formula: x = (-b +- sqrt(b^2 - 4ac)) / 2a
//Calculate discriminant
decimal insideSquareRoot = (bValue * bValue) - 4 * aValue * cValue;
if (insideSquareRoot < 0)
{
//No real solution
solution1String = "No real solutions!";
solution2String = "No real solutions!";
txtSolution1.Text = solution1String;
txtSolution2.Text = solution2String;
}
else if (insideSquareRoot == 0)
{
//One real solution
decimal sqrtOneSolution = (decimal)Math.Sqrt((double)insideSquareRoot);
solution1 = (-bValue + sqrtOneSolution) / (2 * aValue);
solution2String = "No real solution!";
txtSolution1.Text = solution1.ToString();
txtSolution2.Text = solution2String;
}
else if (insideSquareRoot > 0)
{
//Two real solutions
decimal sqrtTwoSolutions = (decimal)Math.Sqrt((double)insideSquareRoot);
solution1 = (-bValue + sqrtTwoSolutions) / (2 * aValue);
solution2 = (-bValue - sqrtTwoSolutions) / (2 * aValue);
txtSolution1.Text = solution1.ToString();
txtSolution2.Text = solution2.ToString();
}
txtSolution1
and txtSolution2
are the text boxes which aren't allowed to receive input, but output the results of the calculation
nmcA
, nmcB
and nmcC
are NumericUpDown controls which are used for a, b and c value input by the end user
OK, so, I was hoping to take it a step further, and possibly solve for imaginary values as well. Considering I have the conditionals already set up, I would need to account for imaginary values only when the discriminant is equal to 0
or less than 0
.
However, I can't think of a good way to approach this. The complex solutions occur when one attempts to take the square root of a negative number, leading to the i
s appearing everywhere. i = sqroot(-1)
and i^2 = -1
.
Does anyone know how to approach this problem, or if it is just not worth the time?
EDIT
With a little more Googling, I have found that it is possible with C# 4.0 (or .NET 4.0 I'm not sure which) there is built-in complex number support in System.Numerics.Complex
. I'm checking this out now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么您认为自己可能遇到的问题是什么?
你已经在检查想象的结果了。只需相应地计算 - 例如。执行正值的平方根,并跟踪实部和虚部。
So what is the problem that you think you might have?
You are already checking for imaginery results. Just calculate accordingly - eg. perform the square root but of a positive value, and keep track of the real and imaginery parts.
例如,您正在尝试计算
Math.Sqrt
,但它不知道虚数,因此如果inside
inside
,它会发出嘎嘎声。 0 。但我们总是可以在不改变答案的情况下乘以 1。请注意,i2 = -1。并且 -1 * i2 = 1。因此,让我们乘以 -1 * i2 并简化:
因此,以下 C# 代码:
e.g. you are trying to calculate
Math.Sqrt
doesn't know about imaginary numbers, so it croaks ifinside < 0
. But we can always multiply by 1 without changing the answer. Note that i2 = -1. And -1 * i2 = 1. So let's multiply by -1 * i2 and simplify:So the following C# code: