       ͻ
        Lesson 5 Part 090  F-PC 3.5 Tutorial by Jack Brown 
       ͼ

          Ŀ
           Fixed decimal arithmetic demonstration. 
          

Fixed decimal numbers are take up two stack entries, 32 bits, 4 bytes,
or 2 single numbers.  They have a preset decimal postion that is set
using the word FIXED.  All operators are prefixed with  " X " ( " F "
was not used since it is used for F-PC's floating point operators )

Once you have set the number of decimal places you would like to work to
you basicly just prefix all your operators with and " X "

Demonstration:

3 FIXED  \ set to work to three decimal places.  ok

\ To put a fixed point number on the stack you must enter it
\ with a decimal  and follow it with the word FIX
14.3 FIX    2.2 FIX <enter> ok

\ Most operators are just prefixed with X
XOVER X. <enter> 14.300 ok

XDUP X. <enter 2.200  ok

X* XDUP X. <enter> 31.460  ok

2.2 FIX X/ X. <enter> 14.300  ok

1234. FIX  4.04 FIX  <enter> ok

XOVER XOVER X- X. <enter> 1229.960  ok  \ 1234. - 4.04

X+ X. <enter> 1238.040  ok \ 1234. + 4.04
\ *********** LOOK BELOW AND READ *******************
\ To enter fixed point numbers in a definition enter them with
\ the decimal point and the EXACT number of decimal places!
\ as set by the last use of  FIXED !
3 FIXED  <enter> ok

: .AREA ( r -- )  3.142 XOVER X* X* X. ; <enter>  ok

1. FIX  .AREA <enter> 3.142  ok
2. FIX  .AREA <enter> 12.568  ok
3. FIX  .AREA <enter> 28.278  ok
.1 FIX  .AREA <enter> 0.031  ok

\ Note numbers entered with the correct number of decimal places
\ do not need to be followed with FIX
111.000 .AREA <enter> 38712.582  ok
100.000 .AREA <enter> 31420.000  ok

3 FIXED  <enter> ok
3.142 XCONSTANT PI <enter>  ok
XVARIABLE RADIUS <enter>  ok
15.5 FIX RADIUS X! <enter>  ok
RADIUS X@ X. <enter> 15.500  ok
RADIUS X@ PI X* X. <enter> 48.701  ok

Here are a couple of mixed mode fixed point operators which may
prove useful.

\ Multiply two fixed point numbers producing a double fixed point
\  product.
: XM*   ( x1 x2 -- xd=x1*x2 )
       DUP 3 PICK XOR >R     \ Save sign
       DABS 2SWAP DABS      \ ux2 ux1
       UMD*                 \ uqxproduct
       FPLACES 0 ?DO
       BASE @ S>D  DUM/MOD 2ROT 2DROP  \ scale product.
       LOOP
       R> Q+- ;
\ Divide two fixed point numbers leaving fixed pt quotient.
: XM/   ( xd1 x2 -- xquot=x1/x2 )
        DUP 3 PICK XOR >R           \ Save sign
        DABS >R >R QABS             \ uxd1   save divisor
        FPLACES 0
        ?DO BASE @ UQN* LOOP        \ Scale dividend
        R> R> UMD/MOD               \ uxrem uxquot
        2SWAP 2DROP
        R> ?DNEGATE ;

ķ
 Problem 5.12  
Ľ
Use the ideas in the above definitions to implement the word X*/ which
functions like */ except it assumes fixed point numbers.

X*/ ( x1 x2 x3 -- x4 ) \ where x4 = x1*x2/x3

Note: You would like X*/ to retain an accurate 64 bit fixed point
      intermediate product.

ķ
 Problem 5.13  
Ľ
As an exercise in using fixed point arithmetic rewrite the polygon area
case study of Lesson 4 Part 15 so that it all aritmetic is done with
fixed point numbers. You may keep loop counters as single numbers.

Ŀ
  Please Move to Lesson 5 Part 100  

