Trying to create a recursive method that raises a double value to an int power for a java course. The instructions say "However, write the code so that when n is even the method will return (x ^ (n/2))^2."
This is what I have thus far:
     public static double powerFaster(double x, int n) {
         if (n == 0) {
            return 1;
         } 
         else if ((n % 2) == 0) {
           return ((x ^ (n / 2.0) ^ 2.0));  //Error occurs here.
         } else {
           return x * powerFaster(x, (n - 1));
         }
     }
                Explanation: The cause of this error is due to the precedence of operators. == operator has higher precedence over & operator. As a result, 100==1 will be calculated first and return the boolean value. If you notice & operator has two operands now, one is int, and the other is boolean.
Interface BinaryOperator<T>Represents an operation upon two operands of the same type, producing a result of the same type as the operands. This is a specialization of BiFunction for the case where the operands and the result are all of the same type.
OR is a logical operator in Java that is mostly utilized in control statements such as if-else. It outputs true if any one of the statements is evaluated as true; otherwise, it will be returned as false. OR operator checks the condition from the left side.
^ is a XOR operator, not power. Use Math.pow() for power.
That said, I think you missed the point of the exercise.
You should return powerFaster(x, n/2) * powerFaster(x, n/2); when n is even (actually make one recursive call, store its result in a variable and multiply it by itself).
public static double powerFaster(double x, int n) {
     if (n == 0) {
        return 1;
     } 
     else if ((n % 2) == 0) {
       double pow = powerFaster(x, n/2);
       return pow * pow;
     } else {
       return x * powerFaster(x, (n - 1));
     }
 }
                        If you are doing this for speed, you want to avoid using Math.pow as this is the function you are trying to replace.
To get a square of a value you can do d * d
public static void main(String[] args) {
    System.out.println(powerOf(2, 9));
}
public static double powerOf(double d, int n) {
    if (n < 0) return 1 / powerOf(d, -n);
    double ret = 1;
    if (n > 1)
        ret = powerOf(d * d, n / 2);
    if (n % 2 != 0)
        ret *= d;
    return ret;
}
prints
512.0
                        ^ is bitwise XOR, used with integers:
int a = 6; //00000110
int b = 5; //00000101
int c = a ^ b; //gives you 3 = 00000011, not 6^5
The operation is in the binary level:
00000110 //a
00000101 //b
--------- XOR
00000011
To perform power, use Math.pow():
Math.pow(2.0, 1.0) //gives you 2.0
                        In Java you should use java.lang.Math.pow(double x, double n) for raising value x to a power of n
^ is a XOR operator - see this SO question for more details
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With