Saturday, February 12, 2011

How do you do exponentiation in C?

I tried "x = y ** e", but that didn't work.

  • use the pow function (it takes floats/doubles though).

    man pow:

       #include <math.h>
    
       double pow(double x, double y);
       float powf(float x, float y);
       long double powl(long double x, long double y);
    

    EDIT: BTW, for the special case of positive integer powers of 2, you can use bit shifting: (1 << x) will equal 2 to the power x. There are some potential gotchas with this, but generally it would be correct.

    From Evan Teran
  • To add to what Evan said: C does not have a built-in operator for exponentiation, because it is not a primitive operation for most CPUs. Thus, it's implemented as a library function.

    Also, for computing the function e^x, you can use the exp(double), expf(float), and expl(long double) functions.

    Note that you do not want to use the ^ operator, which is the bitwise exclusive OR operator.

    John Rudy : I'm just learning C, and that ^ threw me for a major loop at first. I'm beginning to "get it" now, but your reminder is very valuable for me and (I'm sure) hundreds more like me. +1!
  • or you could just write the power function, with recursion as a added bonus

    int power(int x, int y){
          if(y == 0)
            return 1;
         return (x * power(x,y-1) );
        }
    

    yes,yes i know this is less effecient space and time complexity but recursion is just more fun!!

    From Mark Lubin
  • pow only works on floating-point numbers (doubles, actually). If you want to take powers of integers, and the base isn't known to be an exponent of 2, you'll have to roll your own.

    Usually the dumb way is good enough.

    int power(int base, unsigned int exp) {
        int i, result = 1;
        for (i = 0; i < exp; i++)
            result *= base;
        return result;
     }
    

    Here's a recursive solution which takes O(log n) space and time instead of the easy O(1) space O(n) time:

    int power(int base, int exp) {
        if (exp == 0)
            return 1;
        else if (exp % 2)
            return base * power(base, exp - 1);
        else {
            int temp = power(base, exp / 2);
            return temp * temp;
        }
    }
    
    Evan Teran : it'll work fine if you cast you int to a double/float and then back to int.
    ephemient : Inefficient, though, and rounding error *will* make a difference when the result gets near INT_MAX.
    From ephemient
  • The non-recursive version of the function is not too hard - here it is for integers:

    long powi(long x, unsigned n)
    {
        long  p;
        long  r;
    
        p = x;
        r = 1.0;
        while (n > 0)
        {
            if (n % 2 == 1)
                r *= p;
            p *= p;
            n /= 2;
        }
    
        return(r);
    }
    

    (Hacked out of code for raising a double value to an integer power - had to remove the code to deal with reciprocals, for example.)

    ephemient : Yes, O(1) space O(log n) time makes this better than the recursive solution, but a little less obvious.

0 comments:

Post a Comment