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: ... where x is the number you're taking the square root of, and: You can even have 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 ( will be much closer than ), 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 or something, but that's all up to you. Good luck! Also, as a side note, if you insist on calculating arbitrary and you have exponential (and natural log), you can use the formula 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) { 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: 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. 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. :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. 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 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 |