Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the Math.Pow function [duplicate]

Tags:

c#

pow

I have a constructor that returns the result of two ints with a 'to the power of' operator. I am trying to use the math.pow method to do this but im not to sure how it work.

Here is my constructor

public int Power (int firstNumber, int secondNumber)
{
    result = Math.Pow(firstNumber, secondNumber);
    return result;
}

and the code that calls it

case "^":
    result = application.Power(firstInt, secondInt);
    labelResult.Text = ("" + result);
    break;

How do i use the Math.Pow method in this situation?


1 Answers

As far as the error you're getting:

Math.Pow takes 2 doubles as arguments and returns a double. Your method is declared as returning an int. This means that result is an int. You need to cast the result of Math.Pow to int:

result = (int)Math.Pow(firstNumber, secondNumber);
like image 116
Michael Roy Avatar answered Jan 23 '26 15:01

Michael Roy