Why using double ceil gives 30 for 2^29 = 536870912 this when i use float it gives the right ans i.e 29


#1

int Solution::isPower(int A) {
if(A==1)return 1;
else if(A<=3)return 0;
else
{
int i;
for(i=2;i<=sqrt(A);i++)
{
float ans= log(A)/log(i);
//cout<< ceil(ans)<<" "<<floor(ans)<<endl;
if(ceil(ans)==floor(ans))return 1;
}
return 0;
}

}


#2

Dude, did you get your ans?
If yes, tell me the reason too.


#3

I used if(j-float((int)j) == (float)0) and it works fine


#4

it is working fine if you take log10 instead of natural log (i.e., log). I don’t know the exact reason why it isn’t working with log :confused:
double temp = log10(A)/log10(i);
if(floor(temp) == ceil(temp)) return 1;


#5

Thanks for this. I think using natural log will lead to some minute differences in the calculation because we are using division. (there will be some extra decimal digits even in the case of 2^29)