所以我必須做一個代碼來檢查一個數字是否是一個完美的立方體,但由於某種原因,任何大於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#模塊十進制 –