术语“BODMAS”是什么意思? 意思是?

发布于 2024-07-04 01:51:58 字数 30 浏览 5 评论 0原文

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

What is BODMAS and why is it useful in programming?

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

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

发布评论

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

评论(7

独守阴晴ぅ圆缺 2024-07-11 01:51:58

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

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

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

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

BODMAS 可以拯救我们并为我们提供遵循的规则,以便我们始终得到正确的答案:

(B)球拍(O)顺序(D)除法(M)乘法(A)加法(S)减法

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

为什么它在编程中有用? 不知道,但我想这是因为你可以去掉一些括号? 我是一个相当防御性的程序员,所以我的台词可以是这样的:

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

使用 BODMAS 你可以让它更清楚一点:

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

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

只是猜测那部分。

迈克·斯通编辑:正如盖乌斯指出的那样修正了数学

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

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

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

or 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:

(B)rackets (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 .

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:

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

我家小可爱 2024-07-11 01:51:58

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

  • 括号
  • 乘法
  • 除法
  • 加法
  • 减法
  • 指数

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

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.

So要识趣 2024-07-11 01:51:58

表达式中的运算顺序,例如:

foo * (bar + baz^2 / foo) 
  • B球优先
  • O顺序(即幂和平方根等)
  • D除法和<乘法(从左到右)
  • 加法减法(从左到右)

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

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

花开浅夏 2024-07-11 01:51:58

我无权编辑@Michael Stum 的答案,但它并不完全正确。 他简化

(i + 4) - (a + b)

(i + 4 - a + b)

“它们不等价”。 我能得到的整个表达式的最佳减少是

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

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

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

(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;
终陌 2024-07-11 01:51:58

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

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.

影子的影子 2024-07-11 01:51:58

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

B球拍
E指数
部门部门
乘法乘法
添加添加
减法

仅适用于来自世界这一地区的人......

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

Brackets
Exponents
Division
Multiplication
Addition
Subtraction

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

忆梦 2024-07-11 01:51:58

我在某处读到,特别是在 C/C++ 中,将表达式拆分为小语句更有利于优化; 因此,您不必在一行中编写极其复​​杂的表达式,而是将各个部分缓存到变量中并逐步执行每个部分,然后在进行过程中构建它们。

优化例程将在有变量的地方使用寄存器,因此它不会影响空间,但它可以为编译器提供一些帮助。

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.

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.

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