u*/mod
(scale) uses a 32-bit intermediate result.
FlashForth also provides arithmetic operators
for double numbers (32-bit integers), signed and unsigned.
See the companion reference sheet [2] for a more complete list.
word | effect | comment |
+ |
( n1 n2 -- n1+n2) |
sum |
- |
( n1 n2 -- n1-n2) |
difference |
* |
( n1 n2 -- n1*n2) |
product |
/ |
( n1 n2 -- n1/n2) |
quotient |
mod |
( n1 n2 -- n.rem) |
remainder |
*/ |
( n1 n2 n3 -- n1*n2/n3) |
scaled quotient |
u/ |
( u1 u2 -- u1/u2) |
unsigned quotient |
u/mod |
( u1 u2 -- rem quot) |
remainder and quotient |
u*/mod |
( u1 u2 u3 -- rem quot) |
scaled remainder and quotient |
For an example of using arithmetic operators, consider the
conversion of temperature values from Celcius to Fahrenheit
using the formula n2 = (n1*9/5 + 32)
.
decimal ok<#,ram> : to-f-1 ( n1 -- n2) 9 * 5 / #32 + ; ok<#,ram> 0 to-f-1 . 32 ok<#,ram> 100 to-f-1 . 212 ok<#,ram> 500 to-f-1 . 932 ok<#,ram> 5000 to-f-1 . -4075 ok<#,ram>This simple function works fine, up until the intermediate result
(n1*9)
overflows the 16-bit cell.
With a bit more bother, we can make use of the scaled operator
to avoid overflow of the intermediate result.
Again, the following function computes expression (u1*9/5 + 32)
but now uses the scale operator */
.
This operator uses a 32-bit intermediate result to avoid overflow.
: to-f-2 ( n1 -- n2) 9 5 */ #32 + ; ok<#,ram> 0 to-f-2 . 32 ok<#,ram> 100 to-f-2 . 212 ok<#,ram> 500 to-f-2 . 932 ok<#,ram> 5000 to-f-2 . 9032 ok<#,ram>Note that not all of the arithmetic operators are part of the core FlashForth that is written in PIC18 assembly language and, to get the scale operator, you will need to load the
math.txt
file of word definitions before
trying this second example. (See AppendixA.)