I am trying to find the cube root of a number by bisecting it and then narrowing it down. I have the program for the square root in that way, but the cube root method simply continues to loop and never gives an answer. I am not sure where I have gone wrong and need some advice.
public class myFunc
{
public static double squareRoot(double value, double precision)
{
double low, high, middle;
high = 1;
low = value;
middle = (high + low) / 2;
Console.WriteLine("{0,20:n12}{1,20:n12}{2,20:n12}", low, middle, high);
while ((high-low)>precision)
{
if ((middle * middle) <value)
{
low = middle;
}
else
{
high = middle;
}
middle = (high + low) / 2;
Console.WriteLine("{0,20:n12}{1,20:n12}{2,20:n12}", low, middle, high);
}
return (middle);
}
public static double cubeRoot(double value, double precision)
{
double low, high, middle;
high = value;
low = 1;
middle = (high + low) / 3;
Console.WriteLine("{0,20:n12} {1,20:n12} {2,20:n12}", low, middle, high);
while ((high - low) > precision)
{
if ((middle * middle*middle)>value)
{
high = middle;
}
else
{
low = middle;
}
middle = (high + low) / 3;
Console.WriteLine("{0,20:n12} {1,20:n12} {2,20:n12}", low, middle, high);
}
return (middle);
}