home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / tutor / l6p010 < prev    next >
Text File  |  1990-07-15  |  4KB  |  110 lines

  1.        ╔════════════════════════════════════════════════════╗
  2.        ║ Lesson 6 Part 010  F-PC 3.5 Tutorial by Jack Brown ║
  3.        ╚════════════════════════════════════════════════════╝
  4.  
  5.                   ┌─────────────────────────┐
  6.                   │   Topics for Lesson 6   │
  7.                   └─────────────────────────┘
  8.  
  9.           0) Review of Lesson 5
  10.           1) Forth String Primitives
  11.           2) Numeric Output Formatting.
  12.           3) COMPILE [COMPILE] and IMMEDIATE
  13.           4) Simple String Package.
  14.           5) Case  Statement
  15.           6) Line Editor Case Study.
  16.  
  17.          ┌──────────────────────────────────────────┐
  18.          │  Review of the */ , "star-slash" scalar. │
  19.          └──────────────────────────────────────────┘
  20.  
  21. */  ( a b c -- ab/c )
  22.  
  23. Perform multiplication and then division. Star-slash multiplies 16-bit
  24. a  and  16-bit  b  to form a 32-bit intermediate result which is then
  25. divided by 16-bit c to give a 16-bit result.  The 32-bit intermediate
  26. product ensures accurate results when multiplying by fractions. We use
  27. */  to multiply a  by the fraction b/c
  28.  
  29. Examples:
  30.  32-bit intermediate product results in correct answer.
  31.  15000   3   4  */      gives   11250     correct answer
  32.  16-bit intermediate product results in overflow and the
  33.  15000   3   *  4 /     gives   -5134     wrong   answer
  34.  
  35.            ┌──────────────────────────────┐
  36.            │  Review of the */MOD Scalar  │
  37.            └──────────────────────────────┘
  38.  
  39. The "star-slash-mod", scalar and its application to rounding fractions.
  40.  
  41.  */MOD  ( a b c -- r q )
  42.  
  43. Compute ab/c with 32-bit intermediate product ab  and leave
  44. quotient q and remainder r .  Note:  Forth-83 */MOD uses
  45. signed values  a b c  and uses floored division.
  46.  
  47. Rounding calculations that involve division.
  48. : %R1    10 */     5 +            10 /  .    ;
  49. : %R2    50 */     1+             2/    .    ;
  50. : %R3   100 */MOD  SWAP 50 +  100 / +   .    ;
  51. : %R4   100 */MOD  SWAP 49 > NEGATE +   .    ;
  52.  
  53. \ Fractions:  see Brodie page 125 for more.
  54. : *PI       355     113 */ ;
  55. : *SQRT(2)  19601 13860 */ ;
  56. : *E        28667 10546 */ ;
  57.  
  58.           ┌─────────────────────────────────────────────┐
  59.           │   Review of Infinite and Indefinite Loops.  │
  60.           └─────────────────────────────────────────────┘
  61.  
  62. The infinite loop with no exit.
  63.      ... (step 1)  BEGIN   (step2)  AGAIN   (step3) ...
  64.  
  65. The infinite loop with EXIT  escape hatch.
  66.    ... (s1) BEGIN (s2)
  67.                   (condition) IF EXIT THEN
  68.                   (s3)
  69.             AGAIN (s4) ...
  70.  
  71. Indefinite Loops
  72.     ... (s1)  BEGIN   (s2)
  73.                      (condition)
  74.               UNTIL   (s3) ...
  75.  
  76.  
  77.  ... (s1)  BEGIN  (s2)
  78.                   (condition)
  79.            WHILE  (s3)
  80.            REPEAT (s4) ...
  81.  
  82.          ┌───────────────────────┐
  83.          │   Review of Do Loops  │
  84.          └───────────────────────┘
  85.  
  86.   ... (s1)  l i   DO  (s2)      LOOP  (s3) ...
  87.   ... (s1)  l i   DO  (s2)  n  +LOOP  (s3) ...
  88.   ... (s1)  l i  ?DO  (s2)      LOOP  (s3) ...
  89.   ... (s1)  l i  ?DO  (s2)  n  +LOOP  (s3) ...
  90.  
  91.         ┌─────────────────────────┐
  92.         │   Leaving Loops early.  │
  93.         └─────────────────────────┘
  94.  
  95.   (s1)  l i   DO    (s2)
  96.                     (condition) IF  (s3) LEAVE THEN
  97.                     (s4)
  98.               LOOP  (s5) ...
  99.  
  100. This is an alternative form if step 3 is not required.
  101.  
  102.   (s1)  l i   DO    (s2)
  103.                     (condition) ?LEAVE
  104.                     (s4)
  105.               LOOP  (s5) ...
  106.  
  107. ┌─────────────────────────────────────┐
  108. │   Please Move to Lesson 6 Part 020  │
  109. └─────────────────────────────────────┘
  110.