The standard way to check for floating point equality is to check the quality within a certain tolerance:
def floateq(a, b, tolerance=0.00000001):
return abs(a-b) < tolerance
Now you can check whether the rounded, converted to integer version of the cube root is equal to the cube root itself within a certain tolerance:
def has_integer_cube_root(n):
floatroot = (n ** (1.0 / 3.0))
introot = int(round(floatroot))
return floateq(floatroot, introot)
Using:
>>> has_integer_cube_root(125)
True
>>> has_integer_cube_root(126)
False
However, this is pretty inaccurate for your use case:
>>> has_integer_cube_root(40000**3)
True
>>> has_integer_cube_root(40000**3 + 1)
True
, - .
EDIT: , , :
def has_integer_cube_root(n):
floatroot = (n ** (1.0 / 3.0))
introot = int(round(floatroot))
return introot*introot*introot == n
>>> has_integer_cube_root(40000**3)
True
>>> has_integer_cube_root(40000**3 + 1)
False