I was just wondering what the actual function behind the Math.sqrt() method was. I know what it does, but I would like to know how it finds the square root of a number.
Asked
Active
Viewed 464 times
0
-
its implementation specific according to the es2015 spec. could be implemented in hardware, software, etc. – Daniel A. White May 03 '17 at 10:40
-
1you could look up the v8 source code to see how its implemented there, but it would be different for firefox, edge, safari, etc. – Daniel A. White May 03 '17 at 10:41
-
1there are many ways to do this like approximation, binary search, `log/exp` ... for example see [How to get a square root for 32 bit input in one clock cycle only?](http://stackoverflow.com/a/34657972/2521214) and [Power by squaring for negative exponents](http://stackoverflow.com/a/30962495/2521214) Which method to use depends on what numbers are you dealing with, what HW capabilities you got, what precision and speed you need ... – Spektre May 03 '17 at 11:05
-
1I wanted to know the same thing a few years back and was actually a bit surprised when i found out that you cant calculate the sqrt, you basically "guess" until you are close enough. Of course there are smart algorithms but you still have to test until you are close enough. There is no formula to get the exact value that you can try out on a calculator. Thought you might want to know that since that was all i wanted to know before, but otherwise if you want to go deeper the earlier links are a good start! – MrApnea May 03 '17 at 14:59