|
MIN and MAX
These two statememts ask PBASIC to check if the result of the calculation they follow is a minimum of, ar a maximum of the stated value.
x = 30 * datav MIN 60 MAX 120
will do the operation 30 times datav, then if datav was only 1 or zero, MIN would return a value of 60. If datav is 2, 3 or 4, the result is passed through. If datav > 4 then MAX will replace the product with 120
Since the Stamp only uses integer arithmetic, there are no other values of datav to worry about. Heh heh heh, not so fast !
Some people mess it up with MIN 0. if a=4 and b=5 in your program and x = a - b MIN 0 is used, the result a-b is more than zero, because -1 is 255 in the 8-bit world. 255 passes MIN 0 with flying colours. Similarly, with byte variables, (253 + 3) MAX 256 appears to do nothing giving 1 as a result.
To work around this eight-bit under/overflow, we add 2000, so the arithmetic has to be done 16 bits wide.
x = (((a + 2000) - b) MIN 2000) - 2000
will return 0, because the result of the original subtraction was 1999, a positive integer, but it will be replaced by MIN with 2000, the MINimum allowable value.
|