Kevin Markson
Kevin Markson

Reputation: 33

Algorithm for solving decimal exponents without fractions

Could someone explain the steps involved in solving something like 2^2.2 if fractions couldn't be used, such as in an infinite precision calculation?

Upvotes: 0

Views: 1625

Answers (2)

peterchen
peterchen

Reputation: 41106

In the general case a^b where ^ is exponentiation (not XOR), and a and b are real numbers:

pow(a,b) = exp( b * log(a) ) 
exp(x)   = sum[n = 0->inf]  x^n / n!
ln(x)    = sum[n = 1->inf]  (x-1)^n / n
x^n      = n == 0  ? 1   // unless x == 0
          (n%2==0) ? x^(n/2) * x^(n/2) 
          othewrwise x*x^(n-1)  
          // faster than loop for large n, 

This requries two series that you need to terminate at a certain precision, but the exponentiation is only with natural numbers.

You also have to deal with sign of a and b (a^-b = 1/(a^b)), zero values etc.

Upvotes: 2

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272537

A typical implementation of pow(x,y) (i.e. x^y) involves computing exp(y*log(x)). No fractions involved.

Upvotes: 0

Related Questions