home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
source
/
p2_16bs.seq
< prev
next >
Wrap
Text File
|
1990-03-29
|
3KB
|
101 lines
\ Comp 462
\ Balraj Sidhu Set: 14D4
\ Date: March 29, 1990
\ Problem 2.16
\ Program INTDIV
\ Function: Plots r vs m and q vs m for floored and symmetric division.
\ plot point
: plot ( -- )
16 -15 do i over / 10 swap - I 15 + 2* swap at
ascii o emit loop drop ;
\ make grid for plot
: grid ( -- )
0 10 at 60 0 do 197 emit 196 emit 2 +loop
ascii + emit
21 1 do 30 i at 197 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 g vs m" dup .divisor .eqn
30 0 at ascii q emit 61 10 at ascii m emit
5 21 at ." Quotient vs Dividend fo Floored Integer Division"
16 -15 do i over / 10 swap - I 15 + 2* swap at
219 emit loop drop ;
\ floored integer division... 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
219 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 Divsion"
16 -15 do i over s/ 10 swap - I 15 + 2* swap at
219 emit loop drop ;
\ symmetric integer divsion... remainder vs dividend.
: srvsm ( n -- )
1 1 at ." SYMMETRIC r vs m" dup .divisor .eqn
31 0 at ascii q emit 62 10 at ascii m emit
5 21 at ." Quotient vs Dividend for Symmetric Integer Divsion"
16 -15 do i over smod 10 swap - I 15 + 2* swap at
219 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"
then ;