2

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?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • What don't you understand? Are you asking what variables are? – SLaks Nov 29 '13 at 01:23
  • 2
    Sounds like he doesn't understand what "constructor" means. Or the difference between instance and static methods. Or how to convert a double to int. Or a number to a string. Hard to guess, too many things wrong. – Hans Passant Nov 29 '13 at 01:25
  • I am trying to figure out how to do the power of two ints as the result = Math.Pow(firstNumber, secondNumber); returns error: Error 1 Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?) S:\Visual Studio 2013\Projects\Calculator\Calculator\Calculations.cs 39 22 Calculator –  Nov 29 '13 at 01:25
  • 1
    "Are you missing a cast?" (int)true. – Hans Passant Nov 29 '13 at 01:26
  • Give a bit more info on what you are struggling with if you would. The [MSDN](http://msdn.microsoft.com/en-us/library/system.math.pow(v=vs.110).aspx) page should tell you everything you need to know. [Dotnetperls](http://www.dotnetperls.com/math-pow) also gives an explanation with examples. – phil Nov 29 '13 at 01:27
  • That isn't a constructor. – SLaks Nov 29 '13 at 01:27

3 Answers3

10

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);
Michael Roy
  • 176
  • 1
  • 1
  • 7
6

Basically, it returns a specified number raised to the specified power.

The first argument contains the value you want to use

The second argument contains the power value you want to implement.

For example, if you have this

result = Math.Pow(3, 3);
return result;

It will return value 27 because it is 3 x 3 x 3.

But if I write

result = Math.Pow(1, 3);
return result;

It will return value of 1 because it is 1 x 1 x 1.

What if I write the 1st argument of 5 and 2nd argument 1?

result = Math.Pow(5, 1);
return result

It will return as 5 because it is 5 x 1.

You can figure it your own. http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_pow

UPDATED

Here's the example of the input. Use Power instead of declaring application.Power

case "^":
    result = Power(firstInt, secondInt);
    labelResult.Text = ("" + result);
    break;
Minelava
  • 221
  • 1
  • 7
  • 21
  • Thankyou, what I am trying to ask is how can i use firstInt and secondInt as the paramaters for Math.Pow instead of hard numbers –  Nov 29 '13 at 01:48
  • @ultrapulse May I know what is the input code? Is it user writing text inside the textbox? – Minelava Nov 29 '13 at 01:52
  • the input code are ints passed to Math.Pow by the user with the variable name firstInt and secondInt –  Nov 29 '13 at 02:42
  • @ultrapulse Read my updated post – Minelava Nov 29 '13 at 02:46
2

Like Minelava said, the Math.Pow method has two parameters, the first parameter indicates the number you are trying to base the result on. The second parameter is the exponent itself.

Math.Pow(3, 2) = 3 * 3 = 9

Math.Pow(2, 3) = 2 * 2 * 2 = 8

If you like, you could directly call the Math.Pow method, so you do not need Power method anymore. I think it's more simple otherwise you have another concern to have other Power method.

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

Just my two cents. Thanks.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
azisfatoni
  • 120
  • 5