       ͻ
        Lesson 5 Part 040  F-PC 3.5 Tutorial by Jack Brown 
       ͼ


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 +    ;

Example of execution, the exact value is 33.6
15 224 %R1 . 34  ok
15 224 %R2 . 34  ok
15 224 %R3 . 34  ok
15 224 %R4 . 34  ok

ķ
 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.
:  TIME.IT ( -- )
   CR ." This could take several minutes!! please wait!!"
   CR ." as we are executing test loop 1,000,000 times."
   TIME-RESET
   1000   0 DO  1000 0 DO
             \  blank  loop          ( 54 micro-sec?  )
                15 224 %R1 DROP      (  ??  micro-sec )
            LOOP  LOOP
   TIME-ELAPSED B>SEC  . 230 EMIT ." -seconds for one pass." ;


          Ŀ
            The Infinite Loop. ( A review of LOOPs ) 
          

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:

: 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
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:
: COUNT-UP  ( -- )
         0                         \ step 1
         BEGIN  1+ DUP CR .        \ step 2
                KEY?               \ condition
         UNTIL  DROP ." DONE" ;    \ step 3
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.

Ŀ
   Please move to Lesson 5 Part 050  

