Multiply Middle '*/'
Syntax
Assignment Variable = Variable */ Variable
Overview
Multiplies unsigned variables and/or constants, returning the middle 16 bits of the 32-bit result.
Operands
Variables may be constants, variables or expressions. Assignment Variable can be any valid variable type.
The Multiply Middle operator has the effect of multiplying a value by a whole number and a fraction. The whole number 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 */ operator allows a workaround for the compiler's integer-only math.
Suppose we are required 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: -
Device = 24FJ64GA002 ' Select the device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Declare Hserial1_Baud = 9600
Declare Hrsout1_Pin = PORTB.14
Dim MyWord as Word
PPS_Output(cOut_Pin_RP14, cOut_Fn_U1TX) ' Make Pin RB14 U1TX
MyWord = 100
MyWord = MyWord */ $0180 ' Multiply by 1.5 [1 + (128/256)]
HrsoutLn Dec MyWord ' Display 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: -
int(fraction * 256)
For example, 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%.
Notes.
This operator enables compatibility with BASIC Stamp code, and, to some extent, melab's compiler code, but is rather obsolete considering the 32-bit capabilities of the Positron16 compiler.
