简体   繁体   English

带负数的楼层划分

[英]Floor division with negative number

The expression 6 // 4 yields 1 , where floor division produces the whole number after dividing a number.表达式6 // 4产生1 ,其中地板除法在除以数字后产生整数。

But with a negative number, why does -6 // 4 return -2 ?但是对于负数,为什么-6 // 4返回-2

The // operator explicitly floors the result. //运算符显式对结果进行分层。 Quoting the Binary arithmetic operations documentation :引用二进制算术运算文档

the result is that of mathematical division with the 'floor' function applied to the result.结果是将“floor”函数应用于结果的数学除法。

Flooring is not the same thing as rounding to 0; flooring 与四舍五入为 0 不同; flooring always moves to the lower integer value . flooring 总是移动到较低的整数值 See the math.floor() function :查看math.floor()函数

Return the floor of x , the largest integer less than or equal to x .返回x的下限,小于或等于x的最大整数。

For -6 // 4 , first the result of -6 / 4 is calculated, so -1.5 .对于-6 // 4 ,首先计算-6 / 4的结果,因此-1.5 Flooring then moves to the lower integer value, so -2 . Flooring 然后移动到较低的整数值,所以-2

If you want to round towards zero instead, you'll have to do so explicitly;如果您想向零舍入,则必须明确这样做; you could do this with the int() function on true division:你可以在真正的除法上用int()函数做到这一点:

>>> int(-6 / 4)
-1

int() removes the decimal portion, so always rounds towards zero instead. int()删除小数部分,因此总是向零舍入。

Floor division will also round down to the next lowest number, not the next lowest absolute value.楼层划分也将向下舍入到下一个最低数字,而不是下一个最低绝对值。

6 // 4 = 1.5 , which rounds down to 1, and up to 2. 6 // 4 = 1.5 ,向下舍入为 1,向上舍入为 2。

-6 // 4 = -1.5 , which rounds down to -2, and up to -1. -6 // 4 = -1.5 ,向下-6 // 4 = -1.5到 -2,向上舍入到 -1。

// in Python is a "floor division" operator. //在 Python 中是一个“地板除法”运算符。 That means that the result of such division is the floor of the result of regular division (performed with / operator).这意味着这种除法的结果是常规除法结果的下限(使用 / 运算符执行)。

The floor of the given number is the biggest integer smaller than the this number.给定数的下限是小于这个数的最大整数。 For example例如

7 / 2 = 3.5 so 7 // 2 = floor of 3.5 = 3.

For negative numbers it is less intuitive: -7 / 2 = -3.5, so -7 // 2 = floor of -3.5 = -4 .对于负数,它不太直观: -7 / 2 = -3.5,所以-7 // 2 = floor of -3.5 = -4 Similarly -1 // 10 = floor of -0.1 = -1.同样-1 // 10 = floor of -0.1 = -1.

// is defined to do the same thing as math.floor() : return the largest integer value less than or equal to the floating-point result. //被定义为做与math.floor()相同的事情:返回小于或等于浮点结果的最大整数值。 Zero is not less than or equal to -0.1.

A useful way to understand why floor division // yields the results it does for negative values is see this as complimenting the modulo, or remainder, % operator.理解为什么地板除法 // 产生负值结果的一种有用方法是将其视为对模或余数 % 运算符的补充。

5/3  is equivalent to 1 remainder 2 

ie IE

5//3 = 1
5%3 = 2

But

-5/3 = -2
-5%3 = 1

Or或者

-2 + 1/3rd which is -1.6667 (ish)

It can seem strange, but it ensures results such as -2,-2,-2,-1,-1,-1,0,0,0,1,1,1,2,2,2,3,3,3 etc. when generating sequences.这看起来很奇怪,但它确保了诸如-2,-2,-2,-1,-1,-1,0,0,0,1,1,1,2,2,2,3,3,3等生成序列时。

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

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