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


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



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


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


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;


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)