home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / source / intdiv.seq < prev    next >
Encoding:
Text File  |  1988-10-23  |  3.0 KB  |  97 lines

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