简体   繁体   English

python3使用除法和地板除法产生两种不同的结果

[英]python3 is generating two different results using division and floor division

When using the normal division使用普通除法时

9999999999999999 / 2 9999999999999999 / 2

returned value: 5000000000000000.0返回值:5000000000000000.0

and when using the floor division以及使用地板分割时

9999999999999999 // 2 9999999999999999 // 2

returned value: 4999999999999999返回值:4999999999999999

I am using python 3.8.1 can anyone explain this?我正在使用 python 3.8.1 谁能解释一下? Thanks in advance提前致谢

The / operator on int operands uses float arithmetic. int操作数上的/运算符使用float运算。 Assuming an IEEE 754 compatible system, a float has 53 bits of precision.假设 IEEE 754 兼容系统, float具有 53 位精度。 Thus, it cannot exactly represent 9999999999999999, which is a 54-bit number.因此,它不能准确地表示 9999999999999999,它是一个 54 位的数字。 So this value gets rounded up to 1e+16, and half of this is 5000000000000000.0.所以这个值被四舍五入到 1e+16,其中一半是 5000000000000000.0。

The // operator on int operands uses int arithmetic. int操作数上的//运算符使用int算术。 The Python 3 int type has unlimited range, so can represent 9999999999999999 exactly. Python 3 int类型具有无限范围,因此可以准确表示 9999999999999999。 Halving this number gives 4999999999999999 (with a discarded remainder of 1).将此数字减半得到 4999999999999999(丢弃的余数为 1)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM