Max function
kevinj
Posts: 9
I want to find the maximum of 3 8-bit digital numbers. I wanted to know if there was a max function available. I did a search for max and maximum, but nothing relevant came up. I think writing it correctly will be some work because I will need to shift bits and perform some logic. Please let me know. Kevin.
Comments
Find_Max:
· maxVal =·value1
· IF·(value2 > maxVal) THEN
··· maxVal = value2
· ENDIF
· IF·(value3 > maxVal) THEN
··· maxVal = value3
· ENDIF
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
z = z MIN w
Will result in z holding the value of x or y or w, whichever is greatest. You might think that you should use (z = x MAX y), but that is not the way it works. MIN is kind of a floor function, specifying that the result is above the MINimum. The operator is commutative, that is, x MIN y = y MIN x.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
z = x MIN y -y MAX 1
will have the value 1 if x>y, and 0 if x<=y. On the Stamp II, before the advent of PBASIC 2.5, I used to use that kind of statement lot instead of IF-THEN-LABEL logic. But it is convoluted. It can really puzzle you if you have forgotten how it works! On the other hand, PBASIC 2.5 makes the logic so much clearer to follow, using IF-THEN-statements_ELSE.... . I still do use MAX and MIN a lot for things like keeping track of maximum windspeed etc.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com