home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
tutor
/
l2p080
< prev
next >
Wrap
Text File
|
1990-07-15
|
5KB
|
105 lines
╔════════════════════════════════════════════════════╗
║ Lesson 2 Part 080 F-PC 3.5 Tutorial by Jack Brown ║
╚════════════════════════════════════════════════════╝
┌─────────────────────────────────────────────────────────┐
│ Floored Division vs Symmetric or truncated Division │
└─────────────────────────────────────────────────────────┘
You already know that a number on Forth's parameter stack is a 16 bit
integer. What this means is that when we divide two numbers we are
going to get an integer result. The problem is there is more than one
way to define what the result should be. There is the intuitive method
called Symmetric Integer Division that is used by most computer
languages including the FORTH79 Standard Forth and FIG Forth. An
alternative method is Floored Integer Division. This method is used by
the FORTH83 Standard Forth's and that means F-PC. Some say that Floored
integer division is superior to Symetric integer division but we will
let you be the judge. Anyway we don't really have any choice since we
are working with a FORTH83 Standard system.
First some notation and the integer division equations that any integer
division algorithm must satisfy.
m = dividend, n = divisor, q = quotient, and r = remainder.
The equations are:
m r
m = n q + r or --- = q + ---
n n
Integer division is a mathematical function of two integers ( the
dividend m and the divisor m ) that results in and integer quotient q
and and integer remainder r. Whatever the algorithm or type of integer
divison the above equations must be satisfied.
The Forth words that are used for (floored) integer division in FORTH83
are:
/MOD ( m n -- r q ) \ Leave remainder and floored quotient.
/ ( m n -- q ) \ Leave floored quotient only.
MOD ( m n -- r ) \ Leave remainder only.
In FORTH83 the integer quotient q that is left is the FLOOR of the
real quotient (decimal answer) that results from m/n. # elevator
4-|--
Do you want to know what the FLOOR of a real number is? |
3-|--
Well... Suppose we want to divide 5 by 2. 2.5=real answer --> |
In this case m = 5 and n = 2. In FORTH83 and F-PC the 2-|--
which use Floored integer division the value you choose |
for q is the floor of the real value. To explain what we 1-|--
mean by the floor of a real number think of a vertical |
number line where the integers represent the floors of 0-|--
in a building where the elevator can stop. Then by the |
floor of the number 2.5 we mean the first floor below 2.5 -1-|--
which will be floor number 2. So.. when we divide 5 by 2 |
the integer answer for q will be the floor of 2.5=5/2 or -2-|--
just 2. Now we know this is just what you expected! -2.5>>>|
-3-|--
But wait!! what is the answer when m = -5 ?? Sorry your |
wrong, its not -2!! Watch. -5/2 = -2.5 look at the -4-|--
elevator next door... The Floor of -2.5 is -3!!!
SO>>>>> -5/2 = -3 which is not what you expected and it is not
intuitive but it is the floor of the actual real quotient and that
is what Floored Integer Division gives and that is what FORTH83 and
F-PC use!!!
By the way there is more good(bad?) news. Once q has been determined
the remainder r must be choosen so as to satisfy the integer division
equations above. This means that for Floored Integer Division:
floor
m n (real q) q r = m - nq ( m = nq + r)
----- ----- ------ ---- -------------
5 2 2.5 2 1 = 5 - 2x2
-5 2 -2.5 -3 1 = -5 - 2x-3
5 -2 -2.5 -3 -1 = 5 - -2x-3
-5 -2 2.5 2 -1 = -5 - -2x2
*** Especially study the last two entries than complete the following:
8 3 ? ? ?
-8 3 ? ? ?
8 -3 ? ? ?
-8 -3 ? ? ?
Note: you should beable to complete the table without using your
computer.
╓──────────────╖
║ Problem 2.15 ║
╙──────────────╜
Make up a similar table for Symmetric Integer Division.
For symmetric integer division the value for q is choosen by simply
truncating any decimal fraction that occurs in the real value of q.
For the first four entries above the q's would be 2, -2, -2, and 2.
You still have to complete the table and find all values for the
remainder that satisfy the the division equations.
Here is a mathematical definition of the floor of a real number:
The floor of the real number x, which we can denote by floor(x) is the
largest integer n which is less than of equal to x.
┌────────────────────────────────────┐
│ Please move to Lesson 2 Part 090 │
└────────────────────────────────────┘