It is necessary to determine whether log a on base b is integer. My solution is: cout<<fmod(log(a)/log(b),1); But, for example, when testing a = 46656 b = 6, the program outputs 8.88178e-016 , instead of the expected 0 . How can this be fixed / implemented in another way?

1 answer 1

A logarithm is integer if and only if the number is a power of a base.

Operations with fractional numbers introduce an error in the calculations, so if the condition is given integers (in the sense that they are stored in integer variables), then it makes sense not to regret 32 ​​operations just to check the corresponding degrees:

 bool check(unsigned a, unsigned b) { unsigned x; for (x=1; x<a; x*=b); return x==a; } 

If necessary, consider overflowing.