简体   繁体   English

C#中的运算符优先级

[英]Operator precedence in C#

Is

(int)(int1 / (float)var2.Count() * 100)

equivalent to 相当于

(int)((int1 / (float)var2.Count()) * 100)

...and will it use floating point or integer division? ...它会使用浮点数还是整数除法?

Edit... if the answer is yes to the above, what is the advantage of performing a floating point division here? 编辑...如果上面的答案是肯定的,那么在这里执行浮点除法有什么好处?

/ and * have the same operator precedence, under §7.2.1 so the two results should be the same (using float rules). /*具有相同的运算符优先级,在§7.2.1下,因此两个结果应该相同(使用float规则)。

I, however, can't be bothered to learn precedence tables; 但是,我不能为学习优先表而烦恼; I just use brackets. 我只是使用括号。 Then it works in any language without needing to remember it. 然后它可以使用任何语言,而无需记住它。

Another important question is the rounding in the final (int) cast: do you expect that to be "up", "down" or "bankers"? 另一个重要的问题是在最终(int)演员阵容中的四舍五入:你是否期望成为“向上”,“向下”或“银行家”?

Precedence rules are really annoying to remember, so I too prefer to use brackets to disambiguate. 优先规则真的很难记住,所以我也更喜欢使用括号来消除歧义。 I try to follow the advice to "write code for people first, computers second". 我尝试遵循建议“先为人们编写代码,然后再为计算机编写代码”。 But, there's an interesting mnemonic (that I learned from Bruce Eckel's books) to remember (some of) the rules: " Ulcer Addicts Really Like CA lot ": 但是,有一个有趣的助记符(我从Bruce Eckel的书中学到的)要记住(一些)规则:“ Ulcer Addicts真的很像CA很多 ”:

Ulcer   - Unary (+, -, ++, --, !)
Addicts - Arithmetic (and shift) (+, -, *, /, %, <<, >>)
Really  - Relational (<, >, ==, <=, >=, !=)
Like    - Logical (and bitwise) (&&, ||, &, |, ^)
C       - Conditional ( ? : ) <- this is the conditional ternary operator
A lot   - Assignment ( =, *=, +=, ...)

It's not perfect, bitwise operators are squeezed in and we have to know that multiplication operators (*, /, %) take precedence over addition ones (+, -). 它并不完美,按位运算符被挤入,我们必须知道乘法运算符(*,/,%)优先于加法运算符(+, - )。

They are equivalent and it will use floating point division. 它们是等价的,它将使用浮点除法。 Even if the multiplication happened first, floating point division would be used since the int is divided by the result of float*int which is float. 即使乘法首先发生,也会使用浮点除法,因为int除以float * int的结果,即浮点数。

Edit: 编辑:

If the answer is yes to the above, what is the advantage of performing a floating point division here? 如果上面的答案是肯定的,那么在这里执行浮点除法有什么好处?

Is it an advantage? 这是一个优势吗? The first thing that you should be considering is whether or not it's correct since the result will be different. 你应该考虑的第一件事是它是否正确,因为结果会有所不同。 Judging by the code it seems you are trying to calculate some percentage. 从代码来看,你似乎在试图计算一些百分比。 When using integer divison, if "int1" is always smaller than var2.Count() the result will always be 0 which might not be what you want. 使用整数除法时,如果“int1”总是小于var2.Count(),则结果将始终为0,这可能不是您想要的。

I would say Yes, division and multiplication should go left-to-right. 我会说是的,除法和乘法应该是从左到右。 And thus it is a float division. 因此它是一个浮动部门。

PS: replace float with double wherever you can. PS:尽可能用double替换float

请参阅C#语言规范: 运算符优先级和关联性

It's the same. 一样的。 Both will use float division. 两者都将使用浮动分割。

yes both are equivalent. 是的,都是等价的。

Both will use floating point division. 两者都将使用浮点除法。

整数除法只会给出答案的整个部分,即3/2将为1而浮点除法将给出1.5

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

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