Compact use of #> - how to?
Erlend
Posts: 612
Is there a shorter way to use the minimum and maximum operators when you want both of them to apply?
snippet: (first line is not relevant to the question)
Erlend
snippet: (first line is not relevant to the question)
iHeat/= 100 iHeat #>= 20 iHeat <#= 100
Erlend
Comments
-Phil
... I find this helps me "see" the process a little easier (I'm a visually-oriented person)
The arrow pointing left means the lowest value. X <# Y returns whichever is the lowest value. It is symmetric. Y <# X means exactly the same thing as X <# Y.
Similarly, arrow pointing right means the highest value. X #> Y returns whichever is the highest value. Likewise symmetric.
Read Phil's as, whichever is lower, heat/100 or 100, then, whichever is higher, that first result or 20.
Read Jon's as, whichever is higher, heat/100 or 20, then, whichever is lower, that first result or 100.
Similar confusion exists with the BASIC Stamp MAX and MIN operators, which act exactly the same, interpreted as floor and ceiling. X MAX Y in the Stamp is the same as Y MAX X and returns the lower of the two values, limited by the ceiling MAX value.
Thanks, I put that right in. And thanks to Tracy for the syntax explananation. It ought to go into the Prop manual at next update.
Erlend
this is one of the things I really love with the stamp and the propeller. Redefining the meaning of common used things.
a >= b is not checking two values, it does change the first one. USE =>. How many times I made that mistake? How many hours debugging, because you do not see it, using other languages over the daytime job?
MIN and MAX are complete opposite of what you think in Stamp and Propeller. It's Limit MIN and Limit Max or something like that. @Chip again.
As Tracy states:
so x MAX y returns the minimum value and x MIN y the maximum value of both presented. Makes sense doesn't it?
Anyways I do love to program the propeller. In opposite to my day job it is fun and helps me to remember why I wanted to be a programmer at all 30-40 years ago.
Sadly I just have time to lurk around here, all my projects are on ice. Daytime Job is crushing me.
Enjoy!
Mike
Can this instead somehow be written using the += and -= operator?
Erlend
... will not do what you want because everything to the right side of = in an expression is evaluated first. What you're doing is...
...because 5 is less than 60. What you're trying -- that isn't working -- is apply an assignment and max value at the same time with a single operator. The way you coded it the first time is correct.
Sometimes it is not so easy to understand how things in Spin can be written compact, ref advice I got at the start of the thread. This time I take comfort in that it is not my limited skills that stops me finding a more compact way, since you say there isn't one. Time to go on and learn more about other parts of Spin.
Erlend
read the code at a later date without confusion:
That way the code for constrain() remembers the unusual syntax on your behalf.
can be read naturally as, "Add 5 to x and force the result to be great than or equal to 10 and less than or equal to 30." Incorporating the terms "min" and "max" just confuses things, IMO.
-Phil
Writing compact code is not always advantageous. Remember, listings are for humans and rule #1 should be readability. Compact variations do not always mean an increase in speed -- the only sure what to know is to test which is easy to do with the Propeller using the cnt register.
I suggest you focus on working code first, [possible] optimization later. BTW... I have to remind myself of this, too. The other day I optimized a bug into a program that wasn't finished and spent two days looking for it.
I'm not bothered by the min and max operators, so I do it inline, yet your suggestion is a good one.
-Phil
I like to avoid reading it as "greater than or equal to", which suggests non-symmetry.
Unfortunately, #>, which is symmetric, looks an awful lot like >, which is not. A #> B produces the same result as B #> A. I see the arrow pointing to the right up the number line and read, "take the bigger of A and B".
-Phil
Given a, b and c, the median value is,
median_abc := (a #> b) <# (a <# b #> c)}
In the following I've listed in the 1st column the 6 possible orderings of a, b and c lowest to highest. The second column is the result of evaluating the two terms in parentheses. The final column is the result of evaluating the final <#, which is in fact the median. Cases where two or three values are equal falls out naturally. While this can be done with a bunch of if-else constructs, the above is above all concise. In fact, John could, as he asked, win the enigmatic if not unreadable code award.