Root and power in C#

I need to do Root and power calculation according to the logic that follows You.. so far ta funfando

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace calculadora
{
    public partial class Form1 : Form
    {
        Double value = 0;
        String operacao = "";
        bool operacao_press = false;

        public Form1()
        {
            InitializeComponent();
        }



        private void button_Click(object sender, EventArgs e)
        {
            if ((resultado.Text == "0") || (operacao_press))
                resultado.Clear();

            Button b = (Button)sender;
            resultado.Text = resultado.Text + b.Text;
        }

        private void button16_Click(object sender, EventArgs e)
        {
            resultado.Text = "0";
        }

        private void button20_Click(object sender, EventArgs e)
        {
            resultado.Text = "0";
        }

        private void operador_click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            operacao = b.Text;
            value = Double.Parse(resultado.Text);
            operacao_press = true;
        }

        private void button18_Click(object sender, EventArgs e)
        {                              
            switch (operacao) //OPERAÇÕES MATEMÁTICAS   
            {         

                case "+":
                    resultado.Text = (value + Double.Parse(resultado.Text)).ToString();
                    break;

                case "- ":
                    resultado.Text = (value - Double.Parse(resultado.Text)).ToString();
                    break;

                case "/":
                    resultado.Text = (value / Double.Parse(resultado.Text)).ToString();
                    break;

                case "*":
                    resultado.Text = (value * Double.Parse(resultado.Text)).ToString();
                    break;


                case "RAIZ":
                    resultado.Text = (value  Double.Parse(resultado.Text)).ToString();
                    break;


                case "POTENCIALIZAÇÃO":
                    resultado.Text = (value  Double.Parse(resultado.Text)).ToString();
                    break;

                default:
                    break;

            } //Final do Switch
            operacao_press = false;
        }
    }
}
Author: Jefferson Quesado, 2018-02-27

1 answers

            case "RAIZ":
                resultado.Text = Math.Sqrt(value).ToString(); // num
                break;


            case "POTENCIALIZAÇÃO":
                resultado.Text = Math.Pow(value, Double.Parse(resultado.Text)).ToString(); // num, expoente
                break;

To calculate the root of another exponent you can define the following function:

public static void cubicPairs(double n, int expoente)
{
    double root = (System.Math.Pow(n, (1 / expoente)));
    double roundedRoot = Math.Round(root);

    if (Math.Abs(roundedRoot - root) < VERY_SMALL_NUMBER)
        return roundedRoot;
    else
        return root;
}

And call it normally in your code:

            case "RAIZ":
                resultado.Text = cubicPairs(value, Double.Parse(resultado.Text)).ToString();
                break;
 2
Author: CypherPotato, 2018-02-27 04:13:46