所以我必须做一个代码来检查一个数字是否是一个完美的立方体,但由于某种原因,任何大于27的立方体,它说它的根是x.99999999。 (即它返回64 **(1/3)作为3.9999 & 125 **(1/3)作为4.9999)。检查一个完美的立方体
n = int(input("What number would you like to check if it is a cube?"))
def is_cube(n):
guess = n**(1.0/3.0)
if (guess)%1 == 0:
print(True, "it's cubed root is", guess)
else:
print(False, "it's cubed root is", guess)
is_cube(n)
浮点数不精确:https://docs.python.org/3/tutorial/floatingpoint.html查看使用十进制模块:https://docs.python.org/3/library/decimal.html#模块十进制 –