home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / source / l5p030.seq < prev    next >
Text File  |  1990-04-17  |  3KB  |  91 lines

  1. \ Lesson 5 Part 3  ( F-PC 3.5 Tutorial by Jack Brown )
  2. \ One way to use */ is in the rational approximations to common
  3. \ constants. These are from Starting Forth page 122 1st ed, p109 2nd ed.
  4.  
  5. : *PI       355     113 */ ;
  6. : *SQRT(2)  19601 13860 */ ;
  7. : *SQRT(3)  18817 10864 */ ;
  8. : *E        28667 10546 */ ;
  9.  
  10. \ Problem 5.2
  11. \ If you are going to use integer arithmetic in you programs part of
  12. \ your responsibility is to make sure both you and your users know what
  13. \ the range of valid inputs each word or program.  Consider the simple
  14. \ programs given below.  For each of these find the smallest and largest
  15. \ values of the inputs that will still give valid results.
  16.  
  17. \ Area of circle
  18. : AREA ( r -- a )    DUP * *PI  ;
  19.  
  20. \ Volume of sphere  v = pi*r*r*r*4/3
  21. : VS    ( r -- vol )   DUP DUP * * *PI 4 3 */ ;
  22.  
  23. \  Volume of a cone.   v = pi*r*r*h/3
  24. : VC  ( h r -- vol )   AREA  SWAP 3 */ ;
  25.  
  26. \ Diagonal of a square.  d=s*sqrt(2)
  27. : DI  ( s -- diagonal ) *SQRT(2) ;
  28.  
  29. \ Height of an equilateral triangle. h = s*sqrt(3)/2
  30. : HI  ( s -- height )   *SQRT(3) 2/ ;
  31.  
  32.  
  33. \ Even though you are restricted to working with integers you can still do
  34. \ fractional artimetic.  Here is a brute force approch to displaying
  35. \ fractions of the form  m/n  where m and n are on the stack and improper
  36. \ fractions of the form i+m/n where i m and n are on the stack and i is an
  37. \ integer.
  38.  
  39.  
  40. \ Brute force approach to fractions.
  41. \ Display decimal equivalent of fraction m/n.
  42. : .XXX  ( m  n    -- )
  43.           2DUP > ABORT" Improper fraction."  \ require m < n
  44.           >R 2000 R>  */   1+  2/   ( Scale and round fraction )
  45.           ASCII . EMIT  DUP 10 <
  46.           IF   ASCII 0 DUP EMIT EMIT
  47.           ELSE DUP 100 <
  48.                IF   ASCII 0 EMIT
  49.                THEN
  50.           THEN  . ;
  51.  
  52. \ Print the decimal equivalent of the mixed fraction i+m/n
  53. : I.XXX  ( i m n    -- )
  54.        ROT 5 .R .XXX ;
  55.  
  56. \ Display decimal equivalents of 1/n through  (n-1)/n
  57. : TEST   ( n   -- )
  58.       CR DUP 1
  59.       ?DO   CR I OVER  2DUP SWAP
  60.              . ." /" . ." = "  .XXX
  61.       LOOP  DROP ;
  62.  
  63. \ */MOD  a relative of */ ....
  64.  
  65. \ This word takes the same stack inputs as */ but leaves both the
  66. \ remainder and the quotient.
  67.  
  68. \ */MOD ( a b c -- rem(ab/c) quot(ab/c) )
  69.  
  70. \ Compute ab/c with 32bit intermediate product ab  and leave quotient
  71. \ q and remainder r .  Note:  Forth-83 */MOD uses signed values  a b c
  72. \ and uses floored division.  */MOD  ( a b c --  r q )
  73.  
  74. \ Example:
  75. \ Calculate area of a circle and display to 3 decimal places.
  76. : .AREA   ( r    -- )
  77.         DUP *   355 113   \ This is ratio for pi
  78.         */MOD  SWAP 113   \ We need remainder for I.XXX
  79.         ." The area of the circle is "  I.XXX  ;
  80.  
  81. \ Example:
  82. \ Calculate volume of a sphere and display to 3 decimals.
  83. : .VOLUME  ( r   -- )
  84.         DUP DUP *   SWAP 1420 *   ( r*r  r*1420 )
  85.         339  */MOD  SWAP  339
  86.         ." The volume of the sphere is  "  I.XXX ;
  87.  
  88. \ Problem 5.3
  89. \ Write words that will calculate the surfacer area of a sphere and the
  90. \ volume of a cone and display the answers to three decimal places.
  91.