class Solution {
public double myPow(double x, int n) {
double res = 1.0;
long N = n;
if (n < 0) {
x = 1 / x;
N = -N; // wrong: N = -n;
}
double curProduct = x;
for (long i = N; i > 0; i /= 2) {
if (i % 2 == 1) {
res *= curProduct;
}
curProduct *= curProduct;
}
return res;
}
}