64

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));
Paul Turner
  • 38,949
  • 15
  • 102
  • 166
davethecoder
  • 3,856
  • 4
  • 35
  • 66

9 Answers9

95

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

Math.Pow(100.00, 3.00); // 100.00 ^ 3.00
Paul Turner
  • 38,949
  • 15
  • 102
  • 166
15

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

Gerhard
  • 6,850
  • 8
  • 51
  • 81
Daniel Brückner
  • 59,031
  • 16
  • 99
  • 143
6

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

AakashM
  • 62,551
  • 17
  • 151
  • 186
6

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:

decimal   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
H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
4

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);
}
  • Good, we all expected Math.Pow to be slower. But not that much. Why? Now let's make homework and check what's the **compiler generated code** (both cs and assembly after JIT) and you might be surprised. Try... – Adriano Repetti Jun 04 '17 at 13:42
  • I was going to rework this one https://github.com/THEjoezack/ColorMine/tree/master/ColorMine/ColorSpaces because there is something like `x*x*x` where I though I could use `x^3`, what I couldnt, so I tried `Math.Pow` In the Reference Source of math.cs I found this one `public static extern double Pow(double x, double y);` So I was not able to look into the code, what did I wrong? – Christopher Aicher Jun 04 '17 at 13:47
  • What should I find out there? – Christopher Aicher Jun 04 '17 at 14:08
  • 4
    Your version is faster because your version only uses integers, how would you do `x^x` for example? – zedling Oct 10 '17 at 17:19
  • 1
    @zedling are you sure that is the reason? I just tried it where x is a double with many decimal places and got the same results! (231ms vs 4870ms): https://dotnetfiddle.net/jgNDfm – Ben Aug 22 '19 at 14:40
  • Nope, and after some searching I've found this: https://stackoverflow.com/a/936548/4308297 – zedling Aug 27 '19 at 10:46
3

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;
        }
2

For powers of 2:

var twoToThePowerOf = 1 << yourExponent;
// eg: 1 << 12 == 4096
jeromej
  • 10,508
  • 2
  • 43
  • 62
0

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

double d = Math.Pow(100.00, 3.00);
prosti
  • 42,291
  • 14
  • 186
  • 151
0
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);
        }