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

I've come up with this now:

public static double power(double x, int n){
  if (n == 0)
    return 1;
  else
    if (n > 0)
    return x * power(x,n - 1);
  else
    return (1.0 / power(x, Math.abs(n)));
  }

I've come up with this now:

public static double power(double x, int n){
  if (n == 0)
    return 1;
  else
    if (n > 0)
    return x * power(x,n - 1);
  else
    return (1.0 / power(x, Math.abs(n)));
  }

That seems like it should work to me. Pretty clever, actually. I've never been any good with the "static" operator, so I don't know if that makes any difference here. But the logicc seems good to me. But does Java let you do this since you are returning a double?

return 1;

or do you have to do this?

return 1.0;

Anyway, the function looks right to me.

The first three lines of code are from the book.

Now I just have to write a main which I don't want to do! Hey could you write a main for me? wink wink!

Ok well here is my complete program with the main:

public static double power(double x, int n){
    if (n == 0)
      return 1;
    else
    if (n > 0)
      return x * power(x,n - 1);
    else
    return (1.0 / power(x, Math.abs(n)-1));
  }
    
 public static void main(String[]args){
    System.out.println(power(2, -9));
    System.out.println(power(4,6));
    System.out.println(power(3,0));
    
  }

But I'm getting this error and I don't understand why. Everything looks right too me so I don't understand why I'm getting it. Maybe I'm just having a blonde moment and I need another set of eyes too look at it.

exponential.java:1: class, interface, or enum expected
public static double power(double x, int n){
^
exponential.java:4: class, interface, or enum expected
else
^
exponential.java:7: class, interface, or enum expected
else
^
exponential.java:9: class, interface, or enum expected
}
^
exponential.java:11: class, interface, or enum expected
public static void main(String[]args){
^
exponential.java:13: class, interface, or enum expected
System.out.println(power(4,6));
^
exponential.java:14: class, interface, or enum expected
System.out.println(power(3,0));
^
exponential.java:16: class, interface, or enum expected
}
^

I tried adding this, it didn't work:

public class exponential {
  private double x;
  private int n;

Never mind I fixed it! I didn't put it at the top.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.