Fractional Exponentiation in Forth

197 Views Asked by At

I'm trying to write a function that fits a value to a model.

I have a measurement from a pressure sensor and using a calibrated model I have to convert the value into the final pressure management. Doing so involves raising the measurement to a fractional power, in this case x^2.032.

I'm writing this in Mecrisp Stellaris, a dialect of Forth.

I'm a bit stuck. I understand 2.032 = 254/125, but is there a cleaner way to write things than to simply take a huge power and a huge root?

1

There are 1 best solutions below

0
On

If your language (or calculator) has square-root, then ypu can use that to compute any power. Of course if the language has a power function, it would be better (simpler, faster, more accurate) to use that.

For example to compute

pow( x, 2.032)

we first expand 2.032 as a binary fraction (for example by looking at it in floating point in hex) as

1.032 = 2 + 1/pow(2,5) + 1/pow(2,11) + 1/pow(2,12)

Thus

pow( x, 2.032) = pow(x,2) * pow( x, 1/pow(2,5)) * ...

We can compute

pow( x, 1/pow(2,5)) 

by starting with x and taking 5 square roots in succession. The general method is to loop over the binary expansion of 2.032, taking square roots, and accumulating into the answer when the binary digit is 1