为什么 Python 中这两个被证明相等的小数中有一些被认为不相等?
Why are some of these two proven-to-be-equal decimals in Python considered not to be equal?
在我的 Python 课程中,我正在尝试比较将多个浮点数相加与将多个小数相加的准确性。实验室的要求之一是计算有多少次将 .1 相加得到正确的和。我的代码中的所有内容都正常工作,除了我的计数器,用于计算 decimal 添加的正确数量。对于 100 次迭代,它只计算 20 次是正确的(每 5 次迭代),尽管我已经通过打印每次迭代证明它们都是正确的。为什么会发生这种情况,如何解决?谢谢。 (您可以在下面找到完整的代码以获得完整的上下文和输出。)
代码:
from decimal import *
floatSum = 0
decSum = 0
toAddFloat = .1
toAddDec = Decimal(".1")
max = 100 # number of iterations
floatCorrect = 0
decCorrect = 0
# continually add .1 as floats together for specified number of iterations
for i in range (0, max, 1):
floatSum = floatSum + toAddFloat
print(floatSum,(i + 1) / 10)
if (floatSum == (i + 1) / 10): # check if the addition is what it should be
floatCorrect += 1
i = 0
# continually add .1 as decimals together for specified number of iterations
for i in range (0, max, 1):
decSum = decSum + toAddDec
print(decSum, (i + 1) / 10)
print(decCorrect)
if (decSum == (i + 1) / 10): # check if the addition is what it should be
decCorrect += 1
print("\nFLOAT RESULTS:")
print("--------------")
print("Iterations: ", max, ", Added Number: ", toAddFloat, ", Calculated Sum: ", floatSum,
", Times Correct: ", floatCorrect, ", Correct Result: ", toAddFloat * max, ", Difference: ", (toAddFloat * max) - floatSum, "\n")
print("DECIMAL RESULTS:")
print("----------------")
print("Iterations: ", max, ", Added Number: ", toAddDec, ", Calculated Sum: ", decSum,
", Times Correct: ", decCorrect, ", Correct Result: ", toAddDec * max, ", Difference: ", (toAddDec * max) - decSum)
(部分) 输出:
FLOAT RESULTS:
--------------
Iterations: 100 , Added Number: 0.1 , Calculated Sum: 9.99999999999998 , Times Correct: 11 , Correct Result: 10.0 , Difference: 1.9539925233402755e-14
DECIMAL RESULTS:
----------------
Iterations: 100 , Added Number: 0.1 , Calculated Sum: 10.0 , Times Correct: 20 , Correct Result: 10.0 , Difference: 0.0
将此行更新为
if (decSum*10 == (i + 1)):
for python 除法给出浮点数结果。如果这个数字没有像它看起来的那样精确表示,它不会等于小数对应的数字。
在我的 Python 课程中,我正在尝试比较将多个浮点数相加与将多个小数相加的准确性。实验室的要求之一是计算有多少次将 .1 相加得到正确的和。我的代码中的所有内容都正常工作,除了我的计数器,用于计算 decimal 添加的正确数量。对于 100 次迭代,它只计算 20 次是正确的(每 5 次迭代),尽管我已经通过打印每次迭代证明它们都是正确的。为什么会发生这种情况,如何解决?谢谢。 (您可以在下面找到完整的代码以获得完整的上下文和输出。)
代码:
from decimal import *
floatSum = 0
decSum = 0
toAddFloat = .1
toAddDec = Decimal(".1")
max = 100 # number of iterations
floatCorrect = 0
decCorrect = 0
# continually add .1 as floats together for specified number of iterations
for i in range (0, max, 1):
floatSum = floatSum + toAddFloat
print(floatSum,(i + 1) / 10)
if (floatSum == (i + 1) / 10): # check if the addition is what it should be
floatCorrect += 1
i = 0
# continually add .1 as decimals together for specified number of iterations
for i in range (0, max, 1):
decSum = decSum + toAddDec
print(decSum, (i + 1) / 10)
print(decCorrect)
if (decSum == (i + 1) / 10): # check if the addition is what it should be
decCorrect += 1
print("\nFLOAT RESULTS:")
print("--------------")
print("Iterations: ", max, ", Added Number: ", toAddFloat, ", Calculated Sum: ", floatSum,
", Times Correct: ", floatCorrect, ", Correct Result: ", toAddFloat * max, ", Difference: ", (toAddFloat * max) - floatSum, "\n")
print("DECIMAL RESULTS:")
print("----------------")
print("Iterations: ", max, ", Added Number: ", toAddDec, ", Calculated Sum: ", decSum,
", Times Correct: ", decCorrect, ", Correct Result: ", toAddDec * max, ", Difference: ", (toAddDec * max) - decSum)
(部分) 输出:
FLOAT RESULTS:
--------------
Iterations: 100 , Added Number: 0.1 , Calculated Sum: 9.99999999999998 , Times Correct: 11 , Correct Result: 10.0 , Difference: 1.9539925233402755e-14
DECIMAL RESULTS:
----------------
Iterations: 100 , Added Number: 0.1 , Calculated Sum: 10.0 , Times Correct: 20 , Correct Result: 10.0 , Difference: 0.0
将此行更新为
if (decSum*10 == (i + 1)):
for python 除法给出浮点数结果。如果这个数字没有像它看起来的那样精确表示,它不会等于小数对应的数字。