Power(x, n)
Calculate nth power of a double number x, where n is an integer.
You must handle cases that:
xis negativenis negativex = 1andn = Integer.MAX_VALUEx = 1andn = Integer.MIN_VALUEx = -1andn = Integer.MAX_VALUEx = -1andn = Integer.MIN_VALUE
As for x = 1 we directly return 1. For x < 0 we find the result's sign first and calculate -x's
power of n. For n < 0, we calculate 1/pow(x, -n).
public class Power {
public double myPow(double x, int n) {
if (x == 0) return 0;
if (x == 1) return 1.0;
if (x < 0) return ((n & 1) == 1) ? -myPow(-x, n) : myPow(-x, n);
else if (n == 0) return 1;
else if (n < 0) return 1.0 / myPow(x, -n);
else if ((n & 1) == 1) return myPow(x, n - 1) * x;
else {
double r = myPow(x, n >> 1);
return r * r;
}
}
}