1

I am trying to implement fixed point C code for power function i.e. without using math.h library

For example % base = linspace(-10,10,4) % exp = linspace(-5,5,4)

I want to implement fixed point power() in C. Below is the pseudo code

#include <stdio.h>
#include <stdint.h>
void main()
{
  int i,j;
  double base[4] = [-10,-3.33333333333333,3.33333333333333,10];
  double exp[4] = [-5,-1.66666666666667,1.66666666666667,5];
  for (i =0; i < 4;i++)
  {
     for(j = 0;j < 4;j++)
     {
         output(i,j) =  power(base[i],exp[j]);
     }
  }
}

Thank you!!

Coder
  • 27
  • 7
  • Implementing `pow(a, b)` using logarithms is easy: it's basically `exp(log(a) * b)`. But of course that means you need `exp()`, as in [your other question](https://stackoverflow.com/questions/70958051/derive-exponential-function-using-log-in-c). – Steve Summit Feb 02 '22 at 23:07
  • You can't assign to a function call in C. Is `output` supposed to be an array? – Barmar Feb 02 '22 at 23:30
  • `exp` is an array, not a function. So it should be `exp[j]` – Barmar Feb 02 '22 at 23:31
  • Are you a Fortran programmer by any chance? – Barmar Feb 02 '22 at 23:31
  • @Barmar Looks like Matlab – EOF Feb 02 '22 at 23:36
  • He did say it was pseudocode. – Steve Summit Feb 03 '22 at 00:39
  • @SteveSummit, yes,I need exp() as well. – Coder Feb 03 '22 at 05:04
  • What do you mean by *fixed point power*? How do you define `(-10)^(-5)` ? – Damien Feb 03 '22 at 06:46
  • you define -10 and -5 as Q5.27 and Q4.28 . Please have a look , for -5 value in a 32 bit DSP where 32 is word size contains integer and fractional bits. I am not allowed to stored the data in double or float . -5 real world value will be -1342177280 and with respect to Q4.28 format , one can do -1342177280 * 2 ^ -28 to get back -5. I hope it is clear. Similarly I can do for -10 – Coder Feb 03 '22 at 07:12
  • The calculated value for power(-10,-5) is -0.0000100000000000 – Coder Feb 03 '22 at 07:18

0 Answers0