• cerement@slrpnk.net
    link
    fedilink
    arrow-up
    52
    ·
    11 months ago
    • 16 is the right answer if you use PEMDAS only: (8 ÷ 2) × (2 + 2)
    • 1 is the right answer if you use implicit/explicit with PEMDAS: 8 ÷ (2 × (2 + 2))
    • both are correct answers (as in if you don’t put in extra parentheses to reduce ambiguity, you should expect expect either answer)
    • this is also one of the reasons why postfix and prefix notations have an advantage over infix notation
      • postfix (HP, RPN, Forth): 2 2 + 8 2 ÷ × .
      • prefix (Lisp): (× (÷ 8 2) (+ 2 2))
    • brian@programming.dev
      link
      fedilink
      arrow-up
      3
      ·
      11 months ago

      prefix notation doesn’t need parentheses either though, at least in this case. lisp uses them for readability and to get multiple arity operators. infix doesn’t have any ambiguity either if you parenthesize all operations like that.

      • synae[he/him]@lemmy.sdf.org
        link
        fedilink
        English
        arrow-up
        2
        ·
        edit-2
        11 months ago

        PEMDAS is actually (PE)(MD)(AS). Those that are grouped together have equal precedence and are evaluated left to right.

        8 / 2 * (2+2)

        8 / 2 * 4

        4 * 4

        16

        Edit to fix formatting, maybe?