home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / source / p2_16dc.seq < prev    next >
Text File  |  1990-03-31  |  3KB  |  98 lines

  1. \ INTDIV PROGRAM FOR P1.16
  2. \ Revised by Dickson Cheng  03/31/90 17:19:12.07
  3.  
  4. \ Make grid for plot.
  5. : GRID          ( -- )
  6.         0 10 AT 60 0 DO 197 EMIT 196 EMIT 2 +LOOP
  7.                         197 EMIT
  8.                 21 1 DO 30 I AT 197 EMIT LOOP
  9.            9 9 AT -10 . 50 9 AT 10 .
  10.           28 0 AT  10 . 27 20 AT -10 . ;
  11.  
  12. \ Display divisor.
  13. : .DIVISOR      ( n -- )
  14.         40 1 AT ." Divisor = n = " . ;
  15.  
  16. \ Display division equations.
  17. : .EQN          ( -- )
  18.         40 18 AT ."  m  = nq + r"
  19.         40 19 AT ." m/n =  q + r/n"
  20.         0 22 AT
  21.         ." ( m = dividend, n = divisor, q = quotient, and r = remainder )" ;
  22.  
  23. \ Floored integer division... quotient vs dividend.
  24. : FQVSM         ( n -- )
  25.         1   1 AT ." FLOORED q vs m" DUP .DIVISOR .EQN
  26.        30   0 AT ASCII q EMIT  61 10 AT ASCII m EMIT
  27.         5  21 AT ." Quotient vs Dividend for Floored Integer Division"
  28.        16 -15 DO I OVER / 10 SWAP -
  29.                  I 15 + 2* SWAP AT
  30.                  4 EMIT LOOP DROP ;
  31.  
  32. \ Floored integer division... remainder vs dividend.
  33. : FRVSM         ( n -- )
  34.         1   1 AT ." FLOORED r vs m " DUP .DIVISOR .EQN
  35.        30   0 AT ASCII r EMIT 62 10 AT ASCII m EMIT
  36.         5  21 AT ." Remainder vs Dividend for Floored Integer Division"
  37.        16 -15 DO I OVER MOD 10 SWAP -
  38.                  I 15 + 2* SWAP AT
  39.                  4 EMIT LOOP DROP ;
  40.  
  41. \ Symmetric integer division form of /MOD
  42. : S/MOD         ( m n -- r q )
  43.         2DUP XOR 0<
  44.         IF      2DUP
  45.                 ABS SWAP ABS SWAP
  46.                 / NEGATE
  47.                 -ROT 2 PICK
  48.                * -
  49.                SWAP
  50.         ELSE /MOD
  51.         THEN ;
  52.  
  53. \ Symmetric integer division form of /
  54. : S/            ( m n -- q )
  55.         S/MOD NIP ;
  56.  
  57. \ Symetric integer division form of MOD
  58. : SMOD          ( m n -- r )
  59.         S/MOD DROP ;
  60.  
  61. \ Symmetric integer division... quotient vs dividend.
  62. : SQVSM         ( n -- )
  63.         1   1 AT ." SYMMETRIC r vs m " DUP .DIVISOR .EQN
  64.        31   0 AT ASCII q EMIT 62 10 AT ASCII m EMIT
  65.         5  21 AT ." Quotient vs Dividend for Symmetric Integer Division"
  66.        16 -15 DO I OVER S/ 10 SWAP -
  67.                  I 15 + 2* SWAP AT
  68.                  4 EMIT LOOP DROP ;
  69.  
  70. \ Symmetric integer division... remainder vs dividend.
  71. : SRVSM         ( n -- )
  72.         1   1 AT ." SYMMETRIC r vs m " DUP .DIVISOR .EQN
  73.        31   0 AT ASCII r EMIT 62 10 AT ASCII m EMIT
  74.         5  21 AT ." Remainder vs Dividend for Symmetric Integer Division"
  75.        16 -15 DO I OVER SMOD 10 SWAP -
  76.                  I 15 + 2* SWAP AT
  77.                  4 EMIT LOOP DROP ;
  78.  
  79. \ Run through the four plots for an integer divisor.
  80. : INTDIV        ( n -- )
  81.         -10 MAX 10 MIN ?DUP
  82.         IF
  83.         CURSOR-OFF
  84.         DUP DARK GRID FQVSM KEY DROP
  85.         DUP DARK GRID FRVSM KEY DROP
  86.         DUP DARK GRID SQVSM KEY DROP
  87.         DUP DARK GRID SRVSM KEY DROP
  88.         CURSOR-ON
  89.         DROP
  90.         ELSE DARK CR ." Can't divide by zero dummy"
  91.         THEN ;
  92.  
  93. : MESSAGE       ( -- )
  94.         DARK CR ." To run INTDIV type n INTDIV." ;
  95.  
  96. MESSAGE
  97.  
  98.