I am trying to create a recursive function:
double fifthRoot(double x, double maxError)
In this function, I have to return y such that y^5 will be within maxError of x. Assume x > 0. Also assume maxError is big enough to prevent problems due to rounding.
In order to make this function work, I have to call a helper function that takes the original number x, maxError and two doubles, e.g. low and high. low is a number whose fifth power is <= x an high's fifth power is >= x. Take the average of those two numbers and take the fifth power of that. Then update low and high appropriately.
Here is my code:
double fifthRootHelper(double x, double maxError, double low, double high)
{
double average = (high + low) / 2;
double fifthPower = (average * average * average * average * average);
if((fifthPower - x) <= x)
{
return fifthRootHelper(x,maxError,low,fifthPower);
}
if((-fifthPower - x) >= x)
{
return fifthRootHelper(x,maxError,fifthPower,high);
}
return fifthPower - x;
}
double fifthRoot(double x, double maxError)
{
double maximum = (x <= maxError || x >= maxError) ? maximum : 1;
return fifthRootHelper(x,0,maximum,maxError);
}
int main()
{
double num = 32;
double error = 1;
cout << fifthRoot(num,error);
return 0;
}
When I ran this code, my program crashed. Is there anything that I need to change in my fifthRoot() and/or helper functions?