Solution of applications in C # for quadratic imaginary roots

I created an extremely simple, but fully functional and very useful WinForms C # application that solves the real roots of the 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();
    }

txtSolution1and txtSolution2- these are text fields that cannot accept input, but display calculation results

nmcA, nmcBand nmcCare the NumericUpDown elements that are used to enter the values ​​a, b, and c by the end user

Well, that’s why I was hoping to take another step and possibly solve the imaginary values. Given that I have already established conditional conditions, I will need to consider imaginary values ​​only when the discriminant is equal to 0or less 0.

, . , , i. i = sqroot(-1) i^2 = -1.

- , , ?

Googling, , # 4.0 ( .NET 4.0, , ) System.Numerics.Complex. .

+3
2

.

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

Math.Sqrt , , inside < 0. 1 . , 2= -1. -1 * 2= 1. , -1 * 2 :

(-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

, #:

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

, , , , ? . - . , , .

0

Source: https://habr.com/ru/post/1773727/


All Articles