BASIC stamp manual v2.2
BASIC Stamp Architecture – MIN, MAX
Page 116 • BASIC Stamp Syntax and Reference Manual 2.2 • www.parallax.com
MIN works in positive math only; its comparisons are not valid when
used on two’s complement negative numbers, since the positive-integer
representation of a number like -1 ($FFFF or 65535 in unsigned decimal) is
larger than that of a number like 10 ($000A or 10 decimal). Use MIN only
with unsigned integers. Because of the way fixed-size integers work, you
should be careful when using an expression involving MIN 0. For
example, 0-1 MIN 0 will result in 65535 because of the way fixed-size
integers wrap around.
SYMBOL value1 = W0
SYMBOL value2 = W1
FOR value1 = 100 TO 0 STEP -10 ' Walk value1 from 100 to 0
value2 = value1 MIN 50 ' Use MIN to clamp at 50
DEBUG value2 ' Show "clamped" value
NEXT
-- or --
value VAR Word
FOR value = 100 TO 0 STEP 10 ' Walk value from 100 to 0
DEBUG ? value MIN 50 ' Show value1, use MIN to clamp at 50
NEXT
The Maximum operator (MAX) limits a value to a specified 16-bit positive
maximum. The syntax of MAX is:
value MAX Limit
Where value is a constant or variable value to perform the MAX function
upon and limit is the maximum value that value is allowed to be. Its logic
is, ‘if value is greater than limit, then make result = limit; if value is less than
or equal to limit, make result = value.’ MAX works in positive math only;
its comparisons are not valid when used on two’s complement negative
numbers, since the positive-integer representation of a number like -1
($FFFF or 65535 in unsigned decimal) is larger than that of a number like
10 ($000A or 10 decimal). Use MAX only with unsigned integers. Because
of the way fixed-size integers work, you should be careful when using an
expression involving MAX 65535. For example, 65535+1 MAX 65535 will
result in 0 because of the way fixed-size integers wrap around.
1
1
A
ll
2
All
2
MAXIMUM: MAX