s-news
[Top] [All Lists]

Re: [S] Unusual result

To: calder@stat.colostate.edu
Subject: Re: [S] Unusual result
From: Berwin Turlach <berwin.turlach@anu.edu.au>
Date: Wed, 25 Mar 1998 22:09:36 +1100
Cc: s-news@wubios.wustl.edu
In-reply-to: <Pine.SUN.3.96.980324213219.16434A-100000@tlaloc.stat.colostate.edu> (message from Matt Calder on Tue, 24 Mar 1998 21:54:59 -0700 (MST))
Sender: owner-s-news@wubios.wustl.edu
G'day Matt,

>>>>> "MW" == Matt Calder <calder@stat.colostate.edu> writes:

  MW> S-news, I came across this today and was surprised by the
  MW> result. Perhaps someone could explain this in terms of order of
  MW> operations.

  MW> After: a <- 0
  MW>        a <- (a <- a + 1) + (a <- a + 1)

  MW> The value of a is 4. I expected 3.
Well, I cannot explain it in terms of order of operations.  My guess
is, that this sequence of instruction eventually boils down to the
execution of the following instructions:
1) fetch `a', add 1 to it and store it in `a' [first a<-a+1, now a=1]
2) fetch `a', add 1 to it and store it in `a' [second a<-a+1, now a=2]
3) to form the addition, we need the result of the code inside the two
   ()'s, but that is stored in `a' [first ()] and `a' [second ()]. Hence,
fetch `a' and `a', add these two numbers and store the result in `a'.
Result: a=4.

It seems to be the S+ equivalent to i=i++ in C.  You are probably
lucky that S+ didn't reformat your disk :)

Best wishes,

        Berwin

=============================== Full address ================================

Berwin A Turlach
Centre for Mathematics and its Applications Tel.: +61 (2) 6249 0706 (Secr.) 
Statistical Science Program                       +61 (2) 6249 0731 (direct)
The Australian National University          FAX : +61 (2) 6249 5549
Canberra ACT 0200                         e-mail: berwin.turlach@anu.edu.au  
Australia                                http://wwwmaths.anu.edu.au/~berwin
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>