1.在python2 中导入未来的支持的语言特征中division(精确除法),即from __future__ import division ,当我们在程序中没有导入该特征时,"/"操作符执行的只能是整除,也就是取整数,只有当我们导入division(精确算法)以后,"/"执行的才是精确算法。
如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #python 2.7.6 Python 2.7 . 6 (default, Nov 10 2013 , 19 : 24 : 18 ) [MSC v. 1500 32 bit (Intel)] on win32 Type "copyright" , "credits" or "license()" for more information. #导入前 >>> 1 / 2 0 >>> 10 / 3 3 #导入后 >>> from __future__ import division >>> 1 / 2 0.5 >>> 10 / 3 3.3333333333333335 #导入后如果要去整数,加'//' >>> 10 / / 3 3 |
2.但是在python3中已经支持了精确算法,所以无需再导入division(精确算法):
如:
1 2 3 4 5 6 7 8 9 10 | #python3.4.4 Python 3.4 . 4 (v3. 4.4 : 737efcadf5a6 , Dec 20 2015 , 20 : 20 : 57 ) [MSC v. 1600 64 bit (AMD64)] on win32 Type "copyright" , "credits" or "license()" for more information. >>> 1 / 2 0.5 >>> 10 / 3 3.3333333333333335 #如果需要取整数,加'//' >>> 10 / / 3 3 |