C# 中的运算符优先级

发布于 2024-09-10 14:25:21 字数 222 浏览 4 评论 0原文

相当于

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

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

...并且它会使用浮点还是整数除法

编辑...如果上述答案是肯定的,那么在这里执行浮点除法有什么好处?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

吲‖鸣 2024-09-17 14:25:21

根据第 7.2.1 节,/* 具有相同的运算符优先级,因此两个结果应该相同(使用 float 规则)。

然而,我懒得去学习优先级表;我只使用括号。然后它可以用任何语言工作而无需记住它。

另一个重要问题是最终 (int) 转换中的四舍五入:您希望是“向上”、“向下”还是“银行家”?

/ and * have the same operator precedence, under §7.2.1 so the two results should be the same (using float rules).

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"?

溺孤伤于心 2024-09-17 14:25:21

优先级规则真的很难记住,所以我也更喜欢使用括号来消除歧义。我尝试遵循“首先为人编写代码,其次为计算机编写代码”的建议。但是,有一个有趣的助记符(我从 Bruce Eckel 的书中学到的)来记住(一些)规则:“溃疡成瘾者真的很喜欢 CA 很多”:

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

它并不完美,按位运算符被挤进去并且我们必须知道乘法运算符(*、/、%)优先于加法运算符(+、-)。

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 C A lot":

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 (+, -).

清泪尽 2024-09-17 14:25:21

它们是等价的,并且将使用浮点除法。即使首先发生乘法,也会使用浮点除法,因为 int 除以 float*int 的结果是 float。

编辑:

如果上述答案是肯定的,那么在这里执行浮点除法有什么好处?

这是一个优势吗?您应该考虑的第一件事是它是否正确,因为结果会有所不同。从代码来看,您似乎正在尝试计算一些百分比。使用整数除法时,如果“int1”始终小于 var2.Count(),则结果将始终为 0,这可能不是您想要的。

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.

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.

遮了一弯 2024-09-17 14:25:21

我会说是的,除法和乘法应该从左到右。因此它是一个浮动除法。

PS:尽可能将 float 替换为 double

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.

行至春深 2024-09-17 14:25:21

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

See the C# Language Specification: Operator precedence and associativity.

超可爱的懒熊 2024-09-17 14:25:21

是一样的。两者都将使用浮点除法。

It's the same. Both will use float division.

來不及說愛妳 2024-09-17 14:25:21

是的,两者是等价的。

两者都将使用浮点除法。

yes both are equivalent.

Both will use floating point division.

梦里寻她 2024-09-17 14:25:21

整数除法只会返回答案的整个部分,即 3/2 将是 1,而浮点数除法将返回 1.5

Integer division will only give back the whole part of the answer i.e. 3/2 will be 1 whereas float division will give you 1.5

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文