2

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fuzzy_Bunnys
  • 193
  • 8
  • 3
    If `a ^ (m / n) == nth_root(a) ^ m` then this will help: http://rosettacode.org/wiki/Nth_root#Forth – Jerry Jeremiah May 04 '21 at 05:03
  • 3
    There is a [fixed point library](https://mecrisp-stellaris-folkdoc.sourceforge.io/_downloads/fixpt-math-lib.fs) for Mecrisp Stellaris. Then take the log of x divide by 2.032 and anti log, `123.4 log2 2.032 f/ pow2`. I can't test this but it should work. – Tls Chris May 04 '21 at 12:26
  • Fantastic, thank you both for pointing me to these examples, was unaware of them. – Fuzzy_Bunnys May 04 '21 at 21:31

1 Answers1

0

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

dmuir
  • 4,211
  • 2
  • 14
  • 12