4: BASIC Stamp Architecture – /
BASIC Stamp Programming Manual 2.0c • www.parallaxinc.com • Page 69
is the upper byte of the multiplier (0 to 255 whole units) and the fraction is
the lower byte of the multiplier (0 to 255 units of 1/256 each). The */ (star-
slash) instruction gives you an excellent workaround for the BASIC
Stamp's integer-only math. Suppose you want to multiply a value by 1.5.
The whole number, and therefore the upper byte of the multiplier, would
be 1, and the lower byte (fractional part) would be 128, since 128/256 = 0.5.
It may be clearer to express the */ multiplier in hex—as $0180—since hex
keeps the contents of the upper and lower bytes separate. Here's an
example:
Value1 VAR WORD
Value1= 100
Value1= Value1*/ $0180 ' Multiply by 1.5 [1 + (128/256)]
debug ? Value1 ' Show result (150).
To calculate constants for use with the */ instruction, put the whole
number portion in the upper byte, then use the following formula for the
value of the lower byte: Hint: INT( fraction * 256). For instance, take Pi (
π,
3.14159). The upper byte would be $03 (the whole number), and the lower
would be INT(0.14159 * 256) = 36 ($24). So the constant Pi for use with */
would be $0324. This isn’t a perfect match for Pi, but the error is only
about 0.1%.
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).
2
2
2
2
2
2
DIVIDE
2
2
2