when number is negative min value
when base is 0 and number is negative
class Solution {
public double myPow(double x, int n) {
if (n < 0) {
if (x == 0) return Double.POSITIVE_INFINITY;
else return 1 / myPow(x, -(n + 1)) * 1 / x;
}
double res = 1;
while (n != 0) {
if ((n & 1) == 1) {
res *= x;
}
x *= x;
n >>= 1;
}
return res;
}
}
class Solution {
public double myPow(double x, int n) {
if (n < 0) {
if (x == 0) return Double.POSITIVE_INFINITY;
else return 1 / myPow(x, -(n + 1)) * 1 / x;
}
if (n == 0) return 1;
return n % 2 == 1 ? x * myPow(x, n - 1) : myPow(x * x, n >> 1);
}
}