Power(x, n)
Calculate n
th power of a double number x
, where n
is an integer.
You must handle cases that:
x
is negativen
is negativex = 1
andn = Integer.MAX_VALUE
x = 1
andn = Integer.MIN_VALUE
x = -1
andn = Integer.MAX_VALUE
x = -1
andn = 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;
}
}
}