When I run the code
b = 7**7
I get b as 823543
But when I run
b= 823543.0**(1.0/7.0)
It gives me b as 6.999999999999999
If it is something as simple as 4**(1/2) it returns 2.
My question is why doesn't python just return a perfect 7?
Also I was doing this to check if a number can be written n can be written in the form p^q where p>0 and q>1 to do so I did this:
def isPower(self, A):
possible = 0 # Integer value to check if it is possible , 0 assuming it is false initally
if(A==1):
return 1
for i in xrange(2,A-1):
res = float(A)**(1.0/float(i)) #Check if it has sqaure root or cube root up untill A-1
if(res.is_integer()): #If taking the power gives me whole number then saying it is possible
possible = 1
break
return possible
This logic fails with higher numbers like 823543 because the power returns a imprecise value how would I solve this?