Home » Questions » Computers [ Ask a new question ]

What does the term "BODMAS" mean?

What does the term "BODMAS" mean?

What is BODMAS and why is it useful in programming?

Asked by: Guest | Views: 391
Total answers/comments: 4
Guest [Entry]

"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"
Guest [Entry]

"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."
Guest [Entry]

"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"
Guest [Entry]

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