Purely as a matter of style, when a value such as
0.00001 is used more than once in a program, it is better to use a defined constant, such as
const double accuracy = 0.00001;
- use whatever name is appropriate, instead of typing the same value repeatedly. This has several advantage, one being that if you want or need to change the value, it only has to be done once, in one place.
As for the algorithm, it looks a little complex.
One way to find a square root is to start with an initial estmate, which can be any reasonable value, such as 1.0, or the the number itself.
Then improve the accuracy of the estimate by repeatedly carrying out this calculation:
1 2
|
new_estimate = ((number/old_estimate) + old_estimate) * 0.5;
old_estimate = new_estimate;
|