python浮点数 [英] python floating number

查看:161
本文介绍了python浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 >>>我很困惑为什么python会在这种情况下增加一些额外的十进制数;> mylist = [list item 1,2,3.14] 
>>> print mylist ['list item 1',2,3.1400000000000001]


解决方案

<浮点数是一个近似值,它们不能精确地存储十进制数。因为他们试图用64位代表一个非常大的数字范围,所以他们必须在一定程度上接近。

意识到这一点非常重要,因为它导致一些奇怪的副作用。例如,你可能非常合理地认为,十个手数 0.1 的总和应该是 1.0 。虽然这看起来合乎逻辑,但浮点数也是错的:

 >>> f = 0.0 
>>>对于范围(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

或者也许同样奇怪,对于所有 n n + 1!= n 。在浮点土地上,数字并不是这样的:

 >>> 10.0 ** 200 
9.9999999999999997e + 199
>>> 10.0 ** 200 == 10.0 ** 200 + 1
True
#$
#浮点表示发生变化之前,我们必须添加多少到10.0 ** 200?
>>> 10.0 ** 200 == 10.0 ** 200 + 10.0 ** 183
True
>>> 10.0 ** 200 == 10.0 ** 200 + 10.0 ** 184
False

请参阅每个计算机科学家应该知道的关于浮点数的问题如果您需要精确的十进制表示,请查看十进制模块,自2.4以来的Python标准库的一部分。它允许你指定有效数字的数量。不足之处在于,它比浮点要慢得多,因为浮点运算是用硬件实现的,而十进制运算纯粹是用软件来实现的。它也有它自己的不准确的问题,但是如果你需要十进制数的确切表示(例如对于金融应用程序),那就是理想的选择。

例如:

 >>> 3.14 
3.1400000000000001
>>>进口十进制
>>> decimal.Decimal('3.14')
>>> print decimal.Decimal('3.14')
3.14
#改变精度:
>>> decimal.getcontext()。prec = 6
>>> decimal.Decimal(1)/ decimal.Decimal(7)
十进制('0.142857')
>>> decimal.getcontext()。prec = 28
>>> decimal.Decimal(1)/ decimal.Decimal(7)
十进制('0.1428571428571428571428571429')


i am kind of confused why python add some additional decimal number in this case, please help to explain

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

解决方案

Floating point numbers are an approximation, they cannot store decimal numbers exactly. Because they try to represent a very large range of numbers in only 64 bits, they must approximate to some extent.

It is very important to be aware of this, because it results in some weird side-effects. For example, you might very reasonably think that the sum of ten lots of 0.1 would be 1.0. While this seems logical, it is also wrong when it comes to floating point:

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

You might think that n / m * m == n. Once again, floating-point world disagrees:

>>> (1.0 / 103.0) * 103.0
0.99999999999999989

Or perhaps just as strangely, one might think that for all n, n + 1 != n. In floating point land, numbers just don't work like this:

>>> 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

See What every computer scientist should know about floating point numbers for an excellent summary of the issues.

If you need exact decimal representation, check out the decimal module, part of the python standard library since 2.4. It allows you to specify the number of significant figures. The downside is, it is much slower than floating point, because floating point operations are implemented in hardware whereas decimal operations happen purely in software. It also has its own imprecision issues, but if you need exact representation of decimal numbers (e.g. for a financial application) it's ideal.

For example:

>>> 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')

这篇关于python浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆