Pregunta

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?

¿Fue útil?

Solución

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.

Otros consejos

Your problem is somewhat simpler than writing a generalized pow() function, since the exponent is fixed. You may also be able to relax the precision requirement a bit from a full-featured pow().

Option 1: Implement a Taylor Series expansion for f(x) = x^1.4 . You will need to pick some value of x about which to expand the Taylor series, and you'll lose precision as x gets farther away from this value.

Option 2: Construct an interpolation table by tabulating values of x^1.4 at some number of points, then use linear (or higher order polynomial) interpolation to calculate values at intermediate points.

You might get away with less code using float instead of double and powf():

float var=0.12345f;
var=powf(var,1.4f);

Naturally, the result will be less precise.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top