python float 精度_python精度问题?

python有关浮点数的误差是这么说的:

On a typical machine running Python, there are 53 bits of precision available for a Python float, so the value stored internally when you enter the decimal number 0.1 is the binary fraction

见这里 https://docs.python.org/2/tutorial/floatingpoint.html

你可以试试Decimal, 估计底层没有完全实现一个完整的大数算法,实际上,我觉得这个并不完全是Python规范的内容,而是取决于平台和实现,你可以看下当前平台的float大小

import sys

print sys.float_info

sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)

python手册里如是说

Floating point numbers are usually implemented using double in C; information about the precision and

internal representation of floating point numbers for the machine on which your program is running is available in

sys.float_info.

上面说的这些都是CPython的实现,而这个在Jython中,又是另一番定义:

Jython 2.5.3 (2.5:c56500f08d34+, Aug 13 2012, 14:48:36)

[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_40

Type "help", "copyright", "credits" or "license" for more information.

>>> '%.100f' % (9999.0/10000)

'0.9999000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'

结论: 这个取决于平台实现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值