Announcement

Collapse
No announcement yet.

ohoh ... something strange.. have a look

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • ohoh ... something strange.. have a look

    Hi everybody,

    the following will also compile on PB/Win but you need to change the print
    statements

    Why is -1^2 = 1 where it should be -1

    FUNCTION PBMAIN() AS LONG

    Local eA, eB As Extended
    Local nA, nB As Long
    eA = -1
    eB = 2
    Print eA ^ eB

    nA = -1
    nB = 2
    Print nA ^ nB

    Print -1^2 ' this one is ok. = -1



    END Function

    If this is a problem then I will report it to support but maybe there is another
    explanation.

    Cheers

    Steven
    So here we are, this is the end.
    But all that dies, is born again.
    - From The Ashes (In This Moment)

  • #2
    > Why is -1^2 = 1 where it should be -1

    Um, negative one squared is equal to positive one.

    -- Eric
    Last edited by Eric Pearson; 23 Jan 2009, 10:36 AM. Reason: clearer
    "Not my circus, not my monkeys."

    Comment


    • #3
      ...and the reason that PRINT -1^2 displays -1 is "precedence of operators". In a single equation, exponentiation happens first so (1^2) = 1... then the negation makes it negative one.

      All is right with the world.

      -- Eric
      "Not my circus, not my monkeys."

      Comment


      • #4
        Eric,

        One would think so but the following text from the help file indicates the order of precedence.

        Another example of the effect of Order of Precedence on an expression follows:

        x = -1^2

        At first glance, the result of 1 may be the expected result (since -1 * -1 = 1); however, the unary negation operator has a lower precedence than exponentiation, so the expression is evaluated as x& = -(1^2) which gives a result value of -1. As noted above, the use of parentheses can clarify the intended expression:

        x = (-1)^2
        Also, why does Print -1^2 gives us -1 and the other examples give us 1
        So here we are, this is the end.
        But all that dies, is born again.
        - From The Ashes (In This Moment)

        Comment


        • #5
          To clarify... the calculation with the variables produces (-1 times -1 =) +1 which is the correct answer.

          The PRINT statement is calculating -(1^2) not (-1)^2 just as shown in the example from the Help File that you posted, so it produces -1 not +1.

          -- Eric
          "Not my circus, not my monkeys."

          Comment


          • #6
            Well, until the compiler implements....
            Code:
            #DWIM ON
            ... using a few more explicit parens and/or variables should solve your problems.

            MCM
            (Do What I Mean)
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              Thanks Eric for clarifying this to me
              So here we are, this is the end.
              But all that dies, is born again.
              - From The Ashes (In This Moment)

              Comment


              • #8
                Anyway, when thinking about this. this brings up a problem when trying to
                just evaluate using a print statement and actually implementing a routine
                using variables...
                So here we are, this is the end.
                But all that dies, is born again.
                - From The Ashes (In This Moment)

                Comment


                • #9
                  this brings up a problem when trying to
                  just evaluate using a print statement and actually implementing a routine
                  using variables...


                  Code:
                  Local eA, eB As Extended
                  Local nA, nB As Long
                  
                  LOCAL R  AS EXTENDED 
                  eA = -1
                  eB = 2
                  R =  eA^eB
                  'Print eA ^ eB
                  PRINT R 
                  
                  nA = -1
                  nB = 2
                  R   = nA ^ nB
                  'Print nA ^ nB
                  PRINT R
                  .....
                  Michael Mattias
                  Tal Systems (retired)
                  Port Washington WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment

                  Working...
                  X