Shop OBEX P1 Docs P2 Docs Learn Events
Max function — Parallax Forums

Max function

kevinjkevinj Posts: 9
edited 2005-10-19 01:38 in BASIC Stamp
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

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-10-18 23:03
    There is no function that lets you pass three values and returns the highest -- but it's easy to code:

    Find_Max:
    · maxVal =·value1
    · IF·(value2 > maxVal) THEN
    ··· maxVal = value2
    · ENDIF
    · IF·(value3 > maxVal) THEN
    ··· maxVal = value3
    · ENDIF

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2005-10-18 23:04
    z = x MIN y
    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 WilliamsJon Williams Posts: 6,491
    edited 2005-10-18 23:05
    Man, you're smart!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2005-10-18 23:21
    More like convoluted. It comes from experience, thinking about integer functions. One can make some pretty interesting composite functions that use MAX and MIN in combination. For example,
    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
  • kevinjkevinj Posts: 9
    edited 2005-10-19 01:38
    Sorry, I looked in the reference manual, and didn't see that the greater than or less than functions, so I didn't think we could use them. I like how MIN is used. Clever. Kevin.
Sign In or Register to comment.