How do I calculate power-of in C#?

C#Math

C# Problem Overview


I'm not that great with maths and C# doesn't seem to provide a power-of function so I was wondering if anyone knows how I would run a calculation like this:

var dimensions = ((100*100) / (100.00^3.00));

C# Solutions


Solution 1 - C#

See [Math.Pow][1]. The function takes a value and raises it to a specified power:

Math.Pow(100.00, 3.00); // 100.00 ^ 3.00

[1]: http://msdn.microsoft.com/en-us/library/system.math.pow.aspx "System.Math.Pow"

Solution 2 - C#

You are looking for the static method Math.Pow().

Solution 3 - C#

The function you want is Math.Pow in System.Math.

Solution 4 - C#

Do not use Math.Pow

When i use

for (int i = 0; i < 10e7; i++)
{
    var x3 = x * x * x;
    var y3 = y * y * y;
}

It only takes 230 ms whereas the following takes incredible 7050 ms:

for (int i = 0; i < 10e7; i++)
{
    var x3 = Math.Pow(x, 3);
    var y3 = Math.Pow(x, 3);
}

Solution 5 - C#

Following is the code calculating power of decimal value for RaiseToPower for both -ve and +ve values.

public decimal Power(decimal number, decimal raiseToPower)
        {
            decimal result = 0;
            if (raiseToPower < 0)
            {
                raiseToPower *= -1;
                result = 1 / number;
                for (int i = 1; i < raiseToPower; i++)
                {
                    result /= number;
                }
            }
            else
            {
                result = number;
                for (int i = 0; i <= raiseToPower; i++)
                {
                    result *= number;
                }
            }
            return result;
        }

Solution 6 - C#

I'm answering this question because I don't find any answer why the ^ don't work for powers. The ^ operator in C# is an exclusive or operator shorten as XOR. The truth table of A ^ B shows that it outputs true whenever the inputs differ:

Input A Input B Output
0 0 0
0 1 1
1 0 1
1 1 0

Witch means that 100 ^ 3 does this calculation under the hood:

hex   binary
100 = 0110 0100 
  3 = 0000 0011
---   --------- ^
103 = 0110 0111

With is of course not the same as Math.Pow(100, 3) with results to one million. You can use that or one of the other existing answers.


You could also shorten your code to this that gives the same result because C# respects the order of operations.

double dimensions = 100 * 100 / Math.Pow(100, 3); // == 0.01

Solution 7 - C#

For powers of 2:

var twoToThePowerOf = 1 << yourExponent;
// eg: 1 << 12 == 4096

Solution 8 - C#

Math.Pow() returns double so nice would be to write like this:

double d = Math.Pow(100.00, 3.00);

Solution 9 - C#

static void Main(string[] args)
        {
            int i = 2;
            int n = -2;
            decimal y = 1;
            if (n > 0)
            {
                for (int x = 1; x <= n; x++)
                    y *= i;               
            }
            if (n < 0)
            {
                for (int x = -1; x >= n; x--)
                    y /= i;
            }
            Console.WriteLine(y);
        }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondavethecoderView Question on Stackoverflow
Solution 1 - C#Paul TurnerView Answer on Stackoverflow
Solution 2 - C#Daniel BrücknerView Answer on Stackoverflow
Solution 3 - C#AakashMView Answer on Stackoverflow
Solution 4 - C#Christopher AicherView Answer on Stackoverflow
Solution 5 - C#Mumtaz CheemaView Answer on Stackoverflow
Solution 6 - C#H. PauwelynView Answer on Stackoverflow
Solution 7 - C#jeromejView Answer on Stackoverflow
Solution 8 - C#prostiView Answer on Stackoverflow
Solution 9 - C#AtchayanView Answer on Stackoverflow