home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
tutor
/
l2p130
< prev
next >
Wrap
Text File
|
1990-07-15
|
4KB
|
107 lines
╔════════════════════════════════════════════════════╗
║ Lesson 2 Part 130 F-PC 3.5 Tutorial by Jack Brown ║
╚════════════════════════════════════════════════════╝
┌───────────────────┐
│ Division Review │
└───────────────────┘
This is part 13 of lesson 2! Do you think that part 13 will bring us
bad luck? Have you experimented with division? Remember that there are
two common implementations of integer division. Floored Integer
Division that is the implementation required by the FORTH83 standard,
and hence F-PC which is a FORTH83 standard system and Symmetric Integer
Division which was used by the FORTH79 Standard and FIG Forth systems
In Symmetric Integer Division the quotient is always equal to the
truncated value of the real quotient, or you could also say that the
real quotient is always rounded towards zero. Thus
8/3 = 2.666667(real) = 2 (symmetric integer division)
-8/3 = -2.666667(real) = -2 (symmetric integer division)
No matter what the division type the standard division equation:
m = nq + r must always be satisfied. Thus for the above
remainder ( 8/3) = r = m - nq = 8 - 3x2 = 2 (symmetric)
remainder (-8/3) = r = m - nq = -8 - 3x-2 = -2 (symmetric)
In Floored Integer Division the quotient is the floor of the real
quotient. Remember the elevator analogy.... The floor of the real
number x is the largest integer n that is less than or equal to x.
8/3 = 2.666667(real) = 2 (floored integer division)
-8/3 = -2.666667(real) = -3 (floored integer division)
remainder ( 8/3) = r = m - nq = 8 - 3x2 = 2 (floored)
remainder (-8/3) = r = m - nq = -8 - 3x-3 = -1 (floored)
One thing to keep in mind is that both Symmetric Integer Division and
Floored Integer Division will give the same result if m and n both have
the same sign. That is, if m and n are both positive or if m and n are
both negative the results of Floored and Symmetric will be the same.
If you don't believe this then test it out with the program INTDIV
provided in the previous part of lesson 2.
There is a nother plus that you have when you use Forth. If you really
don't like Floored Integer Division, if you feel that the it is not
worth the trouble you can always just use Symmetric integer division.
Just borrow the definitions S/MOD, S/ and SMOD from the program
presented in the previous part of lesson 2.
Well let's use our division to find the area of a triangle.
A = bh/2 We'll make a word called AREA ( b h -- ) that has for its
output: Base = xxx Height = xxx Area = xxx
: AREA ( b h --)
2DUP SWAP
CR ." Base = " .
." Height = " .
* 2 /
." Area = " . ;
Well.... that was fairly simple... and guess what? It wouldn't matter
whether or not we were using Floored or Symmetric Division the results
would always be the same. The reason is that the dividend bh and the
divisor 2 both have the same sign (positive) and as long as the dividend
and the divisor have the same sign ( both positive or both negative)
both Symmetric and Floored division give the same results!!!
Now try this version of the triangle area program.
: TRIANGLE_AREA ( b h --)
2DUP SWAP
CR ." Base = " .
." Height = " .
* 2 /MOD
." Area = " .
1 BACKSPACES ( 8 EMIT does not back up cursor in F-PC)
ASCII . EMIT
IF ASCII 5 EMIT
ELSE ASCII 0 EMIT
THEN ;
╓───────────────╖
║ Problem 2.17. ║
╙───────────────╜
a) What do you observe when you use 5 and 3 for the base and height?
b) What do you observe when you use 5 and 4 for the base and height?
c) Explain in detail why you get the result you see for question 1.
d) Could you modify the idea contained in the above to compute the Area
of a circle to three decimal places? Hint: It can be done quite easily
and if you can do it you have the potential to become a Forth Super Star!
╓──────────────╖
║ Problem 2.18 ║
╙──────────────╜
We end Lesson 2 with a few easy words. Add stack comments and make
high level defintions of the following ....
\ EASY WORDS
1+ 2+ 1- 2- 2* 2/ ABS NEGATE
┌───────────────────────────────────────┐
│ Please move on to Lesson 3 Part 010 │
└───────────────────────────────────────┘