可交换数学运算的顺序 [英] Order of commutative mathematical operations
问题描述
我遇到了一个奇怪的问题(在阅读粗略的代码时自己问).让我们看一下表达式:
I've into curious question (asking it myself while reading a crude piece of code). Let's look at expression:
double a = c*d*e*2/3*f;
其中c,d,e,f是类型为double
的初始化变量.标准是否保证将其视为c*d*e*2
(双重结果),然后除以3
,再乘以f
(或某些类似行为).显然,不希望将2/3计算为0.
where c, d, e, f are initialized variables of type double
. Does standard guarantee that it would be treated as c*d*e*2
(double result) then divided by 3
and multiplied by f
(or some similar behavior). Obviously, 2/3 being calculated to 0 is undesirable.
标准的哪个段落对此进行了定义?
Which paragraph of standard defines that?
推荐答案
可以根据通常的数学规则对运算符进行重新组合 仅在运算符真正具有关联性或可交换性的情况下.
Operators can be regrouped according to the usual mathematical rules only where the operators really are associative or commutative.
MDAS的数学规则是从左到右(考虑运算符的关联性和优先级).因此,其评估如下:
Mathematical rule for MDAS is from left to right (considering the associativity and precedence of operators). So it is evaluated as follows:
(((((c * d) * e) * 2) / 3) * f)
这篇关于可交换数学运算的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!