Menu

What's 6/2(1+2) ?

2022-05-10
2022-05-15
1 2 > >> (Page 1 of 2)
  • stan cartwright

    stan cartwright - 2022-05-10

    This was a youtube vid I saw. What's 6/2(1+2).
    I thought it was one but gcb gives the "correct" answer,, needs a space to asm 6/ 2(1+2)
    no wonder my code fails sometimes :)

     
  • Anobium

    Anobium - 2022-05-11

    Surely, 6/2*(1+2) works ? note the *

     
  • Anobium

    Anobium - 2022-05-11

    And, the answer is?

     
    • Chris Roper

      Chris Roper - 2022-05-11

      The answer to all of mans problems - unity

       
      • stan cartwright

        stan cartwright - 2022-05-11

        Ha ha.. why didn't someone think of that before?
        Quote- 42 (or forty-two) is the Answer to the Ultimate Question of Life, the Universe and Everything. This Answer was first calculated by the supercomputer Deep Thought after seven and a half million years of thought :)

        GCB flash/hexed 6/ 2(1+2) and gave the "correct" answer.... 9
        No 6/2(1+2) needed just a space after6/
        It does 6/2=3
        (1+2)=3
        3 * 3=9.
        like (6/2)
        (1+2)

        Interesting and no * needed as real numbers, not vars, like var(1+2) which would be an arrary element.

        glcdprint 0,0,str((6/2) (1+2)) gives 0
        glcdprint 0,0,str((6/2) * (1+2)) gives 9

         

        Last edit: stan cartwright 2022-05-11
        • Chris Roper

          Chris Roper - 2022-05-12

          Then GCBASIC has its precedence wrong.
          Multiplication should proceed division so the correct result would be :

          6 / (2 * ( 1 + 2) =
          6 / (2 * 3) =
          6 / 6 =
          1

          That is a prime example why parenthesis should be used extensively in programing, to ensure that the computation follows the order the programmer intended.

           

          Last edit: Chris Roper 2022-05-12
  • Chris Roper

    Chris Roper - 2022-05-12

    Interesting results because an HP 42 calculator Returns an answer of 1.
    6 Enter
    1 Enter
    2 Plus
    2 Times
    Divide

    Maybe I have gone rusty on my calculator use............

     
    • Anobium

      Anobium - 2022-05-12

      Must be old fingers... Excel shows 9 also. :=)

       
  • Chris Roper

    Chris Roper - 2022-05-12

    show your working.

    I suspect that one of us is entering data into the calculators with the incorrect precedence based on the original post:

    6/2(1+2)

    is that
    (6 / 2)*(1+2)
    or
    6/(2 * (1+2))

    as I said above : "That is a prime example why parenthesis should be used".

     

    Last edit: Chris Roper 2022-05-12
  • Anobium

    Anobium - 2022-05-12

    Attached is Excel... I just put the raw calc in there.

    =6/2*(1+2)              
        (1+2)   = 3     
        6/2     = 3     
        3*3     = 9
    
     
  • stan cartwright

    stan cartwright - 2022-05-12

    So gcb is doing sums correct.
    I thought brackets (1+2)=3
    2*(3)=6
    6/6=1
    what have I learnt? and that's using left to right evaluation.
    Use braces, brackets, parenthesis so they're first in left to right order.
    Then div,mult,add,subtract... is it??
    Chris says 1 and I thought that but it's 9 cos div before mult.

     

    Last edit: stan cartwright 2022-05-12
    • Chris Roper

      Chris Roper - 2022-05-12

      That is the way I see it too.
      The problem was ambiguous, hence the point I am trying to make "parenthesis should be used".
      If we can't agree on the order of execution how can we expect a compiler to do it?
      We are all putting the * where we think it should be but the original problem implied it.

       
  • Chris Roper

    Chris Roper - 2022-05-12

    Found this https://youtu.be/Q0przEtP19s - Math Prof answers 6÷2(1+2) = ? once and for all Viral Math Problem
    He is a bit long winded but basically says it can go both ways because of the ambiguity.

     

    Last edit: Chris Roper 2022-05-12
    • stan cartwright

      stan cartwright - 2022-05-12

      yes Chris. I just came across one video and tried it with gcb and got 9, so we know gcb uses div before mult,,yes?
      I just use brackets for sums and don't do complicated stuff, I break it down to give the compiler a break,,, it's not psychic,, nor am I so don't know what the compiler "thinks" it should do.

       
  • Chris Roper

    Chris Roper - 2022-05-12

    Final comment.

    HP Calculators say the * has presidency over /.
    TI Calculators treat them as equal precedence and calculate left to right.
    Both consider an implied * if a Number is adjacent to a Brace.

    So bottom line which software library are you using.

    It is up to the person writing the equation to specify the intent with correct notation and not make assumptions.

    That was a good couple of hours diversion, fun and educational though, now back to railway shunting problems :)

     
    • stan cartwright

      stan cartwright - 2022-05-12

      yeah. I saved a prog last night using cgstudio and it's gone or wasn't saved today. Really pissed cos I was when I coded it and it was working.. plot a sprite or reversed or rotated 90 deg clockwise or 90 deg anti clockwise. It was how you read the sprite table. Is it a gcstudio prob? Yeah, more other stuff to think about.
      you can't really check 6/2(1+2) with windows calculator.

       
  • William Roth

    William Roth - 2022-05-13

    From Windows Calculator (Scientific Mode)

     
  • Chris Roper

    Chris Roper - 2022-05-13

    Try it on an HP Calculator: https://hpcalcs.com/download/

     
  • Chris Roper

    Chris Roper - 2022-05-13

    As an engineer would you trust the Math results from windows over an HP Scientific Calculator?

     
  • Chris Roper

    Chris Roper - 2022-05-13

    Note also that in Windows you forced the Precedence by pressing the "Multiply" key.
    On the HP shown it was entered as written without the Multiply symbol the HP then arranged it as shown adding the Multiply itself.

     
  • Chris Roper

    Chris Roper - 2022-05-13

    Solved - The HP Calculator has 3 modes that can be changed in the Settings Menu.
    Textbook, Algebraic and RPM.
    By default it is in Textbook Mode.
    Changing the Mode to Algebraic produces this result:

     
  • Chris Roper

    Chris Roper - 2022-05-13

    So I return to my original conclusion:

    Both results are correct because the equation is ambiguous - Always use Parentheses to force precedence when writing equations, Especially in a programing language.

    AS shown above one calculator can show two different results from the same equation depending on the chosen math library.

     
  • William Roth

    William Roth - 2022-05-13

    The equation is ambiguous because there is more than one rule set/method. As I understand it the most common set of rules used after 1917 is called PEMDAS . A google search will provide details. It should not be too hard to follow PEMDAS, once understood, as this seems to be what GCB uses.

    When in Rome .......

     
1 2 > >> (Page 1 of 2)

Log in to post a comment.