Here is the problem: Does the recursive algorithm for raising x to the power n work for negative values of n? Does it work for negative values of x? Indicate what happens if it is called for each of these cases.
Here is my answer for the above: It's a recursive algorithm, the negative calls depends on the positive calls. The negative exponents will not work, if the positive exponents do not work.
Ok I have this recursive method which evalutes the value of positive exponents fine, but I need it to work with negative exponents, and I'm not sure how to do this. This is the code I have now
Here is the recursive method:
public static double power(double x, int n){
if (n == 0)
return 1;
else
return x * power(x,n - 1);
}
Here is what I came up with so far:
public static double negativepower(double x, int n){
if (n == 0)
return 1.0;
else
return (1.0 / power(x, Math.abs(n)));
}
I keep getting the wrong answer with 2^-3 gives me .5