# Mising test case of power Integer.MIN_VALUE

• In this case it can not be converted to a positive exponential and be evaluated as such. The following code can pass test with one line handling the special situation commented out. (Note that another solution is to copy n to a long type).

``````public class Solution {
public double pow(double x, int n) {
if(n==0) return 1.0;

boolean division = n<0;
//if(n==Integer.MIN_VALUE) return handleSpecialCase(x);
if(division) n = -n;

//Estimate of the powers array size:consider n=16 and 10
double[] powers = new double[(int) (Math.log(n)/Math.log(2.0)) + 2];
powers[0] = x;
int i=1;
int expo = 2;
for(; true; i++, expo *= 2) {
powers[i] = powers[i-1]*powers[i-1];
if(expo*2>n || expo*2<0) { //bug found, overflow!
break;
}
}

double result = 1.0;
for(; n>0; i--, expo /= 2) {
if(expo <= n) {
if (division) result /= powers[i];
else result *= powers[i];
n -= expo;
}
}

return result;
}

// power = Integer.MIN_VALUE is special case since we can't make it
//positive (overflow)
private double handleSpecialCase(double x) {
double power = x;
for(int i=0; i<31; i++) {
power = power*power;
}
//power now is x to the power (2 to the power of 31)
return 1.0/power;
}
}
``````

Suggested test case (Junit):

``````@Test
public void testSolution() {
Solution sol1 = new Solution();
assertEquals("Watch out for overflow.", 5.444710383108578E-94, sol1.pow(1.0000001, -2147483648), 0.00000001);
}``````

• This post is deleted!

• This post is deleted!

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.