BASIC stamp manual v2.2

4: BASIC Stamp Architecture – /, //
BASIC Stamp Syntax and Reference Manual 2.2 www.parallax.com Page 113
The Divide operator (/) divides variables and/or constants, returning a
16-bit result. Works exactly as you would expect with unsigned integers
from 0 to 65535. Use / only with positive values; signed values do not
provide correct results. Here’s an example of unsigned division:
SYMBOL value1 = W0
SYMBOL value2 = W1
value1 = 1000
value2 = 5
value1 = value1 / value2 ' Divide the numbers
DEBUG value1 ' Show the result (200)
-- or --
value1 VAR Word
value2 VAR Word
value1 = 1000
value2 = 5
value1 = value1 / value2 ' Divide the numbers
DEBUG DEC ? value1 ' Show the result (200)
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 result sign
value1 VAR Word
value2 VAR Word
value1 = 100
value2 = -3200
sign = value1.BIT15 ^ value2.BIT15 ' Determine sign of result
value2 = ABS value2 / ABS value1 ' Divide absolute values
IF (sign = 1) THEN value2 = -value2 ' Negate result if sign = 1
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)
DIVIDE: /
M
ODULUS: //
1
A
ll
2
All
2
All
2
1
1
A
ll
2