What does the term “BODMAS” mean?

2020-02-11 18:45发布

What is BODMAS and why is it useful in programming?

标签: math
7条回答
Juvenile、少年°
2楼-- · 2020-02-11 18:59

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

查看更多
兄弟一词,经得起流年.
3楼-- · 2020-02-11 19:02

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.

查看更多
beautiful°
4楼-- · 2020-02-11 19:02

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

查看更多
倾城 Initia
5楼-- · 2020-02-11 19:07

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;
查看更多
来,给爷笑一个
6楼-- · 2020-02-11 19:10

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...

查看更多
Root(大扎)
7楼-- · 2020-02-11 19:13

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.

查看更多
登录 后发表回答