Forums - Open Redstone Engineers
Square root - Printable Version

+- Forums - Open Redstone Engineers (https://forum.openredstone.org)
+-- Forum: Off-Topic (https://forum.openredstone.org/forum-4.html)
+--- Forum: Programming (https://forum.openredstone.org/forum-8.html)
+--- Thread: Square root (/thread-2745.html)



Square root - gelloe - 03-11-2014

I'm making a Java library that finds the square root of a number, yes there already is a java library for that but I don't care. I found the formula for finding the square root of a number, and it's N^0.50. But Java doesn't know what the operator ^ means, so I have to use addition, subtraction, multiplication, and division to substitute ^. But the problem is, I'm not a mathematician, how do I substitute ^? I NEED THE HELP PEOPLE!

TL;DR. N^0.50 = sqrt(N). Need substitute for "^"


RE: Square root - Iceglade - 03-11-2014

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:

[Image: gif.latex?B_%7Bn+1%7D%3D%5Cfrac%7B1...BB_n%7D%29]

... where x is the number you're taking the square root of, and:

[Image: gif.latex?B_0%20%5Capprox%20%5Csqrt%20x]

You can even have [Image: 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] will be much closer than [Image: 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] or something, but that's all up to you.

Good luck! Big Grin

Also, as a side note, if you insist on calculating arbitrary [Image: gif.latex?x%5Ey] and you have exponential (and natural log), you can use the formula [Image: gif.latex?e%5E%7By ln%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. Smile

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)


RE: Square root - gelloe - 03-12-2014

(03-11-2014, 06:06 PM)Iceglade Wrote: 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:

[Image: gif.latex?B_%7Bn+1%7D%3D%5Cfrac%7B1...BB_n%7D%29]

... where x is the number you're taking the square root of, and:

[Image: gif.latex?B_0%20%5Capprox%20%5Csqrt%20x]

You can even have [Image: 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] will be much closer than [Image: 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] or something, but that's all up to you.

Good luck! Big Grin

Also, as a side note, if you insist on calculating arbitrary [Image: gif.latex?x%5Ey] and you have exponential (and natural log), you can use the formula [Image: gif.latex?e%5E%7By ln%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. Smile

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)

Accuracy, well I don't exactly want to use the accuracy method because that puts strain on a computer and it can't always be 100% accurate. I still like the N^0.50 method, but I can still make alternate method based on one of your suggestions.


RE: Square root - xdot - 03-12-2014

Eh, just use the internal sqrt(x) function. For exponentiation you can use pow. sqrt(x) = pow(x, 0.5).


RE: Square root - gelloe - 03-12-2014

(03-12-2014, 07:58 PM)xdot Wrote: Eh, just use the internal sqrt(x) function. For exponentiation you can use pow. sqrt(x) = pow(x, 0.5).

Yeah, I think I'll use math.pow. I made a few posts on reddit and they all suggested I use that functino.


RE: Square root - gelloe - 03-17-2014

(03-11-2014, 06:06 PM)Iceglade Wrote: Play with accuracy.

Did dat.
[Image: 4mVBifz.png]

First one is using accuracy to the 100,000th degree. The second one is the actual value of pi. I gotta say, if you put a number big enough, it can really calculator good.


RE: Square root - Iceglade - 03-17-2014

(03-17-2014, 08:46 PM)gelloe Wrote:
(03-11-2014, 06:06 PM)Iceglade Wrote: Play with accuracy.

Did dat.

[url=http://i.imgur.com/4mVBifz.png"
[Image: 4mVBifz.png]
[/url]

First one is using accuracy to the 100,000th degree. The second one is the actual value of pi. I gotta say, if you put a number big enough, it can really calculator good.

:3 fun


RE: Square root - gelloe - 03-17-2014

(03-17-2014, 08:48 PM)Iceglade Wrote:
(03-17-2014, 08:46 PM)gelloe Wrote:
(03-11-2014, 06:06 PM)Iceglade Wrote: Play with accuracy.

Did dat.

[url=http://i.imgur.com/4mVBifz.png"
[Image: 4mVBifz.png]
[/url]

First one is using accuracy to the 100,000th degree. The second one is the actual value of pi. I gotta say, if you put a number big enough, it can really calculator good.

:3 fun

Jesus Christ! You replied to me before I could finish correcting my post *-*


RE: Square root - Iceglade - 03-17-2014

Hehe, well it was just luck. I happened to get home from school at that exact moment and didn't see the time on the post Tongue


RE: Square root - greatgamer34 - 03-18-2014

umm for calculating square roots its something like sqrt(a)=10^(logbase10 of a)/(2)

Idk if this is what your looking for lmao.


RE: Square root - Iceglade - 03-18-2014

Yeah, there are a lot of ways, I said e^yln x earlier but it looks like he just went with an internal pow function =P