What you are asking for is not simple, especially since you have this
power function that can be hard to approximate.
First, you have to figure out the range and the required precision of
each of the floating point numbers you are manipulating. You wrote that
readSensorData()
returns 7 decimals. If by that you mean that you
require a relative accuracy of 10−7, then I predict it will
be extremely hard to optimize this as a fixed point calculation. If
you can live with a much lower relative accuracy (say, around
10−3 or 10−4), then it may be worth trying
something.
I would start by writing the expression of height
as
height = f(sensval/sensinitial-1)
where
f(x) = 44330 * (1 - pow(1+x, 0.190295))
I would expect sensval
and sensinitial
to be close to one another,
which would imply that x is small. At this point you should try to
estimate the likely range for x, because the feasibility of what
follows strongly depends on it. Next step is to approximate f() by a
polynomial. A simple Taylor
expansion gives:
f(x) ≈ -x*(8435.78-x*(3415.25-x*2060.2))
You may start by trying this, just to see if it looks kind of OK or
completely hopeless. You will notice that the above approximation is
great when |x| is tiny, but it very quickly degrades as |x|
increases. This is why you don't use Taylor expansions in real
approximations, I showed it to you only to give you an idea of the
feasibility of a polynomial approximation.
To do better than Taylor, you must specify the range of x, then you
can scale that range to [−1, 1] and expand on the basis of the
Chebyshev
polynomials. And
then you could fine-tune to get the optimal
polynomial, but
it's likely to be only marginally better than the Chebyshev expansion.
If all this works with a reasonable polynomial degree (say, no more than
5), then you can start converting to integer math. If it doesn't, you
may try a different type of approximation, like interpolating over
tabulated values. If the problem is the range of x being too large,
you may instead write the power function as
ab = 2b × log2a
and write polynomial approximations to the base-2 exponential and
logarithm (the polynomials only needs to cover one octave). If you try a
rational fraction approximation, beware that divisions are slow on the
AVR platform.
If all the above looks like too complex, or too much work, then just get
a faster CPU, or try to live with your program being slow.