C/C++ Math – Use Power Function Without pow(), Functions, or Recursion

c++exponentmathpow

I'm using a C++ compiler but writing code in C (if that helps)

There's a series of numbers

(-1^(a-1)/2a-1)B^(2a-1)

A and X are user defined… A must be positive, but X can be anything (+,-)…

to decode this sequence… I need use exponents/powers, but was given some restrictions… I can't make another function, use recursion, or pow() (among other advanced math functions that come with cmath or math.h).

There were plenty of similar questions, but many answers have used functions and recursion which aren't directly relevant to this question.

This is the code that works perfectly with pow(), I spent a lot of time trying to modify it to replace pow() with my own code, but nothing seems to be working… mainly getting wrong results. X and J are user inputted variables

for (int i = 1; i < j; i++) 
    sum += (pow(-1, i - 1)) / (5 * i - 1) * (pow(x, 5 * i - 1));
}

Best Answer

You can use macros to get away with no function calls restriction as macros will generate inline code which is technically not a function call

however in case of more complex operations macro can not have return value so you need to use some local variable for the result (in case of more than single expression) like:

int ret;
#define my_pow_notemp(a,b) (b==0)?1:(b==1)?a:(b==2)?a*a:(b==3)?a*a*a:0
#define my_pow(a,b)\
    {\
    ret=1;\
    if (int(b& 1)) ret*=a;\
    if (int(b& 2)) ret*=a*a;\
    if (int(b& 4)) ret*=a*a*a*a;\
    if (int(b& 8)) ret*=a*a*a*a*a*a*a*a;\
    if (int(b&16)) ret*=a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a;\
    if (int(b&32)) ret*=a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a;\
    }
void main()
   {
   int a=2,b=3,c;
   c=my_pow_notemp(a,b); // c = a^b
   my_pow(a,b); c = ret; // c = a^b
   }

as you can see you can use my_pow_notemp directly but the code is hardcoded so only up to a^3 if you want more you have to add it to code. The my_pow is accepting exponents up to a^63 and its also an example on how to return value in case of more complex code inside macro. Here are some (normal) ways on how to compute powers in case you need non integer or negative exponents (but to convert it to unrolled code will be insanely hard without loops/recursion):

In case you want to get away with recursion and function calls you can use templates instead of macros but that is limited to C++.

template<class T> T my_pow(T a,T b)
    {
    if (b==0) return 1;
    if (b==1) return a;
    return a*my_pow(a,b-1);
    }
void main()
   {
   int a=2,b=3,c;
   c=my_pow(a,b);
   }

As you can see templates have return value so no problem even with more complex code (more than single expression).

To avoid loops you can use LUT tables

int my_pow[4][4]=
    {
    {1,0,0,0},  // 0^
    {1,1,1,1},  // 1^
    {1,2,4,8},  // 2^
    {1,3,9,27}, // 3^
    };
void main()
   {
   int a=2,b=3,c;
   c=my_pow[a][b];
   }

If you have access to FPU or advanced math assembly you can use that as asm instruction is not a function call. FPU usually have log,exp,pow functions natively. This however limits the code to specific instruction set !!!

Here some examples:

So when I consider your limitation I think the best way is:

#define my_pow(a,b) (b==0)?1:(b==1)?a:(b==2)?a*a:(b==3)?a*a*a:0
void main()
   {
   int a=2,b=3,c;
   c=my_pow(a,b); // c = a^b
   }

Which will work on int exponents b up to 3 (if you want more just add (b==4)?a*a*a*a: ... :0) and both int and float bases a. If you need much bigger exponent use the complicated version with local temp variable for returning result.

[Edit1] ultimative single expression macro with power by squaring up to a^15

#define my_pow(a,b) (1* (int(b&1))?a:1* (int(b&2))?a*a:1* (int(b&4))?a*a*a*a:1* (int(b&8))?a*a*a*a*a*a*a*a:1)
void main()
   {
   int a=2,b=3,c;
   c=my_pow(a,b); // c = a^b
   }

In case you want more than a^15 just add sub term (int(b&16))?a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a:1 and so on for each bit of exponent.

Related Question