简体   繁体   English

运营商优先级分区和分区

[英]operator precedence of floor division and division

I have trouble understanding why python returns different results for these 2 statements: 我无法理解为什么python会为这两个语句返回不同的结果:

-1 // 3/4 and -1 // 0.75 -1 // 3/4-1 // 0.75

The first one returns -0.25 and the second on returns -2 . 第一个返回-0.25 ,第二个返回-2

The way i understand it, the / operator is executed before // , thus those 2 statements should have the same result. 我理解它的方式, /运算符在//之前执行,因此这两个语句应该具有相同的结果。

edit: I was referring to a document provided by my university. 编辑:我指的是我大学提供的一份文件。 I misinterpreted that. 我误解了那个。 Official python documentation proves me wrong. 官方python文档证明我错了。 Thanks for all the quick answers. 感谢所有快速解答。

The / and // operators have the same precedence according to the documentation so they are evaluated from left to right when used in the same expression. 根据文档///运算符具有相同的优先级,因此在同一表达式中使用时,它们将从左到右进行求值。 -1 // 3/4 is therefore equivalent to (-1 // 3)/4 rather than -1 // (3/4) . -1 // 3/4因此等效于(-1 // 3)/4而不是-1 // (3/4)

The way i understand it, the / operator is executed before // , thus those 2 statements should have the same result. 我理解它的方式, /运算符在//之前执行,因此这两个语句应该具有相同的结果。

Your understanding is incorrect. 你的理解是不正确的。 / and // have the same precedence and have left associativity, meaning that Python performs the leftmost operation first - in your case, the / . ///具有相同的优先级并且具有左关联性,这意味着Python首先执行最左边的操作 - 在您的情况下, /

The Expressions documentation has a section about Operator Precedence . 表达式文档有一个关于运算符优先级的部分。 Operators in the same box have the same precedence. 同一个框中的运算符具有相同的优先级。

Thus, the table tells you that // and / have equal precedence, so 因此,该表告诉您///具有相同的优先级,所以

-1 // 3/4 parses as -1 // 3/4解析为

>>> (-1//3)/4
>>> -0.25

no, they have the same precedence, so they're evaluated from left to right. 不,它们具有相同的优先级,因此它们从左到右进行评估。

-1//3 is rounded (to the least integer) integer division, so you get -1 divided by 4 : 0.25 -1//3舍入(到最小整数)整数除法,因此得到-1除以40.25

When you have doubts, it doesn't cost much to throw in a couple of parentheses. 当你有疑虑时,抛出几个括号并不会花费太多。

Think of these from an order of operations standpoint: 从操作顺序的角度考虑这些:

-1 // 3/4

This will perform -1 "floor" 3 , which yields -1 , which then divided by 4 yields -0.25 . 这将执行-1 “floor” 3 ,其产生-1 ,然后除以4得到-0.25

Whereas: 鉴于:

-1 // 0.75

This will simply "floor" the operation straight away and yield -2.0 . 这将简单地“平铺”操作并产生-2.0

According to documentation, Multiplication * , matrix multiplication @ , division / , floor division // , remainder % all have same precedence. 根据文档, Multiplication *matrix multiplication @division /floor division //remainder %都具有相同的优先级。

When two operators have the same precedence, associativity helps to determine the order of operations. 当两个运算符具有相同的优先级时,关联性有助于确定操作的顺序。

Now regarding your question; 现在关于你的问题; both / and // have same precedence, and if both of them are present in an expression, left one is evaluated first based on left-to-right associativity . ///具有相同的优先级,如果它们都存在于表达式中,则首先根据从左到右的关联性来评估左边的一个。

// is essentially an operator for flooring division. //本质上是地板部门的运营商。

So 1 // 0.75 is essentially flooring 1.333 which is 1 所以1 // 0.75基本上是地板1.333,即1

-1 // 0.75 is essentially flooring -1.333 which is -2. -1 // 0.75基本上是地板-1.333,即-2。

Hope this makes sense. 希望这是有道理的。

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

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