Question about order of operations convention and alternative












1












$begingroup$


I'm (trying) to make my own code parser that evaluates expressions. I've done some reading about this topic and seen the various mnemonics that school children are taught, such as:



P(arentheses)

E(xponents)

M(ultiplication)

D(ivision)

A(ddition)

S(ubtraction)



B(rackets)

O(rders) / sometimes I(ndices)

D(ivision)

M(ultiplication)

A(ddition)

S(ubtraction)



In PEMDAS multiplication comes before division, and in BODMAS/BIDMAS division comes before multiplication. However I've realised this doesn't matter because multiplication and division using this convention have the same level of precedence, but it only works if an extra convention is followed, that is, that you evaluate the expressions from left to right. The same goes for subtraction and addition, in some cases if you do addition before subtraction you'll get the wrong answer. The two operations add and minus have the same precedence, however there's still the extra requirement that you evaluate from left to right. This seems to have confused many students on the Khan Academy comments section.



So to take an example:



$8 div 4 times 2 - 2 + 4$



If you follow PEMDAS without taking into account left to right evaluation we get the wrong answer:



Multiplication:
$ 4 times 2 = 8$
$Division: 8 div 8 = 1$

Result: $ 1 - 2 + 4$

Correct result: $4 - 2 + 4$

then

Addition:
$ 2 + 4 = 6 $

Subtraction:
$ 1 - 6 = -1$

Result: $-1$

Correct result: $6$



So obviously this error happened because I didn't evaluate left to right. But I was thinking, originally when I was trying to figure this out I thought about giving division a higher precedence than multiplication, multiplication a higher precedence than subtraction, and subtraction a higher precedence than addition, so the precedence order would look like:



$div$
$times$
$-$
$+$



This goes against what math teaches that division and multiplication have the same precedence, and addition and subtraction have the same precedence. However I noticed that if I order the operations this way, not only does the above example work correctly, but all the other examples do too. For example:



$ 12 div 3 times 2 + 4 div 4 - 1 times 10 $



If I use my own precedence table I get the result reading this forwards or backwards. And it's the same result as using PEMDAS or BODMAS with the left-to-right rule.



A simpler case:



$8 - 4 + 2 - 2$



PEMDAS and BODMAS teaches us that plus and minus have the same precedence, so we can just evaluate this from left to right. Or if we take minus operator to have a higher precedence than addition, then we can forget about both the fact that plus and minus have equal precedence and also forget the left-to-right rule and picture something like:



$(8 - 4) + (2 - 2)$



So would having my own operator precedence in the way I've shown above, ignoring the left-to-right rule, work out in all cases, or am I missing something. I'd like to hear if this slightly modified operator precedence doesn't work, because then I'll just implement a PEMDAS type function.



Also, if it does work, would arranging the operator precedence to be, from highest to lowest $div times - +$ and forgetting about the left-to-right rule be a valid math order of operations equivalent to PEMDAS or BODMAS? Obviously I'd maintain the parentheses and exponentiation as higher.










share|cite|improve this question









$endgroup$

















    1












    $begingroup$


    I'm (trying) to make my own code parser that evaluates expressions. I've done some reading about this topic and seen the various mnemonics that school children are taught, such as:



    P(arentheses)

    E(xponents)

    M(ultiplication)

    D(ivision)

    A(ddition)

    S(ubtraction)



    B(rackets)

    O(rders) / sometimes I(ndices)

    D(ivision)

    M(ultiplication)

    A(ddition)

    S(ubtraction)



    In PEMDAS multiplication comes before division, and in BODMAS/BIDMAS division comes before multiplication. However I've realised this doesn't matter because multiplication and division using this convention have the same level of precedence, but it only works if an extra convention is followed, that is, that you evaluate the expressions from left to right. The same goes for subtraction and addition, in some cases if you do addition before subtraction you'll get the wrong answer. The two operations add and minus have the same precedence, however there's still the extra requirement that you evaluate from left to right. This seems to have confused many students on the Khan Academy comments section.



    So to take an example:



    $8 div 4 times 2 - 2 + 4$



    If you follow PEMDAS without taking into account left to right evaluation we get the wrong answer:



    Multiplication:
    $ 4 times 2 = 8$
    $Division: 8 div 8 = 1$

    Result: $ 1 - 2 + 4$

    Correct result: $4 - 2 + 4$

    then

    Addition:
    $ 2 + 4 = 6 $

    Subtraction:
    $ 1 - 6 = -1$

    Result: $-1$

    Correct result: $6$



    So obviously this error happened because I didn't evaluate left to right. But I was thinking, originally when I was trying to figure this out I thought about giving division a higher precedence than multiplication, multiplication a higher precedence than subtraction, and subtraction a higher precedence than addition, so the precedence order would look like:



    $div$
    $times$
    $-$
    $+$



    This goes against what math teaches that division and multiplication have the same precedence, and addition and subtraction have the same precedence. However I noticed that if I order the operations this way, not only does the above example work correctly, but all the other examples do too. For example:



    $ 12 div 3 times 2 + 4 div 4 - 1 times 10 $



    If I use my own precedence table I get the result reading this forwards or backwards. And it's the same result as using PEMDAS or BODMAS with the left-to-right rule.



    A simpler case:



    $8 - 4 + 2 - 2$



    PEMDAS and BODMAS teaches us that plus and minus have the same precedence, so we can just evaluate this from left to right. Or if we take minus operator to have a higher precedence than addition, then we can forget about both the fact that plus and minus have equal precedence and also forget the left-to-right rule and picture something like:



    $(8 - 4) + (2 - 2)$



    So would having my own operator precedence in the way I've shown above, ignoring the left-to-right rule, work out in all cases, or am I missing something. I'd like to hear if this slightly modified operator precedence doesn't work, because then I'll just implement a PEMDAS type function.



    Also, if it does work, would arranging the operator precedence to be, from highest to lowest $div times - +$ and forgetting about the left-to-right rule be a valid math order of operations equivalent to PEMDAS or BODMAS? Obviously I'd maintain the parentheses and exponentiation as higher.










    share|cite|improve this question









    $endgroup$















      1












      1








      1


      0



      $begingroup$


      I'm (trying) to make my own code parser that evaluates expressions. I've done some reading about this topic and seen the various mnemonics that school children are taught, such as:



      P(arentheses)

      E(xponents)

      M(ultiplication)

      D(ivision)

      A(ddition)

      S(ubtraction)



      B(rackets)

      O(rders) / sometimes I(ndices)

      D(ivision)

      M(ultiplication)

      A(ddition)

      S(ubtraction)



      In PEMDAS multiplication comes before division, and in BODMAS/BIDMAS division comes before multiplication. However I've realised this doesn't matter because multiplication and division using this convention have the same level of precedence, but it only works if an extra convention is followed, that is, that you evaluate the expressions from left to right. The same goes for subtraction and addition, in some cases if you do addition before subtraction you'll get the wrong answer. The two operations add and minus have the same precedence, however there's still the extra requirement that you evaluate from left to right. This seems to have confused many students on the Khan Academy comments section.



      So to take an example:



      $8 div 4 times 2 - 2 + 4$



      If you follow PEMDAS without taking into account left to right evaluation we get the wrong answer:



      Multiplication:
      $ 4 times 2 = 8$
      $Division: 8 div 8 = 1$

      Result: $ 1 - 2 + 4$

      Correct result: $4 - 2 + 4$

      then

      Addition:
      $ 2 + 4 = 6 $

      Subtraction:
      $ 1 - 6 = -1$

      Result: $-1$

      Correct result: $6$



      So obviously this error happened because I didn't evaluate left to right. But I was thinking, originally when I was trying to figure this out I thought about giving division a higher precedence than multiplication, multiplication a higher precedence than subtraction, and subtraction a higher precedence than addition, so the precedence order would look like:



      $div$
      $times$
      $-$
      $+$



      This goes against what math teaches that division and multiplication have the same precedence, and addition and subtraction have the same precedence. However I noticed that if I order the operations this way, not only does the above example work correctly, but all the other examples do too. For example:



      $ 12 div 3 times 2 + 4 div 4 - 1 times 10 $



      If I use my own precedence table I get the result reading this forwards or backwards. And it's the same result as using PEMDAS or BODMAS with the left-to-right rule.



      A simpler case:



      $8 - 4 + 2 - 2$



      PEMDAS and BODMAS teaches us that plus and minus have the same precedence, so we can just evaluate this from left to right. Or if we take minus operator to have a higher precedence than addition, then we can forget about both the fact that plus and minus have equal precedence and also forget the left-to-right rule and picture something like:



      $(8 - 4) + (2 - 2)$



      So would having my own operator precedence in the way I've shown above, ignoring the left-to-right rule, work out in all cases, or am I missing something. I'd like to hear if this slightly modified operator precedence doesn't work, because then I'll just implement a PEMDAS type function.



      Also, if it does work, would arranging the operator precedence to be, from highest to lowest $div times - +$ and forgetting about the left-to-right rule be a valid math order of operations equivalent to PEMDAS or BODMAS? Obviously I'd maintain the parentheses and exponentiation as higher.










      share|cite|improve this question









      $endgroup$




      I'm (trying) to make my own code parser that evaluates expressions. I've done some reading about this topic and seen the various mnemonics that school children are taught, such as:



      P(arentheses)

      E(xponents)

      M(ultiplication)

      D(ivision)

      A(ddition)

      S(ubtraction)



      B(rackets)

      O(rders) / sometimes I(ndices)

      D(ivision)

      M(ultiplication)

      A(ddition)

      S(ubtraction)



      In PEMDAS multiplication comes before division, and in BODMAS/BIDMAS division comes before multiplication. However I've realised this doesn't matter because multiplication and division using this convention have the same level of precedence, but it only works if an extra convention is followed, that is, that you evaluate the expressions from left to right. The same goes for subtraction and addition, in some cases if you do addition before subtraction you'll get the wrong answer. The two operations add and minus have the same precedence, however there's still the extra requirement that you evaluate from left to right. This seems to have confused many students on the Khan Academy comments section.



      So to take an example:



      $8 div 4 times 2 - 2 + 4$



      If you follow PEMDAS without taking into account left to right evaluation we get the wrong answer:



      Multiplication:
      $ 4 times 2 = 8$
      $Division: 8 div 8 = 1$

      Result: $ 1 - 2 + 4$

      Correct result: $4 - 2 + 4$

      then

      Addition:
      $ 2 + 4 = 6 $

      Subtraction:
      $ 1 - 6 = -1$

      Result: $-1$

      Correct result: $6$



      So obviously this error happened because I didn't evaluate left to right. But I was thinking, originally when I was trying to figure this out I thought about giving division a higher precedence than multiplication, multiplication a higher precedence than subtraction, and subtraction a higher precedence than addition, so the precedence order would look like:



      $div$
      $times$
      $-$
      $+$



      This goes against what math teaches that division and multiplication have the same precedence, and addition and subtraction have the same precedence. However I noticed that if I order the operations this way, not only does the above example work correctly, but all the other examples do too. For example:



      $ 12 div 3 times 2 + 4 div 4 - 1 times 10 $



      If I use my own precedence table I get the result reading this forwards or backwards. And it's the same result as using PEMDAS or BODMAS with the left-to-right rule.



      A simpler case:



      $8 - 4 + 2 - 2$



      PEMDAS and BODMAS teaches us that plus and minus have the same precedence, so we can just evaluate this from left to right. Or if we take minus operator to have a higher precedence than addition, then we can forget about both the fact that plus and minus have equal precedence and also forget the left-to-right rule and picture something like:



      $(8 - 4) + (2 - 2)$



      So would having my own operator precedence in the way I've shown above, ignoring the left-to-right rule, work out in all cases, or am I missing something. I'd like to hear if this slightly modified operator precedence doesn't work, because then I'll just implement a PEMDAS type function.



      Also, if it does work, would arranging the operator precedence to be, from highest to lowest $div times - +$ and forgetting about the left-to-right rule be a valid math order of operations equivalent to PEMDAS or BODMAS? Obviously I'd maintain the parentheses and exponentiation as higher.







      binary-operations






      share|cite|improve this question













      share|cite|improve this question











      share|cite|improve this question




      share|cite|improve this question










      asked Dec 1 '18 at 8:03









      ZebrafishZebrafish

      21018




      21018






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "69"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          noCode: true, onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3021111%2fquestion-about-order-of-operations-convention-and-alternative%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Mathematics Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3021111%2fquestion-about-order-of-operations-convention-and-alternative%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Quarter-circle Tiles

          build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

          Mont Emei