Shop OBEX P1 Docs P2 Docs Learn Events
Limit min/max and += — Parallax Forums

Limit min/max and +=

T ChapT Chap Posts: 4,223
edited 2008-12-02 10:07 in Propeller 1
Anyone notice that you can't use <# or #> after += or -=?

x += 1 <# 10

x -= 1 #> 10

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-11-26 22:01
    Haven't noticed before. What happens if you put parentheses around the limit max/min expression?
  • T ChapT Chap Posts: 4,223
    edited 2008-11-27 00:03
    (x -= 1) #> 10 like this?

    Doesn't work for me. Not a big deal, just curious why it doesn't work.
  • RaymanRayman Posts: 14,243
    edited 2008-11-27 00:32
    Well, the limit operators have precedence over assignment, but not addition... So, the compiler should add (or subtract) 1 to x, then apply the limit, then assign x the result...
  • BradCBradC Posts: 2,601
    edited 2008-11-27 01:32
    Which it can't do as the assignment += operator is handled atomically by the interpreter.

    I wonder TChapman does X := X + 1 #> 10 work ?
    (don't have a compiler near me)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cardinal Fang! Fetch the comfy chair.
  • BradCBradC Posts: 2,601
    edited 2008-11-30 19:34
    I can't reproduce this with Propeller Tool 1.2

    PUB ABCD | X
    X += 1 #> 10

    compiles fine here... as does X -= 1 <# 10 and all other permutations of..

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cardinal Fang! Fetch the comfy chair.
  • T ChapT Chap Posts: 4,223
    edited 2008-11-30 20:06
    BradC said...
    I can't reproduce this with Propeller Tool 1.2

    PUB ABCD | X
    X += 1 #> 10

    compiles fine here... as does X -= 1 <# 10 and all other permutations of..


    OK, I am just now testing this using limit max, but the same applies either way. I am only testing in bst. This is a gotcha when it occurs.

    Get an LCD, try this and note the bizarre results. You will soon note that regardless of using (), the limit is affecting the number to be added only, pre-add, not post.


    x := 0   'start at known 0
    
    x += 10 <# 3     
    ser.dec(x)       'will display 3, making you think it is ok, but it is not, what it is doing is affecting the number "10" prior to the addition, not after
    waitcnt(80_000_000)
    (x += 2) <# 3     
    ser.dec(x)            'displays 5
    waitcnt(80_000_000)
    (x += 2) <# 3   
    ser.dec(x)           'displays 7
    waitcnt(80_000_000)
    
    
    
  • BradCBradC Posts: 2,601
    edited 2008-11-30 20:19
    TChapman said...
    BradC said...
    I can't reproduce this with Propeller Tool 1.2

    PUB ABCD | X
    X += 1 #> 10

    compiles fine here... as does X -= 1 <# 10 and all other permutations of..


    OK, I am just now testing this using limit max, but the same applies either way. I am only testing in bst. This is a gotcha when it occurs.

    Get an LCD, try this and note the bizarre results. You will soon note that regardless of using (), the limit is affecting the number to be added only, pre-add, not post.


    x := 0   'start at known 0
    
    x += 10 <# 3     
    ser.dec(x)       'will display 3, making you think it is ok, but it is not, what it is doing is affecting the number "10" prior to the addition, not after
    waitcnt(80_000_000)
    (x += 2) <# 3     
    ser.dec(x)            'displays 5
    waitcnt(80_000_000)
    (x += 2) <# 3   
    ser.dec(x)           'displays 7
    waitcnt(80_000_000)
    
    
    


    Ok, that is a *horrid* (no, really!!) bug in bst
    (x += 2) <# 3
    .. is so incredibly illegal it should _never_ compile (and won't in the next release).
    It's not remotely valid spin code... _ugh_

    X += 10 <# 3
    X should be 3 (as it is in your example as X starts at 0) as (10 <# 3) == 3

    Unfortunately due to the bug in bst (x+=2) #<3 will leave X = 5 (as it adds 2 to X and then proceeds to do nasty things to the stack - lucky it does not crash the interpreter!!)

    += is an assignment. The way it works is _anything_ to the right of that += sign is calculated first, then it is added and stored to the value to the left of the sign.
    Assignments always happen that the value to the right is affected to the value on the left. _always_ Nothing ever affects the left value as stored.. *ever*.

    The simplest way to describe that is pretend the assignment operator is always preceded by parentheses. ie..
    X += 10 <# 3 is equivalent to X += (10 <# 3)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cardinal Fang! Fetch the comfy chair.
  • BradCBradC Posts: 2,601
    edited 2008-11-30 20:39
    Rayman said...
    Well, the limit operators have precedence over assignment, but not addition... So, the compiler should add (or subtract) 1 to x, then apply the limit, then assign x the result...

    Ok, so it's taken me over a week to grok this statement.. but I see where the problem lies..

    yes.. Limit min/max has precedence over addition, but += is not addition, it's an assignment and all assignments have the highest level of precedence.

    To quote the manual "=, :=, all other assignments"

    <more quote>
    "Normal / Assignment
    Normal operators, like Add ‘+’ and Shift Left ‘<<’, operate on their operand(s) and provide
    the result for use by the rest of the expression, without affecting the operand or operands
    themselves. Those that are assignment operators, however, write their result to either the
    variable they operated on (unary), or to the variable to their immediate left (binary), in
    addition to providing the result for use by the rest of the expression."

    <even more quote>
    "Binary operators have special forms that end in equal ‘=’ to make them assignment operators.
    Unary operators do not have a special assignment form; some always assign while others
    assign only in special situations."

    (Page 251-252 Propeller Manual)

    Hope this clears things up

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cardinal Fang! Fetch the comfy chair.
  • SRLMSRLM Posts: 5,045
    edited 2008-11-30 21:37
    These traps should go into the Tricks and Traps document.
  • mparkmpark Posts: 1,305
    edited 2008-12-01 18:36
    BradC said...
    ...and all assignments have the highest level of precedence.



    Lowest, not highest, according to the manual.

    Of course we've seen that the PropTool breaks the precedence rules in some situations.
  • BradCBradC Posts: 2,601
    edited 2008-12-02 10:07
    mpark said...
    BradC said...

    ...and all assignments have the highest level of precedence.



    Lowest, not highest, according to the manual.


    Of course we've seen that the PropTool breaks the precedence rules in some situations.

    You are of course right. I tend to have an upside down view on high/low give I'm antipodean [noparse];)[/noparse]

    However in this case the precedence rules stand. += is an assignment rather than an operator

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cardinal Fang! Fetch the comfy chair.
Sign In or Register to comment.