简体   繁体   English

3/2和-3/2之间有什么区别?

[英]What is the difference between 3/2 and -3/2?

I am beginner in programming and Python. 我是编程和Python的初学者。 I am doing some simple maths operations. 我正在做一些简单的数学运算。 So 3/2 in Python interpreter gives 1 as we know. 因此,我们知道,Python解释器中的3/2给出了1 But -3/2 gives -2 . 但是-3/2-2 Can you point out the difference here? 你能指出这里的区别吗?

In Python 2, / performs integer division. 在Python 2中, /执行整数除法。 What this means is that the result, if it is not an integer, is rounded down to the next integer value. 这意味着结果(如果它不是整数) 向下舍入到下一个整数值。 When the value is negative, this naturally rounds to a greater-magnitude negative number. 当值为负时,这自然会转向更大幅度的负数。

Intuitively, the result of integer division is simply the mathematical floor of the result of float division. 直观地说,整数除法的结果只是浮点除法结果的数学平台 For this reason, integer division is also commonly referred to as floor division . 因此,整数除法通常也称为平面划分

floor(1.5)  # Returns 1.0
floor(-1.5)  # Returns -2.0

It's possible to alter this behavior in Python 2 by putting from __future__ import division at the top of your module. 可以通过在模块顶部放置from __future__ import division来改变Python 2中的这种行为。 This import will make the / operator indicate only true division (float division), and enable explicit floor division (integer division) with the // operator. 此导入将使/运算符仅指示真正的除法(浮点除法),并使用//运算符启用显式换层(整数除法)。 These conventions are the standard in Python 3. 这些约定是Python 3中的标准。

from __future__ import division

print(3/2)  # 1.5
print(3//2)  # 1

As @Dunes notes in the comments, it's worth noting that - has a higher precedence than / , and therefore -3/2 is equivalent to (-3)/2 rather than -(3/2) . 正如@Dunes在评论中指出的那样,值得注意的是-具有比/更高的优先级,因此-3/2相当于(-3)/2而不是-(3/2) If the division were applied first, the result would indeed be -1 . 如果首先应用除法,则结果确实为-1

-3/2 == -1.5 , floor(-1.5)  = -2

同样

 3/2 == 1.5 , floor(1.5)  = 1

Python has two division operators. Python有两个除法运算符。

  1. /

  2. //

Here, // will always round the result to the nearest integer (irrespective of the type of operands). 这里, //将始终将结果舍入到最接近的整数(与操作数的类型无关)。 This is called floor division . 这称为地板划分 But / will round to the nearest integer, if both the operands are integers wheres it does the actual division if either of the operands is a float. 但是/将四舍五入到最接近的整数,如果两个操作数都是整数,那么如果其中一个操作数是浮点数,它就会进行实际除法。

The difference can be clearly understood with this example, 通过这个例子可以清楚地理解这种差异,

>>> 11/4
2
>>> 11.0/4
2.75
>>> 11//4
2
>>> 11.0//4.0
2.0

Quoting from Python Documentation on floor division , 楼层划分的 Python文档引用,

Mathematical division that rounds down to nearest integer . 向下舍入到最接近整数的数学除法。 The floor division operator is // . 地面划分运营商是// For example, the expression 11 // 4 evaluates to 2 in contrast to the 2.7 5 returned by float true division. 例如,表达式11 // 4计算结果为2 ,与float true division返回的2.7 5相比。 Note that (-11) // 4 is -3 because that is -2.75 rounded downward . 注意(-11) // 4-3因为向下舍入-2.75 See PEP 238 . PEP 238

The last line in the quoted text would be the answer to your actual question. 引用文本中的最后一行将是您实际问题的答案。

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

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