home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
tutor
/
l5p020
< prev
next >
Wrap
Text File
|
1990-07-15
|
4KB
|
116 lines
╔════════════════════════════════════════════════════╗
║ Lesson 5 Part 020 F-PC 3.5 Tutorial by Jack Brown ║
╚════════════════════════════════════════════════════╝
┌─────────────────────────────────────┐
│ Fixed Point Arithmetic (continued) │
└─────────────────────────────────────┘
We will continue our examination of fixed point arithmetic in lesson 5.
First let's look at some of the common arguments supporting fixed point
arithmetic and its alternative, floating point arithmetic.
┌────────────────────────────────────────────┐
│ Reasons for using Fixed-point arithmetic │
└────────────────────────────────────────────┘
1. To maximize the computers efficiency:
i) by making the program run as fast as possible.
ii) by using as little computer memory as possible.
2. Applications such as:
Operating systems and utilities; Process control;
Graphics; Data base management; Accounting; Simulation;
Editors; Word processors ; etc. do not require floating point.
Read Brodie page 113-116 1st edition or page 101-103 2nd edition
for further discussion.
┌──────────────────────────────────────┐
│ Reasons for using Floating-point. │
└──────────────────────────────────────┘
1. Scientific and Engineering Calculations.
2. Programming time is more highly valued than program
execution time.
3. Application requires numbers with a large dynamic range
( greater than -2 billion to +2 billion ).
4. Computer has hardware floating-point processor, and
thus we do not pay speed penalty for using floating-point.
F-PC has both hardware and software floating point support. In lesson 5
we will be concerned with fixed point arithmetic and some extensions to
the to the words sets given in the files:
DMULDIV.SEQ from SMITH.ZIP, placed in FPC\TOOLS\ by INSTALL program
and a modified form of DMATH.SEQ originally from TANG.ZIP which
we provide.
┌──────────────────────┐
│ Using the */ scalar │
└──────────────────────┘
The */ , " star-slash " the scalar is very important for extending the
range of fixed point arithmetic. This is because the multiplication
of the two single numbers is retained as a 32 bit product and is used
for the division operation.
*/ ( a b c ab/c ) Perform multiplication and then division.
Star-slash multiplies 16-bit a and 16-bit b to form the 32-bit
intermediate product which is then divided by 16-bit c to give a 16-bit
result. The 32-bit intermediate product ensures accurate results when
multiplying by fractions.
We use */ to multiply a by the fraction b/c
Examples:
15000 3 4 */ gives 11250 correct answer
15000 3 * 4 / gives -5134 wrong answer
The second example above does not use the */ and the answer is wrong
because the product of 15000 x 3 = 45000 which is greater than the
largest positive single number ( 32767) and is hence interpreted as a
negative number which is divided by 4 to give the wrong answer.
With */ we get the correct answer because the intermediate product is
retained as a double number and used for the division operation.
Let's use */ to do some percentage calculations. We want to make a word
that finds p % of a number n and leaves the result as m. The stack
picture would be:
\ % ( p n -- m ) where m = p*n/100
: % ( p n -- m ) 100 */ ; \ definition using */
: %% ( p n -- m ) * 100 / ; \ defined without using */
Exercise 5.1
Try 32 1820 %% . and 32 1820 % .
Why are the answers different?
Exercise 5.2 Percentage calculations
Use % to find Result Actual
15 % of 220 33.00
15 % of 222 33.30
15 % of 224 33.60
Your results should convince you that some refinement is required.
Percent calculations with rounding. Use debug to single step through
this definition so that you can discover how it accomplishes the rounding.
: %R 10 */ 5 + 10 / . ;
Sample calculations.
15 220 %R . 33 ok 15 222 %R . 33 ok 15 224 %R . 34 ok
╓─────────────╖
║ Problem 5.1 ║
╙─────────────╜
Give two alternate definitions of %R and test them to make sure they
work.
┌──────────────────────────────────────┐
│ Please move to Lesson 5 Part 030 │
└──────────────────────────────────────┘