Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate an arbitrary power/root?

I have a application which needs to raise a number to a fractional power. The target platform is an FPGA and I can get estimates on an FPU size for it, but I need an algorithm for raising a number to a fractional power just for a feasibility study. I'm assuming floating point as a worst case, I expect in practice we will be able to use short cuts, but for now I want to show that worst case can be implemented on our part.

Thought I'd ask here and see if there were any common methods that I could checkout. I know there are software methods of doing this, I want just a reasonably efficient algorithm to start with. I'll worry about the FPGA implementation.

like image 288
NoMoreZealots Avatar asked Sep 03 '09 21:09

NoMoreZealots


People also ask

How do you find the power of a digit?

Example: Suppose, 5 is the base and 4 is the exponent. In order to find the power of a number, multiply the number itself 4 times, i.e. (5 * 5 * 5 * 5 = 625).

How do you find the power of a log?

The logarithm of a number is the power to which 10 must be raised to equal that number. Some simple examples: 10^2 = 100, therefore \log 100 = 2. 10^3 = 1000, therefore \log 1000 = 3.

How do computers calculate powers?

It is generally logarithmic in the size of the exponent. The algorithm is based on the invariant "a^b*result = a0^b0", where a0 and b0 are the initial values of a and b. For negative or non-integer exponents, logarithms and approximations and numerical analysis are needed.


1 Answers

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.)

like image 177
Jason S Avatar answered Oct 16 '22 04:10

Jason S