【问题标题】:python floating number [duplicate]python浮点数[重复]
【发布时间】:2011-02-28 11:49:33
【问题描述】:

我有点困惑为什么python在这种情况下要添加一些额外的十进制数,请帮忙解释一下

>>> mylist = ["list item 1", 2, 3.14]
>>> print mylist ['list item 1', 2, 3.1400000000000001]

【问题讨论】:

标签: python floating-point numerical floating-accuracy


【解决方案1】:

浮点数是近似值,它们不能精确存储十进制数。因为它们试图仅用 64 位来表示非常大范围的数字,所以它们必须在某种程度上接近。

意识到这一点非常重要,因为它会导致一些奇怪的副作用。例如,您可能非常合理地认为 10 手 0.1 的总和将是 1.0。虽然这看起来合乎逻辑,但在浮点方面也是错误的:

>>> f = 0.0
>>> for _ in range (10):
...  f += 0.1
...
>>> print f == 1.0
False
>>> f
0.99999999999999989
>>> str(f)
1.0

你可能会认为n / m * m == n。再一次,浮点世界不同意:

>>> (1.0 / 103.0) * 103.0
0.99999999999999989

或者同样奇怪的是,人们可能会认为对于所有nn + 1 != n。在浮点领域,数字就不是这样工作的:

>>> 10.0**200
9.9999999999999997e+199
>>> 10.0**200 == 10.0**200 + 1
True
# How much do we have to add to 10.0**200 before its 
# floating point representation changes?
>>> 10.0**200 == 10.0**200 + 10.0**183
True
>>> 10.0**200 == 10.0**200 + 10.0**184
False

请参阅What every computer scientist should know about floating point numbers 以获得对问题的出色总结。

如果您需要精确的十进制表示,请查看 decimal 模块,它是自 2.4 以来 Python 标准库的一部分。它允许您指定有效数字的数量。缺点是,它比浮点慢得多,因为浮点运算是在硬件中实现的,而十进制运算纯粹是在软件中实现的。它也有其自身的不精确问题,但如果您需要精确表示十进制数(例如,对于金融应用程序),它是理想的。

例如:

>>> 3.14
3.1400000000000001
>>> import decimal
>>> decimal.Decimal('3.14')
>>> print decimal.Decimal('3.14')
3.14
# change the precision:
>>> decimal.getcontext().prec = 6
>>> decimal.Decimal(1) / decimal.Decimal(7)
Decimal('0.142857')
>>> decimal.getcontext().prec = 28
>>> decimal.Decimal(1) / decimal.Decimal(7)
Decimal('0.1428571428571428571428571429')

【讨论】:

  • >>> from decimal import * Traceback (innermost last): File "", line 1, in ? ImportError: No module named decimal 当我尝试导入十进制模块时出现错误,我在哪里可以得到它?听起来可能很愚蠢,但我是 python 新手。
  • 十进制在 python 2.4 及更高版本中可用。
  • @zhack,我们是一个开发者社区,免费回答问题。不要再说你的问题很愚蠢。
【解决方案2】:

如前所述,浮点数是一种近似值。

如果你想要精确,你可以使用小数(这是一种精确的表示): http://docs.python.org/library/decimal.html

a = [1.5, 1.49999]
a
[1.5, 1.4999899999999999]

from decimal import Decimal
b = [1.5, Decimal('1.4999')]
b
[1.5, Decimal('1.4999')]

【讨论】:

  • 请记住,许多非金融应用程序不需要精确性。 sinlnsqrt 等函数不会返回任何基数的准确答案。
【解决方案3】:

值得注意的是,Python 3.1 有一个新的浮点输出例程,它以预期的方式对其进行舍入(它也被向后移植到 Python 2.7):

Python 3.1 (r31:73572, Aug 15 2009, 17:12:41) 
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = [3.14]
>>> print(a)
[3.14]

来自What's New in Python 3.1 文档:

Python 现在使用 David Gay 的算法来寻找不会改变其值的最短浮点表示。这应该有助于减轻围绕二进制浮点数的一些混淆。

使用像 1.1 这样的数字很容易看出其重要性,它在二进制浮点中没有精确的等价物。由于没有精确的等价物,像float('1.1') 这样的表达式计算为最接近的可表示值,即十六进制的0x1.199999999999ap+0 或十进制的1.100000000000000088817841970012523233890533447265625。这个最接近的值曾经并且仍然用于后续的浮点计算。

【讨论】:

    【解决方案4】:

    我们可以通过这个命令修复它:

    >>> x = 1.2 - 1.0
    >>> x
    0.19999999999999996
    >>> y = float(str(x))
    >>> y
    0.2
    

    我添加了来自@mark 的答案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-10
      • 2013-08-10
      • 2017-08-16
      • 1970-01-01
      • 1970-01-01
      • 2015-04-02
      • 2018-04-29
      • 1970-01-01
      相关资源
      最近更新 更多