Limit min/max and +=
T Chap
Posts: 4,223
Anyone notice that you can't use <# or #> after += or -=?
x += 1 <# 10
x -= 1 #> 10
x += 1 <# 10
x -= 1 #> 10
Comments
Doesn't work for me. Not a big deal, just curious why it doesn't work.
I wonder TChapman does X := X + 1 #> 10 work ?
(don't have a compiler near me)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cardinal Fang! Fetch the comfy chair.
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.
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.
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.
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.
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.