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)
      lastGuess = guess;
      guess = ((square / lastGuess + lastGuess) / 2);
   return guess;
comments powered by Disqus
Upcoming Events

.NET Insight

Sign up for our newsletter.

I agree to this site's Privacy Policy.