1

I am trying to find x^y where x is positive and y is a real number. There are many sources online to find for integers but not much for real numbers.

Example:

6 ^ 4.3 = 2218.4537377949778046576946747662

I am not supposed to use any Math library. I know this works:

exp(y*log(x))

But I have to find exp too without any library. Is there any series I can use to get the real number power? I am doing it in Java.

Humayun Shabbir
  • 2,961
  • 4
  • 20
  • 33
  • 6
    Why do you need to do it without a library? Who's going to define `e` and find the logarithm? – EpicPandaForce Jul 27 '14 at 16:59
  • Check source code of `Math.pow()` – Eric Jul 27 '14 at 17:13
  • Yes, the requirements are that there should be no library to be used. – user3882039 Jul 27 '14 at 17:15
  • 2
    In Java, you should use [`Math.pow()`](http://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#pow-double-double-) for finding `x` to the power of `y`, where both `x` and `y` are real numbers. There's no need to use logarithms if a built-in solution exists. A solution from scratch that works for real numbers and takes into account all edge cases will demand quite a bit of work… see this [post](http://stackoverflow.com/questions/2882706/how-can-i-write-a-power-function-myself) for some hints. – Óscar López Jul 27 '14 at 17:02

2 Answers2

2

The integer part of the exponent is easy to deal with by repeated squaring.

For the fractional part, you can use the binary representation and use square roots (x^0.1b=sqrt(x), x^0.01b=sqrt(sqrt(x))...), computed by Heron's method.

4.3 = 4 + 1/4 + 1/32 + 1/64 + 1/512...

Then

6^4=1296

and by Heron's iterations

6^(1/2) = 3.5
6^(1/2) = 2.60714285714
6^(1/2) = 2.45425636008
6^(1/2) = 2.44949437161
6^(1/2) = 2.44948974279
6^(1/2) = 2.44948974278
6^(1/2) = 2.44948974278

6^(1/4) = 1.72474487139
6^(1/4) = 1.57247448714
6^(1/4) = 1.56510194466
6^(1/4) = 1.56508458017
6^(1/4) = 1.56508458007
6^(1/4) = 1.56508458007

6^(1/8) = 1.28254229004
6^(1/8) = 1.25142045246
6^(1/8) = 1.25103346471
6^(1/8) = 1.25103340486
6^(1/8) = 1.25103340486
6^(1/8) = 1.25103340486

6^(1/16) = 1.12551670243
6^(1/16) = 1.11851794241
6^(1/16) = 1.11849604619
6^(1/16) = 1.11849604597
6^(1/16) = 1.11849604597

6^(1/32) = 1.05924802299
6^(1/32) = 1.0575910323
6^(1/32) = 1.05758973424
6^(1/32) = 1.05758973424
6^(1/32) = 1.05758973424

6^(1/64) = 1.02879486712
6^(1/64) = 1.02839189837
6^(1/64) = 1.02839181942
6^(1/64) = 1.02839181942
6^(1/64) = 1.02839181942

6^(1/128) = 1.01419590971
6^(1/128) = 1.01409655817
6^(1/128) = 1.0140965533
6^(1/128) = 1.0140965533

6^(1/256) = 1.00704827665
6^(1/256) = 1.0070236114
6^(1/256) = 1.00702361109
6^(1/256) = 1.00702361109

6^(1/512) = 1.00351180555
6^(1/512) = 1.00350566074
6^(1/512) = 1.00350566072
6^(1/512) = 1.00350566072 

Finally,

6^4.3 = 1296 * 1.56508458007 * 1.05758973424 * 1.02839181942 * 1.00351180555 ... 
      = 2218.45373705

This method is not optimal, it does not converge very quickly and is not very accurate, but it is rather simple to implement.

1

For a good compromise between efficiency and ease of implementation, you can develop your own pow function exp(y.ln(x)) using Taylor expansions.

The "hardest" part is the logarithm.

First normalize x by finding the power of 2 such that x=(2^n).x', with 1<=x'<2. Then ln(x) = n.ln(2) + ln(x').

6 = (2^2) x 1.5

Evaluate ln(x') = 2 argth((x'-1) / (x'+1)) by the inverse hyperbolic tangent series.

ln(1.5) = 2 (0.2 + 0.2^3/3 + 0.2^5/5...) = 
0.4
0.405333333333
0.405461333333
0.405464990476
0.405465104254
0.405465107978
0.405465108104
0.405465108108
...

ln(6) = 2 x 0.69314718056 + 0.405465108108 = 1.791759469228

Then compute y.ln(x), split into integer part (use exponentiation by squaring) and fractional part.

4.3 x 1.791759469228 = 7.704565717681

e^7 = 1096.633158428

For the fractional part, use the standard Taylor development of the exponential.

e^0.704565717681 = 1 + 0.704565717681 + 0.704565717681^2/2 + 0.704565717681^3/6...
1
1.70456571768
1.95277214295
2.01106472233
2.02133246059
2.02277931986
2.0229492211
2.02296632204
2.02296782814
2.02296794604
2.02296795435
2.02296795488
2.02296795491
...

And multiply

6^4.3 = 1096.633158428 x 2.02296795491 = 2218.45373779