home *** CD-ROM | disk | FTP | other *** search
- \ Welcome to Lesson 5 Part 4
-
- \ In Problem 5.1 we suggested that you should provide some
- \ alternate defintions of %R.
- \ Using */ we rounded this way.
- : %R1 ( p n -- m ) 10 */ 5 + 10 / ;
- : %R2 ( p n -- m ) 50 */ 1+ 2/ ;
-
- \ Rounding using */MOD
- : %R3 ( p n -- m ) 100 */MOD SWAP 50 + 100 / + ;
- : %R4 ( p n -- m ) 100 */MOD SWAP 49 > NEGATE + ;
-
- \ It would be intersting to know just which of these words is the fastest.
- \ F-PC has a very easy to use word for checking the elapsed time of words.
- COMMENT:
- Problem 5.4
- Investigate the words TIMER , TIME-RESET and .ELAPSED found in F-PC.
- See if you can discover which of the above versions of rounded percent
- is the fastest. Well... thats not really a fair question. All versions
- are so fast that you cannot get a proper reading using:
- TIMER 15 224 %R1
- Here is a hint on how you might solve the problem.
- COMMENT;
-
- : TIME1 ( -- ) TIME-RESET
- 1000 0 DO 1000 O DO
- \ blank loop ( 0-54 micro-sec )
- 15 224 %R1 DROP ( ?? micro-sec )
- LOOP LOOP
- TIME-ELAPSED B>SEC . 230 EMIT ." -seconds for one pass." ;
-
- : TIMEB ( -- ) TIME-RESET
- 1000 0 DO 1000 0 DO
- \ blank loop ( 0-54 micro-sec )
- \ 15 224 %R1 DROP ( ?? micro-sec )
- LOOP LOOP
- TIME-ELAPSED B>SEC . 230 EMIT ." -seconds for one pass." ;
-
- COMMENT:
- A review of Loops.
-
- The Infinite Loop.
-
- The infinite loop with no exit. This is recommended only for an end
- user application. Examples: FORTH's QUIT & our version MYQUIT that was
- presented in lesson 1 part 8. Dave Brown's first solution to problem
- 4.4 on Pythagorean triples also used an infinite loop with a hidden
- escape hatch.
-
- ... (step 1) BEGIN (step2) AGAIN (step3) ...
-
- step 1 is executed once.
- step 2 is repeated forever.
- step 3 is never executed.
-
-
- The Infinite Loop with EXIT escape hatch.
-
- ... (step1) BEGIN (step2)
- (condition) IF EXIT THEN
- (step3)
- AGAIN (step4) ...
- Example:
- COMMENT;
-
- : COUNT.UP
- 0 \ step 1
- BEGIN 1+ DUP CR . \ step 2
- KEY? \ condition
- IF DROP EXIT THEN
- \ step 3 not present
- AGAIN ." DONE" ; \ step 4
-
- COMMENT:
- step 1 is executed once
- step 2 is repeated until condition is true.
- step 3 is repeated each time exit condition fails.
- step 4 will never be executed because EXIT passes control back
- to the calling word!!
-
- Examples: See #IN and GAME in lesson 3 part 11 for examples of the
- infinite loop with EXIT escape hatch.
-
- The Indefinite Loop.
-
- In the indefinite loop the main action is repeated until a condition is
- true.
- ... (step1) BEGIN (step2)
- (condition)
- UNTIL (step3) ...
- Example:
- COMMENT;
-
- : COUNT-UP ( -- )
- 0 \ step 1
- BEGIN 1+ DUP CR . \ step 2
- KEY? \ condition
- UNTIL DROP ." DONE" ; \ step 3
- COMMENT:
- step 1 is executed once.
- step 2 is executed and then
- (condition) is tested.
- if condition is false step 2 is executed again,
- if condition is true then step 3 is executed.
- COMMENT;
-
-