我發現如果你在下面輸入 0.30,它會得到只包含 0.20 的結果。預期的結果是計算出 0.30 由 1 倍的 0.20 和 1 倍的 0.10 組成。其他數字似乎作業正常。
x = float(input('Type a number ')) #Type 0.30
print(x // 1, "1")
x = x % 1
print(x // 0.50, "0.50")
x = x % 0.50
print(x // 0.20, "0.20")
x = x % 0.20
print(x // 0.10, "0.10")
結果
The actual result is:
0.0 1
0.0 0.50
1.0 0.20
0.0 0.10
The expected is:
0.0 1
0.0 0.50
1.0 0.20
1.0 0.10
uj5u.com熱心網友回復:
我認為當您還在其間列印 x 時就很清楚了:
x = float(input('Type a number ')) #Type 0.30
print(x // 1, "1")
x = x % 1
print(x)
print(x // 0.50, "0.50")
x = x % 0.50
print(x)
print(x // 0.20, "0.20")
x = x % 0.20
print(x)
print(x // 0.10, "0.10")
輸出:
0.0 1
0.3
0.0 0.50
0.3
1.0 0.20
0.09999999999999998
0.0 0.10
當您看到浮點值如何存盤在 python 中時,知道這是有道理的:https : //docs.python.org/3/tutorial/floatingpoint.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/350500.html
上一篇:添加華為套件時出現“Couldnotfindcom.huawei.hms:location:6.0.0.302”錯誤
下一篇:python有多少動作可以達到零
