views:

135

answers:

5

How is the square root function implemented?

+2  A: 

There are lots of ways to compute the square root. Wikipedia has the details for a pseudo computer algo too. Are you looking for the implementation in a specific library or for a specific precision?

Gangadhar
A: 

If you're interested, the Numerical Recipes book has lots on how to calculate square roots, sines and cosines, exponentials, logarithms, and so on.

Philip Potter
A: 

Take a look here. It contains 13 C++ implementations of sqrt, together with speed and precision comparison.

Lior Kogan
The author of the article tries to measure the speed of the functions by averaging the time, which is good. It's just that it's implemented wrong.
Peter Stuifzand
A: 

Square root is usually based on Newton's iterative method:

http://mathworld.wolfram.com/NewtonsIteration.html

duffymo
A: 

On Intel hardware, it's often implemented on top of the hardware SQRT instruction. Some libraries just use the result of that straight off, some may put it through a couple of rounds of Newton optimisation to make it more accurate in the corner cases.

Tom Anderson