Shop OBEX P1 Docs P2 Docs Learn Events
Min, max — Parallax Forums

Min, max

chickchick Posts: 16
edited 2005-12-30 21:07 in BASIC Stamp
I have been working on a program that needs to use the instruction MIN. But it doesn't seem to work.

N is a var (8 bit) from 0-255. At one piont in the program I need to lower n from its midpiont of 128 in increments of 40.

The line is written: n=n-40 min 3
Yet what happens is that n goes from 128 to 88 to 48 to 8, then instead of stopping at 3 it jumps to 255 and keeps subtracting. 8 gets it to 0 or 255 and subtrackt the remaining 32 and ends up at 223 and then 183, etc.

Any thoughts on the fix.

Thanks in advance.

Lou

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-12-30 20:59
    MIN doesn't work the way you expect because it provides a floor to positive values, this means the result to be compared may not be a negative number, something which is violated when you perform 8-40. There are many ways of computing what you want, one way is to do:

    n=n-40
    IF n > 128 THEN n=3

    This catches the situation when a negative occurs and forces it to the value 3.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • chickchick Posts: 16
    edited 2005-12-30 21:07
    Paul,

    Many thanks, that solved the problem.

    Lou
Sign In or Register to comment.