Square root C#

Asked

Viewed 9,722 times

1

Boas, After the tips of the users I decided to improve my C# code of the calculator. I think it’s simpler but I have a little problem in the square root because I try to do the math and the result always gives me 0.

    private void btn_raiz_Click(object sender, RoutedEventArgs e)
    {
        resutado = Convert.ToSingle(Math.Sqrt(primeiro));

        janela.Text = resutado.ToString();

    }
  • 2

    Hi Chris, just looking at this code lacks a lot of information. What is the value of primeiro? Where resultado and janela were declared, who calls this event and when? Try to create a MVCE

  • 2

    Related: http://answall.com/questions/136662/calcular-raiz-quadrada-em-c?rq=1

2 answers

1

Could be the type of variables you’re using.

protected void Button1_Click(object sender, EventArgs e)
    {
        double primeiro = 2;

        double resutado = Convert.ToSingle(Math.Sqrt(primeiro));

        janela.Text = resutado.ToString();

    }

I developed this function and it worked perfectly.

0

I don’t understand why that Convert.ToSingle.

Faster alternative(not tested):

private void btn_raiz_Click(object sender, RoutedEventArgs e)
{
    resutado = (float)Math.Sqrt(primeiro); //A variável "resultado" está declarada como float? Se não, vai dar problema...
    janela.Text = resutado.ToString();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.