Python: 探究py2与py3除法的区别

起因

在用python2解释器运行python3代码的时候,出现了bug。debug后发现是因为python3中的/ 原本表示 精确除法,却被python2解释器解释成了 地板除,最终导致了错误。因此我上网查阅了相关资料,并总结如下表:

总结

version///
py2整数除法时为地板除,浮点数除法时为精确除地板除
py3精确除法地板除

Test

x = y = 10
x /= 2    # 精确除
y //= 2    # 地板除
print(x, type(x))    # 5.0 <class 'float'>
print(y, type(y))    # 5 <class 'int'>
user@user:~$ python
Python 2.7.13 |Anaconda 2.4.1 (64-bit)| (default, Dec 20 2016, 23:09:15) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> 9/2
4
>>> 9.0/2
4.5
>>> 9//2
4
>>> 9.0//2
4.0
>>> float(9)/2
4.5
>>> from __future__ import division
>>> 9/2
4.5
>>> 
>>>
[3]+  Stopped                 python
>>>
>>>
user@user:~$ python3
Python 3.4.3 (default, Nov 17 2016, 01:08:31) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 9/2
4.5
>>> 9//2
4
>>> 9.0//2
4.0
>>> 



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值