C# 应用程序求解二次虚根

发布于 2024-10-01 05:16:15 字数 2188 浏览 7 评论 0原文

我构建了一个极其简单、但功能齐全且非常有用的 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();
    }

txtSolution1txtSolution2是不允许接收输入的文本框,但输出计算结果

nmcA、nmcBnmcC 是 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 is 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 技术交流群。

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

发布评论

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

评论(2

煮茶煮酒煮时光 2024-10-08 05:16:28

那么您认为自己可能遇到的问题是什么?
你已经在检查想象的结果了。只需相应地计算 - 例如。执行正值的平方根,并跟踪实部和虚部。

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.

薄凉少年不暖心 2024-10-08 05:16:27

例如,您正在尝试计算

(-b + sqrt(inside)) / (2*a)

Math.Sqrt ,但它不知道虚数,因此如果 inside inside ,它会发出嘎嘎声。 0 。但我们总是可以在不改变答案的情况下乘以 1。请注意,i2 = -1。并且 -1 * i2 = 1。因此,让我们乘以 -1 * i2 并简化:

(-b + sqrt(inside * -1 * i**2)) / (2*a)
(-b + sqrt(-inside) * sqrt(i**2)) / (2*a)
(-b + sqrt(-inside) * i) / (2*a)
-b/(2*a) + sqrt(-inside)/(2*a) * i

因此,以下 C# 代码:

solution1String = (-b/(2*a)).ToString() +
                      " + " + (Math.Sqrt(-inside)/(2*a)).ToString() + " i";

e.g. you are trying to calculate

(-b + sqrt(inside)) / (2*a)

Math.Sqrt doesn't know about imaginary numbers, so it croaks if inside < 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:

(-b + sqrt(inside * -1 * i**2)) / (2*a)
(-b + sqrt(-inside) * sqrt(i**2)) / (2*a)
(-b + sqrt(-inside) * i) / (2*a)
-b/(2*a) + sqrt(-inside)/(2*a) * i

So the following C# code:

solution1String = (-b/(2*a)).ToString() +
                      " + " + (Math.Sqrt(-inside)/(2*a)).ToString() + " i";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文