home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
tutor
/
l4p060
< prev
next >
Wrap
Text File
|
1990-07-15
|
5KB
|
114 lines
╔════════════════════════════════════════════════════╗
║ Lesson 4 Part 060 F-PC 3.5 Tutorial by Jack Brown ║
╚════════════════════════════════════════════════════╝
┌───────────────────┐
│ Forth CONSTANTs. │
└───────────────────┘
In Forth a constant is used to represent a value which will not change
during the operation of the program. When a constant is created in
Forth both its name and its value must be specified. When a constant is
created its initial value must be on the stack.
CONSTANT <name> ( n -- ) Create a constant <name> whose value is n.
When a constant is executed its initial value will be recovered from
its storage location and be placed on the parameter stack.
<name> ( -- n ) Leave value of <name> on stack.
\ Examples:
24 CONSTANT HOURS/DAY \ Hours per day.
7 CONSTANT DAYS/WEEK \ Days per week.
52 CONSTANT WEEKS/YEAR \ Weeks per year.
12 CONSTANT MONTHS/YEAR \ Months per year.
: HOURS/WEEK ( -- hours ) HOURS/DAY DAYS/WEEK * ;
: HOURS/YEAR ( -- hours ) HOURS/WEEK WEEKS/YEAR * ;
╓─────────────╖
║ Problem 4.7 ║
╙─────────────╜
Create the constants SECONDS/MINUTE and MINUTES/HOUR and then use these
words to calculate the number of seconds and minutes in a Day and a
week. What are you going to do if the answers to any of these should
happen to exceed 65535?
┌────────────────┐
│ The */ Scalar │
└────────────────┘
Let's take take a look at the */ operator that has crept into a few of
our examples. One place it was used was in our random number generator.
*/ ( a b c -- a*b/c)
This operator multiplies a and b and then divides by c, however it is
not exactly the same as the phrase " a b * c / ". This is because
when */ is used the product a*b is retained as a double precision
intermediate or 32bit value until the division has been completed. This
retention of extra accuracy in the product before the division makes
this word useful in scaling or multiplying by fractions.
We like to look at the */ which is often pronounced "star-slash" as the
multiply by a fraction operator.
*/ ( a b c -- a*(b/c) )
Here is an example of using */ in this way to multiply by pi
31416 CONSTANT PI
: *PI ( n -- n*pi ) PI 10000 */ ;
: CIRC ( r -- circ ) 2 * *PI ;
: AREA ( r area ) DUP * *PI ;
╓─────────────╖
║ Problem 4.8 ║
╙─────────────╜
Research project: Find the */ scalar in Brodie's Starting Forth and
report back to the rest of us on the various rational fraction
approximations to some of the common constants used in practical
mathematical calculations. Make up some useful words to illustrate the
use of the */ scalar operator and these rational approximations to
pi, e, sqrt(2) etc.
┌──────────────────────┐
│ Pythagorean triples │
└──────────────────────┘
Here is a little program that looks for pythagorean triples. A
pythagorean triple, is a set of three integers that can represent the
sides of a right angled triangle. You must surely recall the old 3 4 5
right triangle used in geometry. The program below doesn't look
particularly exciting and certainly isn't very efficient. It just uses
brute force to test all every possibility.
\ Pythagorean Triples.
VARIABLE A VARIABLE B VARIABLE C VARIABLE N
VARIABLE AA VARIABLE BB VARIABLE CC
: .ABC ( -- )
CR A @ 12 .R B @ 12 .R C @ 12 .R ;
: TRIPLES ( -- )
25 1 DO I A ! I DUP * AA !
25 1 DO I B ! I DUP * BB !
38 1 DO I C ! I DUP * CC !
AA @ BB @ + CC @ =
IF .ABC THEN
LOOP LOOP
KEY? ?LEAVE ( any key escape ) LOOP ;
╓─────────────╖
║ Problem 4.9 ║
╙─────────────╜
Modify the above program so it will find all triples up to 100. Can you
make the program run faster, using SQRT ? , without using variables?
Modify so that triples are counted. Is it possible to modify the
program so that 3 4 5 and 4 3 5 are not both reported and counted as
they are rally both the same triangle. The triple 6 8 10 is really
just a multiple of the triple 3 4 5 , is it possible to modify the
program so that only unique triples are reported?
┌────────────────────────────────────┐
│ Please move to Lesson 4 Part 070 │
└────────────────────────────────────┘