Administrator
|
Well, I bumbled into something yesterday that I should have been able to fix immediately.
After a bit of fumbling it became face palm time. Who woulda thunk? It has to do with operator precedence. DOH!!!! --- DATA LIST FREE / x. BEGIN DATA 1 END DATA. LOOP #a=1 TO 2. LOOP #b=1 TO 2. COMPUTE a=#a. COMPUTE b=#b. COMPUTE aneg0 = -1**#a. COMPUTE bneg0 = -1**#a. COMPUTE abneg0 = aneg0*bneg0. COMPUTE aneg1 = (-1)**#a. COMPUTE bneg1 = (-1)**#b. COMPUTE abneg1 = aneg1*bneg1. XSAVE OUTFILE 'ab.sav' / KEEP a b aneg0 bneg0 abneg0 aneg1 bneg1 abneg1. END LOOP. END LOOP. EXE. GET FILE 'ab.sav'. LIST. A B ANEG0 BNEG0 ABNEG0 ANEG1 BNEG1 ABNEG1 1.00 1.00 -1.00 -1.00 1.00 -1.00 -1.00 1.00 1.00 2.00 -1.00 -1.00 1.00 -1.00 1.00 -1.00 2.00 1.00 -1.00 -1.00 1.00 1.00 -1.00 -1.00 2.00 2.00 -1.00 -1.00 1.00 1.00 1.00 1.00 Number of cases read: 4 Number of cases listed: 4
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
At 09:20 AM 3/28/2013, David Marso wrote:
>Well, I bumbled into something yesterday ... It has to do with >operator precedence. >--- >LOOP #a=1 TO 2. >LOOP #b=1 TO 2. >COMPUTE a=#a. >COMPUTE b=#b. >COMPUTE aneg0 = -1**#a /* Always returns -1 */. >COMPUTE bneg0 = -1**#a /* Always returns -1 */. >COMPUTE abneg0 = aneg0*bneg0. >COMPUTE aneg1 = (-1)**#a /* Works as desired */. >COMPUTE bneg1 = (-1)**#b /* Works as desired */. Fascinating. So exponentiation takes precedence over monadic "-". I wonder whether the precedence rules are documented anywhere? The word "precedence" doesn't occur in the Universals section of the Command Syntax Reference. Monadic, or prefix, "-" is subject to a certain ambiguity when it precedes a number: Is is a part of the constant, and so has precedence over everything, or is it an operator? And, if an operator, with what precedence? I notice that C++ gives it very high precedence, so your code would have worked. I wonder what language or recommendations SPSS was following, developing its precedences rules? They must date from the 1970s. By the way, David, since you're a lover and master of the neat coding technique, here's how to run your test code and demo without DATA LIST or XSAVE: NEW FILE. INPUT PROGRAM. LOOP #a=1 TO 2. LOOP #b=1 TO 2. COMPUTE a=#a. COMPUTE b=#b. COMPUTE aneg0 = -1**#a. COMPUTE bneg0 = -1**#a. COMPUTE abneg0 = aneg0*bneg0. COMPUTE aneg1 = (-1)**#a. COMPUTE bneg1 = (-1)**#b. COMPUTE abneg1 = aneg1*bneg1. END CASE. END LOOP. END LOOP. END FILE. END INPUT PROGRAM. LIST. List |-----------------------------|---------------------------| |Output Created |30-MAR-2013 12:25:03 | |-----------------------------|---------------------------| a b aneg0 bneg0 abneg0 aneg1 bneg1 abneg1 1.00 1.00 -1.00 -1.00 1.00 -1.00 -1.00 1.00 1.00 2.00 -1.00 -1.00 1.00 -1.00 1.00 -1.00 2.00 1.00 -1.00 -1.00 1.00 1.00 -1.00 -1.00 2.00 2.00 -1.00 -1.00 1.00 1.00 1.00 1.00 Number of cases read: 4 Number of cases listed: 4 ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
From the CSR in the Universals>Transformation
Expressions section
The order of execution is as follows: functions; exponentiation; multiplication, division, and unary -; and addition and subtraction. �� Operators at the same level are executed from left to right. Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] phone: 720-342-5621 From: Richard Ristow <[hidden email]> To: [hidden email], Date: 03/30/2013 10:50 AM Subject: Re: [SPSSX-L] Beware of operator precedence Sent by: "SPSSX(r) Discussion" <[hidden email]> At 09:20 AM 3/28/2013, David Marso wrote: >Well, I bumbled into something yesterday ... It has to do with >operator precedence. >--- >LOOP #a=1 TO 2. >LOOP #b=1 TO 2. >COMPUTE a=#a. >COMPUTE b=#b. >COMPUTE aneg0 = -1**#a /* Always returns -1 */. >COMPUTE bneg0 = -1**#a /* Always returns -1 */. >COMPUTE abneg0 = aneg0*bneg0. >COMPUTE aneg1 = (-1)**#a /* Works as desired */. >COMPUTE bneg1 = (-1)**#b /* Works as desired */. Fascinating. So exponentiation takes precedence over monadic "-". I wonder whether the precedence rules are documented anywhere? The word "precedence" doesn't occur in the Universals section of the Command Syntax Reference. Monadic, or prefix, "-" is subject to a certain ambiguity when it precedes a number: Is is a part of the constant, and so has precedence over everything, or is it an operator? And, if an operator, with what precedence? I notice that C++ gives it very high precedence, so your code would have worked. I wonder what language or recommendations SPSS was following, developing its precedences rules? They must date from the 1970s. By the way, David, since you're a lover and master of the neat coding technique, here's how to run your test code and demo without DATA LIST or XSAVE: NEW FILE. INPUT PROGRAM. LOOP #a=1 TO 2. LOOP #b=1 TO 2. COMPUTE a=#a. COMPUTE b=#b. COMPUTE aneg0 = -1**#a. COMPUTE bneg0 = -1**#a. COMPUTE abneg0 = aneg0*bneg0. COMPUTE aneg1 = (-1)**#a. COMPUTE bneg1 = (-1)**#b. COMPUTE abneg1 = aneg1*bneg1. END CASE. END LOOP. END LOOP. END FILE. END INPUT PROGRAM. LIST. List |-----------------------------|---------------------------| |Output Created |30-MAR-2013 12:25:03 | |-----------------------------|---------------------------| a b aneg0 bneg0 abneg0 aneg1 bneg1 abneg1 1.00 1.00 -1.00 -1.00 1.00 -1.00 -1.00 1.00 1.00 2.00 -1.00 -1.00 1.00 -1.00 1.00 -1.00 2.00 1.00 -1.00 -1.00 1.00 1.00 -1.00 -1.00 2.00 2.00 -1.00 -1.00 1.00 1.00 1.00 1.00 Number of cases read: 4 Number of cases listed: 4 ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
At 01:24 PM 3/30/2013, Jon K Peck wrote:
> From the CSR in the Universals>Transformation Expressions section > >The order of execution is as follows: functions; exponentiation; >multiplication, division, and unary -; >and addition and subtraction. >Operators at the same level are executed from left to right. Thank you, Jon. And my apologies, for having searched superficially for this information. ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
Free forum by Nabble | Edit this page |