I have written the following code in Python 3:
import math
class Solution:
def isPowerOfTwo(self, n: int):
y = math.log(n,2)
z = int(y)
return z==y
I know there are alternate algorithms to find the answer.
I want to understand:
As I see it, mathematically there is nothing wrong with my solution. The solution fails for some values because of the way python calculates logs.
Is there a way I can still make it work?
Also as a beginner programmer do I also need to consider the limitations of python (esp when it comes to floating points) and implement an alternative algorithm even my original algorithm is correct. If so what are the other pitfalls I might run into when writing algorithms which I think are correct but won't work as I want them to because of how the program implements them?