Share via


Math.pow with decimals

Question

Tuesday, January 27, 2015 9:43 PM

Hello

I need to a create function:

public static decimal Pow(
    decimal x,
    decimal y
)

But I don't find how do it...

All replies (3)

Tuesday, January 27, 2015 11:55 PM

@neoaguil17

Here is the definition for System.Math.Pow

public static double Pow( // https://msdn.microsoft.com/en-us/library/system.math.pow(v=vs.110).aspx
    double x,
    double y
)

You could piggy back on the above; keep in mind that exceptions are possible.

public static decimal DecimalPower(decimal x,  decimal y)
{
    Double X = (double)x;
    Double Y = (double)y;
    return (decimal)System.Math.Pow(X, Y);
}

edit:      example

void Main()
{
    Decimal p = (Decimal)Math.Sqrt(2.0);
    Console.WriteLine(p);
    Decimal y = 2.0M;
    Console.WriteLine(DecimalPower(p,y));
}

output:

1.4142135623731
2.00000000000001

end edit.


Monday, February 2, 2015 9:57 PM

But I don't find how do it...

1) What is it that you do not know how to do?

2) Do you know how to write a method that returns a result?

3) Can you write a method that adds two numbers?

4) Is this an exercise from your text book or an assignment from your tutor?

5) Have you done the previous exercises?


Wednesday, February 4, 2015 1:26 PM

@neoaguil17

Archer Wang had marked LINK as the answer to your O.P.; you chose to unmarked it.

neoaguil17, please explain why you are not satisfied with LINK so that your peers here at forums.asp.net can fine tune your answer.

P.S.:  please read http://weblogs.asp.net/gerrylowry/clarity-is-important-both-in-question-and-in-answer