Treat Code as Data: C#: Implement Hero Roots Imperatively: Listing 3

This imperative implementation of Hero's square root algorithm concentrates on how to calculate the root. It contains loops and checks, mixed with algorithms.

public static double HeroRoot(double square, double epsilon)
{
   double lastGuess = 1;
   double guess = ((square / lastGuess + lastGuess) / 2);
   while (Math.Abs(lastGuess - guess) > epsilon)
   {
      Console.WriteLine(guess);
      lastGuess = guess;
      guess = ((square / lastGuess + lastGuess) / 2);
   }
   return guess;
}

Reader Comments:

Add Your Comments Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above