Home > front end >  how could I use the power function in c/c without pow(), functions, or recursion
how could I use the power function in c/c without pow(), functions, or recursion

Time:11-06

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   1; i  ) 
    sum  = (pow(-1, i - 1)) / (5 * i - 1) * (pow(x, 5 * i - 1));
}

CodePudding user response:

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) { if (b==1) ret=a; else ret= ???; }    
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 just example on how to return value in case of more complex code inside macro (but the code is not present). Here are some (normal) ways on how to compute powers (so you still need to convert it to unrolled code which 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.

CodePudding user response:

It is a series. Replace pow() based on the previous iteration. @Bathsheba

double power_n1 = 1.0; 
double power_x5 = x*x*x*x; 
for (int i = 1; i < j   1; i  ) 
  // sum  = (pow(-1, i - 1)) / (5 * i - 1) * (pow(x, 5 * i - 1));
  sum  = power_n1 / (5 * i - 1) * power_x5;
  power_n1 = -power_n1;
  power_x5 *= x*x*x*x*x; 
}
  • Related