python 3楼除法并不总是导致int [英] python 3 floor division doesn't always result in an int

查看:94
本文介绍了python 3楼除法并不总是导致int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python3中使用地板分割时(可能还有在import __future__中使用python2):

When using floor division in python3 (and probably also python2 with import __future__):

>>> 2//2
1

输出是预期的整数.但是,只要一个操作数是浮点数,结果就是浮点数

the output is an integer as expected. But as soon as one operand is a float, you get a float as result

>>> 2.0//2.0
1.0
>>> 2.0//2
1.0
>>> 2//2.0
1.0

我想这是故意的,但实际上我不明白,为什么应该这样.通过始终产生整数的运算而使用未确定的数据类型的设计概念是什么?

I guess this is intended, but actually I don't understand, why it is supposed to be like this. What is the design concept of using a not previously determined data type as a result of an operation that always yields an integer?

真正广泛的搜索所给我的最好的帮助(来自 PEP 238 )

The best a really extensive search gave me (from PEP 238)

楼层划分的语义学

Semantics of Floor Division

地板除法将在所有Python数字中实现 类型,并且将具有

Floor division will be implemented in all the Python numeric types, and will have the semantics of

   a // b == floor(a/b)

除了结果类型将是 和b在操作之前被强制.

except that the result type will be the common type into which a and b are coerced before the operation.

特别是:

- If a and b are of the same type, a//b will be of that type too.
- If the inputs are of different types, they are first coerced   
  to a common type using the same rules used for all other arithmetic operators.

尤其是:

- if a and b are both ints or longs, the result has the same type and value as
  for classic division on these types (including the case of mixed input types;
  `int//long` and `long//int` will both return a long).
- For floating point inputs, the result is a float.
  For example:  `3.5//2.0 == 1.0`
- For complex numbers, // raises an exception, since floor() of a   complex number is not allowed.  
- For user-defined classes and extension types, all semantics are up  to the implementation of the class or type.

但这仍然不能解释为什么这样的行为.

But this still doesn't explain WHY the behavior is implemented like this.

推荐答案

一个可能的优点如下:如果某个操作的输入是float,那么通常最有用的输出类型是float,因为程序正在执行浮点计算.同样,如果操作的输入是整数(int s或long s),则通常最有用的输出类型是整数.

One possible advantage can be the following: If the inputs of an operation are floats, then usually the most useful output type is a float, because the program is doing floating point calculations. Similarly, if the inputs of an operation are integers (ints or longs), then usually the most useful output type is an integer.

一个相关的令人惊讶的数据点:

A related surprising data point:

>>> str(int(123e300 // 10.0))
'12300000000000000348405169443457756499452463917650245579212965288916278422109198944984236481408634018703901759913201583616648277756338685989513894763895354330869046350917957229381143786183918719192956157930593465276658607709014541611368487360619735051905095032755082564499801643679232993692080863707136'

这是令人惊讶的,因为在结尾处自然会期望很多0是很自然的.由于float类型的精度有限,您还会得到其他数字.

It's surprising, because it's natural to expect lots of 0s at the end. You get other digits because of the limited precision of the float type.

因此,通过返回float//表示输出可能不准确.

So by returning a float, // indicates that the output can be inaccurate.

这篇关于python 3楼除法并不总是导致int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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