I was trying to write a simple program to determine if input integer is a power of two.
I had following code. It will fail the test case for n=536870912
(536870912
is the 2^29
).
I tried with formating the number, format(y,'12g')
the output is close to 0
but not equal to 0
, 3.43965 e-07
.
How should I overcome this number issue?
s= math.log(n,2)
[sh,y]=divmod(s,1)
if y!=0:
#if format(yu,'20f')!=format(0,'20f') :
return False
else:
return True