       ͻ
        Lesson 2 Part 120  F-PC 3.5 Tutorial by Jack Brown 
       ͼ

\ Program:  INTDIV
\ Function: Plots r vs m and q vs m for floored
\           and symmetric division.

\ Make grid for plot.
: GRID ( -- )
        0 10 AT 60 0 DO ASCII + EMIT ASCII - EMIT 2 +LOOP
                        ASCII + EMIT
                21 1 DO 30 I AT ASCII + EMIT LOOP
         9  9 AT -10 .  50  9 AT  10 .
        28  0 AT  10 .  27 20 AT -10 . ;

\ Display divisor.
: .DIVISOR ( n -- )
      40 1 AT ." Divisor = n = " . ;

\ Display division equations.
: .EQN ( -- )
      40 18 AT ."  m  = nq + r"
      40 19 AT ." m/n =  q + r/n"
      0 22 AT
     ." ( m = dividend, n = divisor, q = quotient, and r = remainder)" ;


\ Floored integer division... quotient vs dividend.
: FQVSM ( n -- )
       1  1 AT ." FLOORED q vs m"   DUP  .DIVISOR .EQN
      30  0 AT ASCII q EMIT  61 10 AT ASCII m EMIT
       5 21 AT ." Quotient vs Dividend for Floored Integer Division"
      16 -15 DO I OVER /  10 SWAP -
                 I 15 + 2* SWAP AT
                 ASCII o EMIT LOOP DROP ;

\ Floored integer divison... remainder vs dividend.
: FRVSM ( n -- )
       1 1 AT ." FLOORED r vs m "   DUP  .DIVISOR .EQN
       30 0 AT ASCII r EMIT 62 10 AT ASCII m EMIT
        5  21 AT ." Remainder vs Dividend for Floored Integer Division"
       16 -15 DO I OVER MOD 10 SWAP -
                 I 15 + 2* SWAP AT
                 ASCII o EMIT LOOP DROP ;

\ Symmetric integer division form of /MOD
: S/MOD ( m n -- r q )
       2DUP XOR 0<
       IF   2DUP
            ABS SWAP ABS SWAP    \ m n |m| |n|
            / NEGATE             \ m n q
            -ROT 2 PICK          \ q m n q
            * -                  \ q r=m-nq
            SWAP                 \ r q
       ELSE /MOD
       THEN ;

\ Symmetric integer division form of /
: S/   ( m n -- q )
       S/MOD NIP ;

\ Symmetric integer division form of MOD
: SMOD ( m n -- r )
       S/MOD DROP ;

\ Symmetric integer division... quotient vs dividend.
: SQVSM ( n -- )
       1 1 AT ." SYMMETRIC q vs m"  DUP  .DIVISOR  .EQN
       30 0 AT ASCII q EMIT  62 10 AT ASCII m EMIT
        5  21 AT ." Quotient vs Dividend for Symmetric Integer Division"
       16 -15 DO I OVER S/  10 SWAP -
                 I 15 + 2* SWAP AT
                 ASCII o EMIT LOOP DROP ;

\ Symmetric integer division ... remainder vs dividend.
: SRVSM ( n -- )
       1 1 AT ." SYMMETRIC r vs m "  DUP .DIVISOR  .EQN
       31 0 AT ASCII r EMIT 62 10 AT ASCII m EMIT
        5  21 AT ." Remainder vs Dividend for Symmetric Integer Division"
       16 -15 DO I OVER SMOD 10 SWAP -
                 I 15 + 2 * SWAP AT
                 ASCII o EMIT LOOP DROP ;


\ Run through the four plots for an integer divisor.
: INTDIV ( n -- )
       -10 MAX 10 MIN ?DUP
       IF
       DUP DARK GRID FQVSM  KEY DROP
       DUP DARK GRID FRVSM  KEY DROP
       DUP DARK GRID SQVSM  KEY DROP
       DUP DARK GRID SRVSM  KEY DROP
       DROP 0 22 AT
       ELSE DARK ." Can't divide by zero dummy"
       THEN  ;

ķ
 Problem 2.16  
Ľ
Modify the program to use the IBM extended character set to produce
a cleaner display.

Ŀ
  Please Move to Lesson 2 Part 130 

