Python 的舍入问题
可能的重复:
Python 浮点数舍入错误
我在 Python 中遇到舍入问题。如果我计算
32.50 * 0.19 = 6.1749999999999998
但这应该是 6.175。如果我将 6.1749999999999998 四舍五入到小数点后两位,它会正确显示 6.18。所以我可以忍受。
但如果我计算一下:
32.50 * 0.19 * 3 = 18.524999999999999
这应该是 18.525。如果我将值 18.524999999999999 四舍五入到小数点后两位,则显示为 18.52。
它应该显示 18.53。我做错了什么以及如何解决它?
Possible Duplicate:
Python rounding error with float numbers
I have a rounding Problem in Python. If i calculate
32.50 * 0.19 = 6.1749999999999998
But this should be 6.175. If i round 6.1749999999999998 with 2 decimal places it correctly shows 6.18. So i can live with that.
But if i calculate this:
32.50 * 0.19 * 3 = 18.524999999999999
This should be 18.525. If i round the value 18.524999999999999 with two decimal places it shows 18.52.
It should show me 18.53. What am i doing wrong and how can i fix it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
每个计算机科学家应该了解的浮点运算知识。
简而言之 - 您不应该依赖浮点数的精确值,因为它们在内存中的存储方式。
另请参阅有关它的 python 文档 - 浮点算术:问题和限制。它包含下一段:
What Every Computer Scientist Should Know About Floating-Point Arithmetic.
In short - you should not rely on precise values of float numbers because of the way they are stored in the memory.
See also python docs about it - Floating Point Arithmetic: Issues and Limitations. It contains the next passage:
如果您需要精确的算术,可以使用 decimal 模块:
If you need exact arithmetic, you could use the decimal module:
使用 python 中的 Decimal 模块进行精确的浮点算术
Use the Decimal Module from python to do accurate floating arithmatic
你没有做错任何事,这也不是 Python 的错。一些十进制数字无法精确地表示为二进制浮点数。
就像您不能将
1/3
写成十进制 (0.33333....
) 一样,您也不能将十进制0.1
写成二进制(0.0001100110011001100110011001100110011001100110011...
)。解决方案A:
使用
print 32.5 * 0.19
- 它会自动对结果进行四舍五入。解决方案B:
使用
十进制
模块,如果您确实需要这种精度,例如在使用货币值进行计算时。解决方案 C:
使用 Python 3.2 或 Python 2.7,它将在交互式会话中自动对结果进行舍入。
You're doing nothing wrong, and it isn't Python's fault either. Some decimal numbers just cannot be precisely represented as binary floats.
Just like you can't write
1/3
in decimal (0.33333....
), you can't write decimal0.1
in binary (0.0001100110011001100110011001100110011001100110011...
).Solution A:
Use
print 32.5 * 0.19
- it will automatically round the result.Solution B:
Use the
Decimal
module if you actually need this precision, for example when calculating with monetary values.Solution C:
Use Python 3.2 or Python 2.7 which will automatically round the result in an interactive session.
http://docs.python.org/library/functions.html#round
http://docs.python.org/library/functions.html#round
我不认为这是错误的。从 Python 解释器来看:
在这两种情况下,被舍入的数字都小于 5,因此向下舍入。 18.52 和 6.17。
这是正确的。
我不明白的一件事是为什么你得到 6.18,而我得到 6.17。我使用的是Python 3.2.2(最新版本)
I don't think that is wrong. Take this from the Python interpreter:
In both cases, the number being rounded was less than 5, so it rounded down. 18.52, and 6.17.
That is correct.
One thing I don't get is why you are getting 6.18, and I get 6.17. I am using Python 3.2.2 (the latest version)
你没有做错任何事。这是由于数字的内部表示形式造成的:
例如,尝试以下操作:
如果需要更高的精度,请使用十进制表示形式
You are not doing anything wrong. This is due to the internal representation of the numbers:
For example, try this:
If you need more precision use the decimal representation