If you're calculating square roots only, fractional exponentiation might not be what you're looking for. I would look into the Babylonian method first, which is represented by the recursive equation:
... where x is the number you're taking the square root of, and:
You can even have
![[Image: gif.latex?B_0]](http://latex.codecogs.com/gif.latex?B_0)
default to 1 if you really want, you will get to an accurate calculation in the end. The Babylonian method zones in on a correct square root very quickly (
![[Image: gif.latex?B_%7Bn+1%7D]](http://latex.codecogs.com/gif.latex?B_%7Bn+1%7D)
will be much closer than
![[Image: gif.latex?B_%7Bn%7D]](http://latex.codecogs.com/gif.latex?B_%7Bn%7D)
), so you can just calculate a few (10? play around with accuracy!) iterations of the Babylonian sequence and give that as a square root. You may also have to round at the end if you don't want
![[Image: gif.latex?%5Csqrt%2016%20%3D%203.99999784]](http://latex.codecogs.com/gif.latex?%5Csqrt%2016%20%3D%203.99999784)
or something, but that's all up to you.
Good luck!
Also, as a side note, if you insist on calculating arbitrary
![[Image: gif.latex?x%5Ey]](http://latex.codecogs.com/gif.latex?x%5Ey)
and you have exponential (and natural log), you can use the formula
![[Image: gif.latex?e%5E%7By%20ln%20x%7D]](http://latex.codecogs.com/gif.latex?e%5E%7By%20ln%20x%7D)
for an accurate exponent.
Another side note, just because I'm far too maths-happy for my own good, you could also have accuracy as an optional argument which would be a real feature that common sqrt functions don't have. So for instance, you could have something like
Code:
public static double sqrt(int x, int accuracy) {
//calculations and stuff, in a loop based on accuracy
}
public static double sqrt(int x) {
return sqrt(x, //some default accuracy here)
}
Just a thought.
Links:
http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method
http://stackoverflow.com/questions/9434183/whats-the-fastest-algorithm-to-perform-exponentiation
http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Example_3 (This one is a last resort! Don't ruin it for yourself unless you're really stuck :3)