Bhaskara beginner

3

When I compile this code, it appears: a1 vale NaN(Não é um número) and then: a2 vale NaN(Não é um número) , follow the code:

private void button1_Click(object sender, EventArgs e)
{
    int a = 2;
    int b = 3;
    int c = 5;

    double delta;
    double a1;
    double a2;

    delta = (b * b) - 4 * a * c;
    a1 = (-b + Math.Sqrt(delta)) / (2 * a);
    a2 = (-b - Math.Sqrt(delta)) / (2 * a);

    MessageBox.Show("a1 vale " + a1);
    MessageBox.Show("a2 vale " + a2);

}
    
asked by anonymous 06.03.2018 / 21:06

2 answers

3

Negative root is NAN

9-40 is -31, and this is the value of delta

Following your line of code reasoning (which is to show message with the answer) you could put the following test:

if (delta < 0) {
    MessageBox.Show("Essa função não possui zeros reais");
    return;
}
    
06.03.2018 / 21:12
1

As the result you searched in delta is a negative number you can program your function to also adapt and thus show the result taking into account the complex numbers. The calculation of a negative root is simple simply multiply it by -1 and at the end of the operation add an "i" that signals the complex number, so we get something like this:

private void button1_Click(object sender, EventArgs e)
{
    int a = 2;
    int b = 3;
    int c = 5;

    double delta;
    double a1;
    double a2;

    delta = (b * b) - 4 * a * c;

    if (delta < 0)
    {
    delta *=-1;
    a1 = (-b + Math.Sqrt(delta)) / (2 * a);
    a2 = (-b - Math.Sqrt(delta)) / (2 * a);

    MessageBox.Show("utilizando-se da propriedade dos numeros complexo obtemos que:");
    MessageBox.Show("a1 vale " + a1+"i");
    MessageBox.Show("a2 vale " + a2+"i");
    }

    else
    {
    a1 = (-b + Math.Sqrt(delta)) / (2 * a);
    a2 = (-b - Math.Sqrt(delta)) / (2 * a);

    MessageBox.Show("a1 vale " + a1);
    MessageBox.Show("a2 vale " + a2);
    }

}
    
07.03.2018 / 17:02