public double pow2(double x, int n){ // deal with negativity separately
        if(n == Integer.MIN_VALUE) return 1.0/(pow2(x, n+1) * pow2(x, -1));
        if(n < 0) return 1.0 / pow2(x, -n); // can not handle the case where n = MIN_VALUE
        if(n == 1) return x;
        if(n == 0) return 1.0;
        double y = pow2(x, n / 2);
        if(n % 2 == 0) {
            return y*y;
        }else{
            return y * y * x;
        }
    }