Possible Duplicate:
Python float to int conversion problem
def isPerfectCube(x):
temp = x ** (float(1) / 3)
temp = temp ** 3
temp = int(temp)
print (temp == x)
return (temp == x)
I am trying to write a program to determine if a number x is a perfect cube. For some reason, some numbers have been rounding down a whole integer, instead of just truncating. I have never completely understood how floats work. I know they are inaccurate, so I am assuming the issue is coming from there.
For example:
- 125.0 truncates to 124 when casted to int.
- 64.0 truncates to 63 when casted to int.
Any help or explanations would be greatly appreciated.