math - Che cosa significa il termine "BODMAS"?

original title: "math - What does the term "BODMAS" mean?"


What is BODMAS and why is it useful in programming?

Che cos'è BODMAS e perché è utile nella programmazione?

Questo è il riepilogo dopo la traduzione, se è necessario visualizzare la traduzione completa, fare clic sull'icona "traduci"

Tutte le risposte
  • Translate

    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

  • Translate

    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.

  • Translate

    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)


  • Translate

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

    (i + 4) - (a + b)


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


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

  • Translate

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


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

  • Translate

    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.

  • Translate

    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.