I have the following power function that I have written in c#:
public static double pow2(double x, double n)
{
if (n == 0)
return 1;
else
{
stepAccumulator++;
return pow2 (x, Math.Floor (n / 2.0)) * pow2 (x, Math.Ceiling (n / 2));
}
}
When I run the program I simply say result=pow2(1,1000);
and then time the function using a Stopwatch
object and then print the result at the end. Unfortunately, when I run this program I get the following error: Process is terminated due to StackOverflowException. Why is this happening and how can I stop it?