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
Most   Popular
Upcoming Events

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.