```
class Solution {
public:
double pow(double x, int n) {
if (n == 0) return 1;
if (abs(n) % 2 == 1) {
if (n < 0) return 1/(x*pow(x*x, (-(n+1))/2));
else return x * pow(x * x, n / 2);
} else {
if (n < 0) return 1/(x*x*pow(x*x, (-(n+2))/2));
else return pow(x * x, n / 2);
}
}
};
```