What is BODMAS and why is it useful in programming?
-
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
From Michael Stum -
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.
From Chris Benard -
@Michael Stum
To be honest, I'm with your "bracketty" style, rather than the supposedly optimised version. Maybe it's just me, but I find
result = (((i + 4) - (a + b)) * MAGIC_NUMBER) - ANOTHER_MAGIC_NUMBER;
easier to parse mentally...
From ZombieSheep -
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
From Patrick McElhaney -
When I learned this in grade school (in Canada) it was referred to as BEDMAS:
Brackets
Exponents
Division
Multiplication
Addition
SubtractionJust for those from this part of the world...
From Wally Lawless -
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.
-
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;Vinko Vrsalovic : A good reason not to forget parentheses!From James A. Rosen -
When I learned this in grade school (in Canada) it was referred to as BEDMAS:
Likewise here in new zealand.
'Exponents' makes more sense to me than the generically named 'Order'
From Orion Edwards -
I usually use white space to show the order of operations on the four +-/* operations, like this:
a + b*cFor all other operators, I use parentheses to make the order of operations clear.
From yjerem -
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.
From widgisoft -
Operator precedence in C and C++
From foxy
0 comments:
Post a Comment