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

  1.        ╔════════════════════════════════════════════════════╗
  2.        ║ Lesson 4 Part 120  F-PC 3.5 Tutorial by Jack Brown ║
  3.        ╚════════════════════════════════════════════════════╝
  4.  
  5.               ┌───────────────────────────────────┐
  6.               │  Double Variables and Constants.  │
  7.               └───────────────────────────────────┘
  8.  
  9. There is also a word set for dealing with double or 32 bit numbers.
  10.  
  11.  
  12. 2VARIABLE   <name>      Creates a 2 cell ( 4 byte ) variable
  13.                         called <name>.
  14.  
  15. <name>    ( --   adr )  When <name> is executed it will push the
  16.                         address of the first cell onto the stack
  17.  
  18. 2CONSTANT   <name>      Creates a double constant called <name>
  19.             ( d    -- ) with the initial value of d
  20.  
  21. <name>      ( --   d  ) When <name> is executed the double
  22.                         number is pushed to the data stack.
  23.  
  24. 2!      ( d  adr   -- ) Store the double number d at adr.
  25.  
  26. 2@      ( adr      d  ) Fetch the double number d from adr.
  27.  
  28. Below are a few examples.  Don't forget to use a decimal point when
  29. entering double numbers and that you must use D. to display them.
  30.  
  31. 2VARIABLE LEN   <enter> ok
  32. 2VARIABLE WTH   <enter> ok
  33. 123456. LEN 2!  <enter> ok
  34. 12345.  WTH 2!  <enter> ok
  35. LEN 2@ D. <enter> 123456  ok
  36. WTH 2@ D. <enter> 12345  ok
  37. \ Compute perimeter of rectangle
  38. : PERIMETER ( -- p )
  39.        LEN 2@ WTH 2@ D+ 2DUP D+ ;  <enter> ok
  40. PERIMETER D. <enter> 271602  ok
  41.  
  42. ╓──────────────╖
  43. ║ Problem 4.20 ║
  44. ╙──────────────╜
  45. Write Forth program that finds the volume and surface area of a sphere.
  46. Use three double variables  RADIUS , AREA  and VOLUME and one double
  47. constant  PI .  You may want to investigate some of the double number
  48. arithmetic operators that appear in the next part of lesson 4.
  49. How are you going to handle the inputing of double numbers from the
  50. user?
  51.                   ┌────────────────────────┐
  52.                   │  Double Number Arrays  │
  53.                   └────────────────────────┘
  54.  
  55. We can create arrays with double numbers by using the operator ,
  56. two times after each double number.
  57.  
  58. CREATE DTABLE 12345. , , -54321. , , 11111. , , -22222. , , <enter> ok
  59. DTABLE 2@ D. <enter> 12345  ok
  60. DTABLE 4 + 2@ D. <enter> -54321  ok
  61.   ok
  62. : DT@ ( n -- dn )  4 * DTABLE + 2@ ; <enter> ok
  63. : DT! ( dn n -- )  4 * DTABLE + 2! ; <enter> ok
  64.   ok
  65. 9999. 3 DT! <enter> ok
  66. 3 DT@ D. <enter> 9999  ok
  67.  
  68.        ┌───────────────────────────────────────────────────┐
  69.        │  New word  */MOD  ( a relative of the */ scalar ) │
  70.        └───────────────────────────────────────────────────┘
  71.  
  72. */MOD ( a b c --  r q )  were q = a*b/c and r is remainder.
  73.  
  74. This word computes a full 32 bit intermediate product before the
  75. division takes place.  It operates the same as */ with the addition of
  76. the remainder as a stack output.  As an application of */MOD we
  77. provide an alternate solution of Problem 2.17, which was to compute
  78. the area of a circle to three decimal places.
  79.  
  80. \ In this solution we use the right justified display operator .R
  81. \ to avoid the use of 1 BACKSPACES which was used to back up over
  82. \ the trailing blank that  .   provides.
  83. \ We have also replaced the phrase  355 * 113 /MOD
  84. \ with the phrase  355 113 */MOD  for a greater range of radii.
  85. \ */MOD   will first multiply by 355 leaving a 32 bit intermediate
  86. \ product and then divide this 32 bit product by 113.
  87.  
  88.  
  89. \ Compute area of a circle to three decimal places.
  90. : CIRCLE_AREA ( r -- )
  91.         DUP * 355 113 */MOD
  92.         ." Area = " 5 .R        \ ( 8 EMIT ) 1 BACKSPACES
  93.         ASCII . EMIT
  94.         3 0  DO
  95.              10 * 113 /MOD 1 .R \ ( 8 EMIT ) 1 BACKSPACES
  96.              LOOP DROP ;
  97.  
  98. : TEST_CIRC ( -- )
  99.        11 1 DO CR I CIRCLE_AREA LOOP ;
  100.  
  101. ╓──────────────╖
  102. ║ Problem 4.21 ║
  103. ╙──────────────╜
  104. Enter the above definition and verify its operation.  What change
  105. would you have to make to compute the area to 5 decimal places?
  106.  
  107. ┌─────────────────────────────────────┐
  108. │   Please move to Lesson 4 Part 130  │
  109. └─────────────────────────────────────┘
  110.