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

  1.        ╔════════════════════════════════════════════════════╗
  2.        ║ Lesson 5 Part 030  F-PC 3.5 Tutorial by Jack Brown ║
  3.        ╚════════════════════════════════════════════════════╝
  4.  
  5.              ┌───────────────────────────────────────┐
  6.              │  Using */ for Rational Approximations │
  7.              └───────────────────────────────────────┘
  8.  
  9. One way to use */ is in the rational approximations to common
  10. constants. These are from Starting Forth page 122 1st ed, p109 2nd ed.
  11.  
  12. : *PI       355     113 */ ;
  13. : *SQRT(2)  19601 13860 */ ;
  14. : *SQRT(3)  18817 10864 */ ;
  15. : *E        28667 10546 */ ;
  16.  
  17. ╓──────────────╖
  18. ║ Problem 5.2  ║
  19. ╙──────────────╜
  20. If you are going to use integer arithmetic in you programs part of
  21. your responsibility is to make sure both you and your users know
  22. the range of valid inputs each word or program.  Consider the simple
  23. programs given below.  For each of these find the smallest and largest
  24. values of the inputs that will still give valid results.
  25.  
  26. \ Area of circle
  27. : AREA ( r -- a )    DUP * *PI  ;
  28.  
  29. \ Volume of sphere  v = pi*r*r*r*4/3
  30. : VS    ( r -- vol )   DUP DUP * * *PI 4 3 */ ;
  31.  
  32. \  Volume of a cone.   v = pi*r*r*h/3
  33. : VC  ( h r -- vol )   AREA  SWAP 3 */ ;
  34.  
  35. \ Diagonal of a square.  d=s*sqrt(2)
  36. : DI  ( s -- diagonal ) *SQRT(2) ;
  37.  
  38. \ Height of an equilateral triangle. h = s*sqrt(3)/2
  39. : HI  ( s -- height )   *SQRT(3) 2/ ;
  40.  
  41. Even though you are restricted to working with integers you can still do
  42. fractional artimetic.  Here is a brute force approch to displaying
  43. fractions of the form  m/n  where m and n are on the stack and improper
  44. fractions of the form i+m/n where i m and n are on the stack and i is an
  45. integer.
  46.  
  47. \ Brute force approach to fractions.
  48. \ Display decimal equivalent of fraction m/n.
  49. : .XXX  ( m  n    -- )
  50.           2DUP > ABORT" Improper fraction."  \ require m < n
  51.           >R 2000 R>  */   1+  2/   ( Scale and round fraction )
  52.           ASCII . EMIT  DUP 10 <
  53.           IF   ASCII 0 DUP EMIT EMIT
  54.           ELSE DUP 100 <
  55.                IF   ASCII 0 EMIT
  56.                THEN
  57.           THEN  . ;
  58.  
  59. \ Print the decimal equivalent of the mixed fraction i+m/n
  60. : I.XXX  ( i m n    -- )
  61.        ROT 5 .R .XXX ;
  62.  
  63. \ Display decimal equivalents of 1/n through  (n-1)/n
  64. : TEST   ( n   -- )
  65.       CR DUP 1
  66.       ?DO   CR I OVER  2DUP SWAP
  67.              . ." /" . ." = "  .XXX
  68.       LOOP  DROP ;
  69. Sample output from test.  Note that rounding is correct!
  70.  
  71. 7 TEST
  72. 1 /7 = .143
  73. 2 /7 = .286
  74. 3 /7 = .429
  75. 4 /7 = .571
  76. 5 /7 = .714
  77. 6 /7 = .857  ok
  78.  
  79. New word: */MOD ( a b c -- rem(ab/c) quot(ab/c) )
  80.  
  81. This word takes the same stack inputs as */ but leaves both the
  82. remainder and the quotient.
  83. Compute ab/c with 32bit intermediate product ab  and leave quotient
  84. q and remainder r .  Note:  Forth-83 */MOD uses signed values  a b c
  85. and uses floored division.  */MOD  ( a b c --  r q )
  86.  
  87. Example:
  88. \ Calculate area of a circle and display to 3 decimal places.
  89. : .AREA   ( r    -- )
  90.         DUP *   355 113   \ This is ratio for pi
  91.         */MOD  SWAP 113   \ We need remainder for I.XXX
  92.         ." The area of the circle is "  I.XXX  ;
  93. Example:
  94. \ Calculate volume of a sphere and display to 3 decimals.
  95. : .VOLUME  ( r   -- )
  96.         DUP DUP *   SWAP 1420 *   ( r*r  r*1420 )
  97.         339  */MOD  SWAP  339
  98.         ." The volume of the sphere is  "  I.XXX ;
  99. Problem 5.3
  100. Write words that will calculate the surface area of a sphere and the
  101. volume of a cone and display the answers to three decimal places.
  102.  
  103.  
  104.  The file FRACTION.SEQ from Lesson 4 is actually a better way!
  105. \ From the file FRACTION.SEQ
  106. \ The following definiton will display the improper fraction
  107. \ m/n  in the form i.xxxx  where p is the number of decimal
  108. \ digits to follow the decimal point.
  109.  
  110. : I.X  ( m n p -- )
  111.         >R  DUP >R    \ Save precision and a copy of divisor
  112.         /MOD  .       \ Display integer portion.
  113.         1 BACKSPACES
  114.         ASCII . EMIT
  115.         R> R>
  116.         0 DO
  117.             >R  10 R@ */MOD
  118.             48 + EMIT R>
  119.           LOOP 2DROP ;
  120.  
  121.  
  122. ┌────────────────────────────────────┐
  123. │  Please move to Lesson 5 Part 040  │
  124. └────────────────────────────────────┘
  125.