Python整数除法产生浮点数

问题:Python整数除法产生浮点数

Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 2/2
1.0

这是故意的吗?我强烈记得以前的版本返回了int/int=int吗?我该怎么办,有没有新的分公司运营商,或者我必须始终选拔?

Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 2/2
1.0

Is this intended? I strongly remember earlier versions returning int/int=int? What should I do, is there a new division operator or must I always cast?


回答 0

看看PEP-238:更改除法运算符

//运算符将可用于明确要求楼层分割。

Take a look at PEP-238: Changing the Division Operator

The // operator will be available to request floor division unambiguously.


回答 1

糟糕,立即发现2//2

Oops, immediately found 2//2.


回答 2

希望它可以立即帮助某人。

Python 2.7和Python 3中除法运算符的行为

在Python 2.7中:默认情况下,除法运算符将返回整数输出。

将结果以1.0的两倍倍数除以 “除数或除数”

100/35 => 2 #(Expected is 2.857142857142857)
(100*1.0)/35 => 2.857142857142857
100/(35*1.0) => 2.857142857142857

在Python 3中

// => used for integer output
/ => used for double output

100/35 => 2.857142857142857
100//35 => 2
100.//35 => 2.0    # floating-point result if divsor or dividend real

Hope it might help someone instantly.

Behavior of Division Operator in Python 2.7 and Python 3

In Python 2.7: By default, division operator will return integer output.

to get the result in double multiple 1.0 to “dividend or divisor”

100/35 => 2 #(Expected is 2.857142857142857)
(100*1.0)/35 => 2.857142857142857
100/(35*1.0) => 2.857142857142857

In Python 3

// => used for integer output
/ => used for double output

100/35 => 2.857142857142857
100//35 => 2
100.//35 => 2.0    # floating-point result if divsor or dividend real

回答 3

接受的答案已经提到了PEP 238。我只想为那些对正在发生的事情感兴趣的人快速了解幕后情况,而无需阅读整个PEP。

Python将运算符+-,,*和)映射/到特殊函数,例如a + b相当于

a.__add__(b)

关于在Python 2分割,存在通过仅仅默认/映射到__div__,其结果是依赖于输入类型(例如intfloat)。

Python 2.2引入了__future__功能division,该功能通过以下方式更改了分割语义(PEP 238的TL; DR):

  • /__truediv__必须“返回除法的数学结果的合理近似值”的映射(来自PEP 238的引用)
  • //映射到__floordiv__,应返回的底数结果/

在Python 3.0中,PEP 238的更改成为默认行为,并且__div__Python对象模型中没有其他特殊方法。

如果要在Python 2和Python 3中使用相同的代码,请使用

from __future__ import division

并坚持到PEP的238个语义///

The accepted answer already mentions PEP 238. I just want to add a quick look behind the scenes for those interested in what’s going on without reading the whole PEP.

Python maps operators like +, -, * and / to special functions, such that e.g. a + b is equivalent to

a.__add__(b)

Regarding division in Python 2, there is by default only / which maps to __div__ and the result is dependent on the input types (e.g. int, float).

Python 2.2 introduced the __future__ feature division, which changed the division semantics the following way (TL;DR of PEP 238):

  • / maps to __truediv__ which must “return a reasonable approximation of the mathematical result of the division” (quote from PEP 238)
  • // maps to __floordiv__, which should return the floored result of /

With Python 3.0, the changes of PEP 238 became the default behaviour and there is no more special method __div__ in Python’s object model.

If you want to use the same code in Python 2 and Python 3 use

from __future__ import division

and stick to the PEP 238 semantics of / and //.


回答 4

根据Python3文档,python除以整数后,尽管预期为整数,但仍会生成浮点数。

对于仅打印整数,请使用floor division method。楼层除法将四舍五入并除去小数点。Represented by //

因此,使用2/2代替 2//2

__future__无论使用python2还是python3,也可以从中导入除法。

希望能帮助到你!

According to Python3 documentation,python when divided by integer,will generate float despite expected to be integer.

For exclusively printing integer,use floor division method. Floor division is rounding off zero and removing decimal point. Represented by //

Hence,instead of 2/2 ,use 2//2

You can also import division from __future__ irrespective of using python2 or python3.

Hope it helps!