“BODMAS”这个词是什么意思?

时间:2022-09-26 16:55:23

What is BODMAS and why is it useful in programming?

什么是BODMAS,为什么它在编程中有用?

7 个解决方案

#1


20  

http://www.easymaths.com/What_on_earth_is_Bodmas.htm:

What do you think the answer to 2 + 3 x 5 is?

您认为2 + 3 x 5的答案是什么?

Is it (2 + 3) x 5 = 5 x 5 = 25 ?

是(2 + 3)x 5 = 5 x 5 = 25?

or 2 + (3 x 5) = 2 + 15 = 17 ?

或2 +(3 x 5)= 2 + 15 = 17?

BODMAS can come to the rescue and give us rules to follow so that we always get the right answer:

BODMAS可以拯救并给我们遵守规则,以便我们始终得到正确的答案:

(B)rackets (O)rder (D)ivision (M)ultiplication (A)ddition (S)ubtraction

(B)球拍(O)rder(D)ivision(M)ultiplication(A)ddition(S)ubtraction

According to BODMAS, multiplication should always be done before addition, therefore 17 is actually the correct answer according to BODMAS and will also be the answer which your calculator will give if you type in 2 + 3 x 5 .

根据BODMAS,乘法应该总是在加法之前完成,因此根据BODMAS,17实际上是正确的答案,并且如果你输入2 + 3 x 5,也将是你的计算器给出的答案。

Why it is useful in programming? No idea, but i assume it's because you can get rid of some brackets? I am a quite defensive programmer, so my lines can look like this:

为什么它在编程中有用?不知道,但我认为这是因为你可以摆脱一些括号?我是一个非常防守的程序员,所以我的线条看起来像这样:

result = (((i + 4) - (a + b)) * MAGIC_NUMBER) - ANOTHER_MAGIC_NUMBER;

with BODMAS you can make this a bit clearer:

使用BODMAS,您可以更清楚地说明这一点:

result = (i + 4 - (a + b)) * MAGIC_NUMBER - ANOTHER_MAGIC_NUMBER;

I think i'd still use the first variant - more brackets, but that way i do not have to learn yet another rule and i run into less risk of forgetting it and causing those weird hard to debug errors?

我想我仍然会使用第一个变体 - 更多括号,但是这样我不需要学习另一个规则,并且我会忘记忘记它并导致那些奇怪的难以调试错误的风险?

Just guessing at that part though.

只是猜测那个部分。

Mike Stone EDIT: Fixed math as Gaius points out

Mike Stone编辑:Gaius指出,修正了数学问题

#2


8  

Another version of this (in middle school) was "Please Excuse My Dear Aunt Sally".

另一个版本(在中学)是“请原谅我亲爱的莎莉阿姨”。

  • Parentheses
  • Exponents
  • Multiplication
  • Division
  • Addition
  • Subtraction

The mnemonic device was helpful in school, and still useful in programming today.

助记符设备在学校很有用,并且在今天的编程中仍然有用。

#3


5  

Order of operations in an expression, such as:

表达式中的操作顺序,例如:

foo * (bar + baz^2 / foo) 
  • Brackets first
  • Orders (ie Powers and Square Roots, etc.)
  • 订单(即权力和平方根等)

  • Division and Multiplication (left-to-right)
  • 除法和乘法(从左到右)

  • Addition and Subtraction (left-to-right)
  • 加法和减法(从左到右)

source: http://www.mathsisfun.com/operation-order-bodmas.html

#4


5  

I don't have the power to edit @Michael Stum's answer, but it's not quite correct. He reduces

我没有权力编辑@Michael Stum的答案,但这不太正确。他减少了

(i + 4) - (a + b)

to

(i + 4 - a + b)

They are not equivalent. The best reduction I can get for the whole expression is

它们并不等同。对于整个表达,我可以获得的最佳减少是

((i + 4) - (a + b)) * MAGIC_NUMBER - ANOTHER_MAGIC_NUMBER;

or

(i + 4 - a - b) * MAGIC_NUMBER - ANOTHER_MAGIC_NUMBER;

#5


4  

When I learned this in grade school (in Canada) it was referred to as BEDMAS:

当我在小学(加拿大)学到这一点时,它被称为BEDMAS:

Brackets
Exponents
Division
Multiplication
Addition
Subtraction

Brackets Exponents Division乘法加法减法

Just for those from this part of the world...

只为来自这个地区的人......

#6


1  

I'm not really sure how applicable to programming the old BODMAS mnemonic is anyways. There is no guarantee on order of operations between languages, and while many keep the standard operations in that order, not all do. And then there are some languages where order of operations isn't really all that meaningful (Lisp dialects, for example). In a way, you're probably better off for programming if you forget the standard order and either use parentheses for everything(eg (a*b) + c) or specifically learn the order for each language you work in.

我不太确定如何适用于编程旧的BODMAS助记符无论如何。语言之间的操作顺序无法保证,虽然许多语言按顺序保留标准操作,但并非所有操作都按此顺序进行。然后有一些语言的操作顺序并不是那么有意义(例如Lisp方言)。在某种程度上,如果您忘记了标准顺序并且对所有内容使用括号(例如(a * b)+ c)或者专门学习您所使用的每种语言的顺序,那么您可能更适合编程。

#7


0  

I read somewhere that especially in C/C++ splitting your expressions into small statements was better for optimisation; so instead of writing hugely complex expressions in one line, you cache the parts into variables and do each one in steps, then build them up as you go along.

我在某处读过,特别是在C / C ++中,将表达式拆分成小语句更适合优化;因此,不是在一行中编写非常复杂的表达式,而是将这些部分缓存到变量中并逐步执行每个变量,然后随着时间的推移进行构建。

The optimisation routines will use registers in places where you had variables so it shouldn't impact space but it can help the compiler a little.

优化例程将在您拥有变量的位置使用寄存器,因此它不应该影响空间,但它可以帮助编译器一点点。

#1


20  

http://www.easymaths.com/What_on_earth_is_Bodmas.htm:

What do you think the answer to 2 + 3 x 5 is?

您认为2 + 3 x 5的答案是什么?

Is it (2 + 3) x 5 = 5 x 5 = 25 ?

是(2 + 3)x 5 = 5 x 5 = 25?

or 2 + (3 x 5) = 2 + 15 = 17 ?

或2 +(3 x 5)= 2 + 15 = 17?

BODMAS can come to the rescue and give us rules to follow so that we always get the right answer:

BODMAS可以拯救并给我们遵守规则,以便我们始终得到正确的答案:

(B)rackets (O)rder (D)ivision (M)ultiplication (A)ddition (S)ubtraction

(B)球拍(O)rder(D)ivision(M)ultiplication(A)ddition(S)ubtraction

According to BODMAS, multiplication should always be done before addition, therefore 17 is actually the correct answer according to BODMAS and will also be the answer which your calculator will give if you type in 2 + 3 x 5 .

根据BODMAS,乘法应该总是在加法之前完成,因此根据BODMAS,17实际上是正确的答案,并且如果你输入2 + 3 x 5,也将是你的计算器给出的答案。

Why it is useful in programming? No idea, but i assume it's because you can get rid of some brackets? I am a quite defensive programmer, so my lines can look like this:

为什么它在编程中有用?不知道,但我认为这是因为你可以摆脱一些括号?我是一个非常防守的程序员,所以我的线条看起来像这样:

result = (((i + 4) - (a + b)) * MAGIC_NUMBER) - ANOTHER_MAGIC_NUMBER;

with BODMAS you can make this a bit clearer:

使用BODMAS,您可以更清楚地说明这一点:

result = (i + 4 - (a + b)) * MAGIC_NUMBER - ANOTHER_MAGIC_NUMBER;

I think i'd still use the first variant - more brackets, but that way i do not have to learn yet another rule and i run into less risk of forgetting it and causing those weird hard to debug errors?

我想我仍然会使用第一个变体 - 更多括号,但是这样我不需要学习另一个规则,并且我会忘记忘记它并导致那些奇怪的难以调试错误的风险?

Just guessing at that part though.

只是猜测那个部分。

Mike Stone EDIT: Fixed math as Gaius points out

Mike Stone编辑:Gaius指出,修正了数学问题

#2


8  

Another version of this (in middle school) was "Please Excuse My Dear Aunt Sally".

另一个版本(在中学)是“请原谅我亲爱的莎莉阿姨”。

  • Parentheses
  • Exponents
  • Multiplication
  • Division
  • Addition
  • Subtraction

The mnemonic device was helpful in school, and still useful in programming today.

助记符设备在学校很有用,并且在今天的编程中仍然有用。

#3


5  

Order of operations in an expression, such as:

表达式中的操作顺序,例如:

foo * (bar + baz^2 / foo) 
  • Brackets first
  • Orders (ie Powers and Square Roots, etc.)
  • 订单(即权力和平方根等)

  • Division and Multiplication (left-to-right)
  • 除法和乘法(从左到右)

  • Addition and Subtraction (left-to-right)
  • 加法和减法(从左到右)

source: http://www.mathsisfun.com/operation-order-bodmas.html

#4


5  

I don't have the power to edit @Michael Stum's answer, but it's not quite correct. He reduces

我没有权力编辑@Michael Stum的答案,但这不太正确。他减少了

(i + 4) - (a + b)

to

(i + 4 - a + b)

They are not equivalent. The best reduction I can get for the whole expression is

它们并不等同。对于整个表达,我可以获得的最佳减少是

((i + 4) - (a + b)) * MAGIC_NUMBER - ANOTHER_MAGIC_NUMBER;

or

(i + 4 - a - b) * MAGIC_NUMBER - ANOTHER_MAGIC_NUMBER;

#5


4  

When I learned this in grade school (in Canada) it was referred to as BEDMAS:

当我在小学(加拿大)学到这一点时,它被称为BEDMAS:

Brackets
Exponents
Division
Multiplication
Addition
Subtraction

Brackets Exponents Division乘法加法减法

Just for those from this part of the world...

只为来自这个地区的人......

#6


1  

I'm not really sure how applicable to programming the old BODMAS mnemonic is anyways. There is no guarantee on order of operations between languages, and while many keep the standard operations in that order, not all do. And then there are some languages where order of operations isn't really all that meaningful (Lisp dialects, for example). In a way, you're probably better off for programming if you forget the standard order and either use parentheses for everything(eg (a*b) + c) or specifically learn the order for each language you work in.

我不太确定如何适用于编程旧的BODMAS助记符无论如何。语言之间的操作顺序无法保证,虽然许多语言按顺序保留标准操作,但并非所有操作都按此顺序进行。然后有一些语言的操作顺序并不是那么有意义(例如Lisp方言)。在某种程度上,如果您忘记了标准顺序并且对所有内容使用括号(例如(a * b)+ c)或者专门学习您所使用的每种语言的顺序,那么您可能更适合编程。

#7


0  

I read somewhere that especially in C/C++ splitting your expressions into small statements was better for optimisation; so instead of writing hugely complex expressions in one line, you cache the parts into variables and do each one in steps, then build them up as you go along.

我在某处读过,特别是在C / C ++中,将表达式拆分成小语句更适合优化;因此,不是在一行中编写非常复杂的表达式,而是将这些部分缓存到变量中并逐步执行每个变量,然后随着时间的推移进行构建。

The optimisation routines will use registers in places where you had variables so it shouldn't impact space but it can help the compiler a little.

优化例程将在您拥有变量的位置使用寄存器,因此它不应该影响空间,但它可以帮助编译器一点点。