SPIN - limit maximum/minimum question
Ron Czapala
Posts: 2,418
I tried searching the forum, manuals and documentation for an answer but no luck...
I wonder why this code does not limit the value of temp to 20
This code does work of course:
This also works...
I wonder why this code does not limit the value of temp to 20
temp := 0 repeat 30 temp += 1 <# 20
This code does work of course:
temp := 0 repeat 30 temp := temp + 1 <# 20
This also works...
temp := 0 repeat 30 temp += 1 temp <#= 20
Comments
The limit 20 must have higher precedence, so it operates first, limiting the value of 1 to be less than 20...
You could try using parenthesis to make sure the operators work in the order you want...
(temp += 1) <# 20
temp += 1 (<# 20)
etc
temp ++
temp <#= 20
or you can have
temp := (temp + 1) <# 20
In Smalltalk, when you write the equivalent of "temp += 1", that statement carries temp forward so you can write "(temp += 1) <#= 20" and it works as you might think. Spin doesn't work that way. Spin carries the resulting value forward, so it would be like writing "5 <#= 20" if 5 was the new temp value.
Personally, I'd keep the equation simple and use something like:
temp := temp++ <# 20
edit: Mike beat me to it, with a better answer .
If you're going to use that form, you need to write "temp := ++temp <# 20"
You're right. Can I claim that I meant to write it your way and it's a typo (which is the truth)? Or, can I claim that I wrote it the way I did to implement a really fancy "phase shift" in the result?
Yeah. That's my story. More of a "not typing what I was thinking" than an actual typo, I guess.
have worked?
-Phil
I think the most efficient version is temp := (temp + 1) <# 20. I believe temp := ++temp <# 20 will generate the same number of bytecodes, but it results in a wasted store back to hub RAM. The number of cycles will probably be about the same for either expression.
I'm suprised that "temp += 1 <# 20" doesn't give an error and I don't get what "5 <#= 20" would evaluate to...
I think the KISS (keep it simple stupid) methodology is the oftem the best. :surprise:
5 <#= 20 would generate an error because the left side of an assignment operator must be a variable.
Here, temp is augmented by the amount of increment, but by no more than 20.
-Phil
Phil, that does not work - temp will go past 20
-Phil
The "in one step" clarifies it!
Thanks!