BASIC Stamp Architecture – //, MIN
Page 70
•
BASIC Stamp Programming Manual 2.0b
•
www.parallaxinc.com
A workaround to the inability to divide signed numbers is to have your
program divide absolute values, then negate the result if one (and only
one) of the operands was negative. All values must lie within the range of
-32767 to +32767. Here is an example:
Sign VAR BIT ' Bit to hold the sign.
Value1 VAR WORD
Value2 VAR WORD
Value1 = 100
Value2 = - 3200
Sign = Value1.BIT15 ^ Value2.BIT15 ' Sign = (Value1 sign) XOR (Value1 sign).
Value2 = ABS Value2 / ABS Value1 ' Divide absolute values.
IF Sign = 0 THEN Skip0 ' Negate result if one of the
Value2 = - Value2 ' argument was negative.
Skip0:
DEBUG SDEC ? Value2 ' Show the result (-32)
The Modulus operator (//) returns the remainder left after dividing one
value by another. Some division problems don’t have a whole-number
result; they return a whole number and a fraction. For example, 1000/6 =
166.667. Integer math doesn’t allow the fractional portion of the result, so
1000/6 = 166. However, 166 is an approximate answer, because 166*6 =
996. The division operation left a remainder of 4. The // (double-slash)
returns the remainder of a given division operation. Naturally, numbers
that divide evenly, such as 1000/5, produce a remainder of 0. Example:
SYMBOL Value1 = W0
SYMBOL Value2 = W1
Value1= 1000
Value2= 6
Value1= Value1 // Value2 ' Get remainder of Value1 / Value2.
DEBUG Value1 ' Show the result (4).
-- or --
Value1 VAR WORD
Value2 VAR WORD
Value1= 1000
Value2= 6
Value1= Value1 // Value2 ' Get remainder of Value1 / Value2.
DEBUG DEC ? Value1 ' Show the result (4).
The Minimum operator (MIN) limits a value to a specified 16-bit positive
minimum. The syntax of MIN is:
2
e
2
sx
2
p
2
1
2
e
2
sx
2
p
2
M
ODULUS
: //
M
INIMUM
: MIN
1
2
e
2
sx
2
p
2
1
2
e
2
sx
2
p
2
Summary of Contents for BASIC Stamp 2e
Page 1: ...BASIC Stamp Programming Manual Version 2 0c...
Page 34: ...Quick Start Guide Page 32 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Page 340: ...ASCII Chart Page 338 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Page 342: ...Reserved Words Page 340 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Page 346: ...Conversion Formatters Page 344 BASIC Stamp Programming Manual 2 0b www parallaxinc com...