How to calculate an arbitrary power/root?

╄→尐↘猪︶ㄣ 提交于 2019-11-29 07:21:15

Is your range of inputs arbitrary, or known within a certain range?

in either case, xm = exp(m log x), so if you can create functions to evaluate exp(x) and log(x) and have a multiply, you're probably all set.

You'll have to figure out how you want to handle nonpositive values of x.

(hint for log(x): if this is IEEE-754 floating point, shift the significand if necessary until you end up with a range of numbers between 2k and 2k+1 for some value K. This lets you deal with a 2:1 range which isn't too hard to approximate with polynomials. Then you just have a small number of possibilities to handle for the exponent and the shift number.

Corresponding hint for exp(x): write x = k+b where 0 <= b < 1 and k is an integer. Then exp(x) = exp(k)*exp(b); b has limited range and k has a limited number of discrete possibilities.)

(hint #2: the numbers probably work out better for xm = g(m f(x)) where f(x) = log2x and g(x) = 2x.)

As Jason S said, this is done using the identity xm = exp(m log x). In practice though, you will have to deal with truncation errors. I think that the way this is usually done is

  1. Use this identity: xm = (2n * x / 2n)m = 2nm * (x/2n)m and find an integer n such that 1 <= x/2n < 2.
  2. Calculate t = log2(x / 2n). This can be done either using a sufficiently high degree Taylor expansion, or with good ol' Newton-Raphson. You have to make sure that the maximum error over the interval [1, 2[ is not too large for you.
  3. Calculate u = nm + tm.
  4. Our goal is now to calculate 2u. Use the fact that 2u = 2v * 2u-v and find an integer v such that 0 <= u-v < 1.
  5. Calculate w = 2u-v, again using our friend Taylor or Newton-Raphson. The interval of interest is 0 <= u-v < 1.
  6. Your answer is now 2v * w.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!