当我迭代列表并添加它们时,它给了我一个奇怪的总和
a= [8, 12, 11, 15, 12, 2, 16, 3, 6, 19]
mean_a = sum(a)/len(a)
b = list(map(lambda x: x-mean_a, a))
tot = 0
for i in b:
tot+=i
print(tot**2)
#它给了我这个奇怪的答案:1.262177448353619E-29
a= [8, 12, 11, 15, 12, 2, 16, 3, 6, 19]
mean_a = sum(a)/len(a)
b = list(map(lambda x: x-mean_a, a))
tot = 0
for i in b:
tot+=i
print(tot**2)
#it gave me this weird answer: 1.262177448353619e-29
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您期待0?如果是这样,这只是A floating-precision Precision Orror ,由于浮点数是由基本2个分数中的计算机处理的。答案有效0;您可以到达一定数量的小数点位置来获取此答案。
浮点误差发生两次;当您创建
b
时,当您总结其值时。I'm assuming you're expecting 0? If so, this is just another case of a floating-point precision error, as floating point numbers are processed by computers in base 2 fractions. The answer is effectively 0; you can round to a certain number of decimal places to get this answer.
The floating point error occurs twice; when you create
b
, and when you sum its values.