Search code examples
math.h

making your own pow()


I have a project where we are trying to get the code space down. We have one spot in one file that calls the pow() function from the math library which adds an additional +12k of code to the final hex for this one line of code. I have done some searching and I can't seem to find a good way of doing the pow function outside of the math library. Every example I have found is using the math library. The worst is I need floating point because I need to raise some unknown variable that is constantly changing to a power of 1.4 and the controller I have I was told doesn't actually have floating point. Some 72Mhz 32bit ARM device that has no floating point I am told, hence the huge space for the one library function. Has anyone else tried to do this and won the battle?


Solution

  • You can use this rule on fractional exponents to break it into an integer root and integer power function applied one after the other.

    Since the exponent is fixed, this could make your code much simpler, since 1.4 = 7/5. Now you just need to write a single function that performs these exact two steps at once on your integer input and you can avoid the losses of floating point calculations.